Winsock recv help
-
Hello! Im fairly new to winsock programing, and am having trouble receiving a response that is spread accross several packets. I have been watching the communications through a packet sniffer, and know that 30 to 40 1500 byte sized packets are comming in with good data, but my program is only executing one recv and reporting a bytes Recv of 9. There must be some concept that im completely missing :( any help would be greatly appreciated. heres my code: bytesRecv = SOCKET_ERROR; char queryRecvBuff[1500] = ""; CString reply = ""; send(m_socket,serverQuery,30,0); TRACE("SENT\nRECEIVING:\n"); while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, queryRecvBuff, 1500, 0); if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) { TRACE( "Connection Closed in Auth.\n"); break; } if (bytesRecv < 0){ TRACE("\tbytes Recv < 0!!!"); return; } reply.Append(queryRecvBuff,bytesRecv); TRACE( "\tBytes Recv: %ld\n", bytesRecv ); } TRACE("\nRECEIVED"); output: WAITING FOR Verification: VERIFIED Sending Query: SENT RECEIVING: Bytes Recv: 9 RECEIVED
-
Hello! Im fairly new to winsock programing, and am having trouble receiving a response that is spread accross several packets. I have been watching the communications through a packet sniffer, and know that 30 to 40 1500 byte sized packets are comming in with good data, but my program is only executing one recv and reporting a bytes Recv of 9. There must be some concept that im completely missing :( any help would be greatly appreciated. heres my code: bytesRecv = SOCKET_ERROR; char queryRecvBuff[1500] = ""; CString reply = ""; send(m_socket,serverQuery,30,0); TRACE("SENT\nRECEIVING:\n"); while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, queryRecvBuff, 1500, 0); if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) { TRACE( "Connection Closed in Auth.\n"); break; } if (bytesRecv < 0){ TRACE("\tbytes Recv < 0!!!"); return; } reply.Append(queryRecvBuff,bytesRecv); TRACE( "\tBytes Recv: %ld\n", bytesRecv ); } TRACE("\nRECEIVED"); output: WAITING FOR Verification: VERIFIED Sending Query: SENT RECEIVING: Bytes Recv: 9 RECEIVED
Knave777Wave wrote: while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, queryRecvBuff, 1500, 0); I don't have all your code (ie. a server to send responses back) so I can't step through your code to really help you. But based on the code above: SOCKET_ERROR is defined as having value -1 recv(...) is returning 9 bytes to you This explains why the loop is only run once as bytesRecv will contain the value 9 which is not equal to -1 (SOCKET_ERROR). As to if this is the proper value that is being recieved I don't know. If the first packet being sent from the server is of size 9 then this makes sense. Otherwise, I don't really know the reason. Greba, My lack of content on my home page should be entertaining.
-
Knave777Wave wrote: while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, queryRecvBuff, 1500, 0); I don't have all your code (ie. a server to send responses back) so I can't step through your code to really help you. But based on the code above: SOCKET_ERROR is defined as having value -1 recv(...) is returning 9 bytes to you This explains why the loop is only run once as bytesRecv will contain the value 9 which is not equal to -1 (SOCKET_ERROR). As to if this is the proper value that is being recieved I don't know. If the first packet being sent from the server is of size 9 then this makes sense. Otherwise, I don't really know the reason. Greba, My lack of content on my home page should be entertaining.
thnx... i dont know what i was thinking I switched it to a do--while w/ (bytesRecv != SOCKET_ERROR) and now i receive tons of packets and jump out of the loop with bytesRecv == 0. Is this the normal behavior for such a transaction to end? Ive noticed several empty data packets being sent in the sniffer. Are these special packets that singal 'received' and so forth? If so, is my app receiving a special 'transfer done' packet that causes my last recv to unblock and return 0 bytes? I dont know what the server is doing since it is not my app, its a particular games master server with a list of public game servers. I know all of this sounds pretty dumb, but this is a learning experiment where im trying to dive in a learn what is going on at the packet and app level. thnx
-
thnx... i dont know what i was thinking I switched it to a do--while w/ (bytesRecv != SOCKET_ERROR) and now i receive tons of packets and jump out of the loop with bytesRecv == 0. Is this the normal behavior for such a transaction to end? Ive noticed several empty data packets being sent in the sniffer. Are these special packets that singal 'received' and so forth? If so, is my app receiving a special 'transfer done' packet that causes my last recv to unblock and return 0 bytes? I dont know what the server is doing since it is not my app, its a particular games master server with a list of public game servers. I know all of this sounds pretty dumb, but this is a learning experiment where im trying to dive in a learn what is going on at the packet and app level. thnx
Under the documentation of the recv function: "If the connection has been gracefully closed, the return value is zero". I'm assuming this would mean that the server has closed the connection. As to if this a correct or not assumption I don't know. It doesn't sound pretty dumb, that is definitely a challanging and rewarding way to learn. If you still have problems with your recv, then I would suggest creating a server which emulates the behavior of the real server, and determine if you can send empty packets, and what happens when you close down a connection. Greba, My lack of content on my home page should be entertaining.