![]() |
| | #1 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| Winsock, weird sideeffect It's a non-blocking system, the client sends simply using send() and the server's recieve-part looks like this: Code: case FD_READ:
{
CHAR Char;
INT Result;
std::string String;
std::vector<CHAR> Buffer;
while(TRUE)
{
Result = recv(W, &Char, 1, 0);
if(Result == SOCKET_ERROR)
{
if(WSAGetLastError() == WSAEWOULDBLOCK) break;
ErrorMessage = "recv() failed!";
return FALSE;
}
if(Result < 1) break;
Buffer.push_back(Char);
}
Buffer.push_back('\0');
String = "MSG: ";
String += &Buffer[0];
AddMessage(String);
return TRUE;
}
Code: MSG: Hello MSG: MSG: MSG: MSG: MSG:
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline | |
| | #2 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| It seems to me you add a string even if your while loop exits on the first pass.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #3 |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| I could add: Code: if(String != "") AddMessage(String);
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. |
| Magos is offline | |
| | #4 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Are you saying that you read a char, then the loop breaks out because of this if(WSAGetLastError() == WSAEWOULDBLOCK) break;
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #5 | |
| Registered User Join Date: Jan 2005
Posts: 847
| Quote:
| |
| Quantum1024 is offline | |
| | #6 | |
| Confused Join Date: Sep 2001 Location: Sweden
Posts: 3,122
| Quote:
__________________ MagosX.com Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. | |
| Magos is offline | |
| | #7 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| Is there some specific API you can use to find the amount of data available at the moment? At the moment, you can only guarantee with 'data available' that there is at least one byte. If you try and read 10 say, and there is only 9 available, then I think you're going to get "WOULDBLOCK" back straight away.
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #8 |
| Registered User Join Date: May 2005
Posts: 24
| you can (I think) query the recieve buffer with something like this: needed = recv(W, &Char, 0, MSG_PEEK); |
| legend is offline | |
| | #9 |
| Registered User Join Date: Jan 2005
Posts: 847
| MSG_PEEK fills the buffer just the same the only difference is that the data isn't removed from wnsock's internal bufers so another recv call would return the same data. I don't recomend using MSG_PEEK EVER! To get the amount of data available use ioctlsocket with the FIONREAD flag. The 3rd argument to recv isn't the amount of data you want to receive it is the length of the buffer where the data will be placed so if there is less data then the buffer length recv will still return with no error, the amount received is the return value. |
| Quantum1024 is offline | |
| | #10 |
| Registered User Join Date: May 2005
Posts: 24
| I was basing my advice on this from MSDN: "MSG_PEEK: Peeks at the incoming data. The data is copied into the buffer, but is not removed from the input queue. The function subsequently returns the amount of data that can be read in a single call to the recv." but on closer inspection: "...which may not be the same as the total amount of data queued on the socket". so that probably wouldn't be such a good idea after all. |
| legend is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Winsock issues | tjpanda | Windows Programming | 3 | 12-04-2008 08:32 AM |
| Winsock Messaging Program | Morgul | Windows Programming | 13 | 04-25-2005 04:00 PM |
| Where do I initialize Winsock and catch messages for it? | Lithorien | Windows Programming | 10 | 12-30-2004 12:11 PM |
| weird winsock output and winsock 2 not working | whackaxe | Networking/Device Communication | 4 | 07-09-2004 11:10 AM |
| winsock | pode | Networking/Device Communication | 2 | 09-26-2003 12:45 AM |