Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[C++] winsock and char* sendbuf

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Thu Mar 26, 2009 8:58 pm    Post subject: [C++] winsock and char* sendbuf Reply with quote

I'm writing a simple server-client communication program, but I've always seen to get in some kind of obstacle. The obstacle that I'm stuck at now is that the first character of the buffer is always missing. I have no idea why and I checked my code over and over.

Code:

/* client.cpp */

. . .

char* sendbuf = new char[DEFAULT_BUFLEN];
cin.ignore(); // clear the cin buffer
cout << "Client: ";
cin.getline(sendbuf, DEFAULT_BUFLEN, '\n');
iResult = send(ConnectedSocket, sendbuf, (int)strlen(sendbuf), 0);
if(iResult == SOCKET_ERROR)
{
     closesocket(ConnectedSocket);
     WSACleanup();
     return 1;
}

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
   cout << "Server: " << recvbuf << endl;
   delete sendbuf;
}


How do I get the IP of the client or server? I used the addrinfo struct.

P.S. My coding methods aren't the best and it's just works. If a critic like x0r can go ahead and point out ways to optimize the code, that would be most helpful.


Last edited by nwongfeiying on Thu Mar 26, 2009 9:05 pm; edited 1 time in total
Back to top
View user's profile Send private message
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Mar 26, 2009 9:05 pm    Post subject: Reply with quote

It's because you are using std::cin.ignore() incorrectly. The function you are looking for is std::cin.sync().
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Thu Mar 26, 2009 9:05 pm    Post subject: Reply with quote

Do what flyte said. But also, you want to add a null character to the end of recvbuf to prevent garbage data in your packets. And since sendbuf is an array of characters you have to use "del []", not "del".

Code:

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if(iResult > 0) {
   recvbuf[iResult] = '\0';
   cout<<"Server: "<<recvbuf<<endl;
   del [] sendbuf;
}

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing


Last edited by oib111 on Thu Mar 26, 2009 9:07 pm; edited 1 time in total
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Thu Mar 26, 2009 9:06 pm    Post subject: Reply with quote

Flyte, can I add you on MSN? I added you, but I got a feeling that you rejected it.
Back to top
View user's profile Send private message
slippppppppp
Grandmaster Cheater
Reputation: 0

Joined: 08 Aug 2006
Posts: 929

PostPosted: Thu Mar 26, 2009 9:08 pm    Post subject: Reply with quote

Code:

char SendBuffer[500];
char RecvBuffer[500];
puts( "Client: " );
gets( SendBuffer );
if( send(ConnectedSocket, SendBuffer, 500, 0) == SOCKET_ERROR ) {
     closesocket(ConnectedSocket);
     WSACleanup();
     return 1;
}

if ( recv(ConnectSocket, RecvBuffer, 500, 0) != SOCKET_ERROR ) {
   puts( RecvBuffer );
   delete[] SendBuffer;
}
Back to top
View user's profile Send private message AIM Address MSN Messenger
Flyte
Peanuts!!!!
Reputation: 6

Joined: 19 Apr 2006
Posts: 1887
Location: Canada

PostPosted: Thu Mar 26, 2009 9:10 pm    Post subject: Reply with quote

nwongfeiying wrote:
Flyte, can I add you on MSN? I added you, but I got a feeling that you rejected it.


Haha, I never go on it anymore. I'll hop on quickly if you need help with something.
Back to top
View user's profile Send private message
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Thu Mar 26, 2009 9:38 pm    Post subject: Reply with quote

I got it to work for the most part, but sometimes there's an extra character (it's a character that I can't type out) attached at the end of the message. Sometimes, (seems to only be happening to the server) part of the message ending is cut off too.
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Thu Mar 26, 2009 9:52 pm    Post subject: Reply with quote

Once again, remember to add a null character to the end of the recvbuf.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Thu Mar 26, 2009 10:09 pm    Post subject: Reply with quote

oib111 wrote:
Once again, remember to add a null character to the end of the recvbuf.


I'll do that, but can you explain the logic in it? How does adding a null character stop it from cutting off part of the message or preventing the addition of the (I can't' type it out) to be there?
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Fri Mar 27, 2009 1:37 am    Post subject: Reply with quote

Signals end of a string.
Back to top
View user's profile Send private message
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Fri Mar 27, 2009 8:29 am    Post subject: Reply with quote

Moose wrote:
technically you don't need to append a null char to a buffer being send via winsock. it won't result in 'garbage data' either, dunno what that guy is talking about.


Well, do you know the reason for the message being cut off or the extra characters at the end? It doesn't always happen, but sometimes it does.
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Mar 27, 2009 4:54 pm    Post subject: Reply with quote

Moose, I used to not add the null character and I would always get garbage data at the end of my real data.
_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Fri Mar 27, 2009 5:02 pm    Post subject: Reply with quote

Moose, will you had me on MSN? I'm going to try adding a null character at the end.
Back to top
View user's profile Send private message
oib111
I post too much
Reputation: 0

Joined: 02 Apr 2007
Posts: 2947
Location: you wanna know why?

PostPosted: Fri Mar 27, 2009 5:09 pm    Post subject: Reply with quote

Lol...

Code:

iResult = recv(ConnectSocket, recvbuf, recvbuflen, NULL);
recvbuf[iResult] = '\0';

_________________


8D wrote:

cigs dont make people high, which weed does, which causes them to do bad stuff. like killing
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Fri Mar 27, 2009 5:29 pm    Post subject: Reply with quote

oib111 wrote:
Lol...

Code:

iResult = recv(ConnectSocket, recvbuf, recvbuflen, NULL);
recvbuf[iResult] = '\0';


You already posted that. I just wanted Moose on my MSN (if he has one). If someone would help me with my second problem (below the code), that would be great.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites