Winsock recv problems

This is a discussion on Winsock recv problems within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, First off I apologize if this is in the wrong forum. I know its a networking question but ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    Winsock recv problems

    Hi all,

    First off I apologize if this is in the wrong forum. I know its a networking question but its specifically for windows.

    I have coded my server to send the string "hello world\r\n\r\n"

    My client attempts to read the string like so

    Code:
    void response(void)
    	{
    	char String[20] = "1234567890123456789";
    	    int RetVal = SOCKET_ERROR;
    		
    		while (RetVal == SOCKET_ERROR)
    		{
    			RetVal = recv(Socket, String, strlen(String), 0);
    		}
    
    		cout << String << endl;
    	}
    Now when I call the "response" function it outputs the first character from the "hello world" string which is obviously a 'h' followed by 19 junk values. If I the call the "response" function again it manages to output "elloWorld".

    Any idea why this is happening?

    Thanks

    Edit: Is there anyway that I can code it so instead of reading everything thats been sent in one go, can I loop through and read one character at a time until say the character '\r' is read? Thanks
    Last edited by cloudy; 09-08-2006 at 03:11 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > Any idea why this is happening?
    The network does NOT guarantee that if you send "hello world" in ONE send() call that you will receive "hello world" in a single recv() call.

    Message fragmentation and reassembly is a problem you need to address yourself when using a TCP connection, since it is a streaming protocol.

    Contrast that with UDP which is a packet oriented protocol (and an unreliable one at that). Either you get the whole packet, or none of the packet.

    > can I loop through and read one character at a time until say the character '\r' is read?
    Yes you can do that if you want.
    Wrap it up in a function called recvAWholeLikeLikeFgets() perhaps, so you don't have to keep doing it over and over.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Hi thanks for the reply. Unfortunately I have another problem now.

    My server only ever receives messages sent to it once the client calls the CloseSocket method.

    So this works fine (client code)

    Code:
    recv(variables); // Receive servers greeting
    send(variables); // Send Command
    CloseSocket(sock);
    However the following doesn't

    Code:
    recv(variables); // Reveive servers greeting (THIS LINE WORKS)
    send(variables) // Send Command (Server does not receive this)
    recv(variables) // Wait servers reply (Blocks here)
    send(variables) // Reply to server (never executes)
    CloseSocket(Sock);
    Basically whats happening is the server sends its greeting which is received by the client fine. The client attempts to reply with send but server doesnt receive until client closes socket. Then the client blocks waiting for a reply that wont come.

    Sorry for the lack of information as im off out now, if you need more detail i'll post more when i get back.

    I think that maybe if i flush the stream after each client "send" command it might work. Is there a flush command for WinSock?

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about recv
    By carrotcake1029 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-26-2009, 01:10 PM
  2. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 07:32 AM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  4. recv()
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-24-2004, 04:32 PM
  5. TCP Programming problems with recv
    By stovellpaul in forum C++ Programming
    Replies: 1
    Last Post: 09-08-2002, 04:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21