| View previous topic :: View next topic |
| Author |
Message |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Thu Mar 26, 2009 8:58 pm Post subject: [C++] winsock and char* sendbuf |
|
|
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 |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Mar 26, 2009 9:05 pm Post subject: |
|
|
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 |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Thu Mar 26, 2009 9:06 pm Post subject: |
|
|
| Flyte, can I add you on MSN? I added you, but I got a feeling that you rejected it.
|
|
| Back to top |
|
 |
slippppppppp Grandmaster Cheater
Reputation: 0
Joined: 08 Aug 2006 Posts: 929
|
Posted: Thu Mar 26, 2009 9:08 pm Post subject: |
|
|
| 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 |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Mar 26, 2009 9:10 pm Post subject: |
|
|
| 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 |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Thu Mar 26, 2009 9:38 pm Post subject: |
|
|
| 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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Thu Mar 26, 2009 9:52 pm Post subject: |
|
|
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 |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Thu Mar 26, 2009 10:09 pm Post subject: |
|
|
| 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 |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Fri Mar 27, 2009 1:37 am Post subject: |
|
|
| Signals end of a string.
|
|
| Back to top |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Fri Mar 27, 2009 8:29 am Post subject: |
|
|
| 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 |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Mar 27, 2009 4:54 pm Post subject: |
|
|
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 |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Fri Mar 27, 2009 5:02 pm Post subject: |
|
|
| Moose, will you had me on MSN? I'm going to try adding a null character at the end.
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Mar 27, 2009 5:09 pm Post subject: |
|
|
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 |
|
 |
nwongfeiying Grandmaster Cheater
Reputation: 2
Joined: 25 Jun 2007 Posts: 695
|
Posted: Fri Mar 27, 2009 5:29 pm Post subject: |
|
|
| 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 |
|
 |
|