question about recv()

This is a discussion on question about recv() within the C Programming forums, part of the General Programming Boards category; So, I wrote a small server program where when I run it I specify a file in the command line ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    71

    question about recv()

    So, I wrote a small server program where when I run it I specify a file in the command line arguments... then when a client connects to that server the server starts sending the file to the client.

    Once the file is received by the client the connection is terminated and both programs close down. It works perfectly. There is just one thing I got to work by mainly fiddling around. I needed a way to know when the file was done transmitting so I could stop receiving packets. The way I did this is:

    Code:
    	while(1){
    		drec = recv(sockfd, buf, PSIZE, 0);
    		if(drec <= 0){
    			break;
    		}
    		fwrite(buf, 1, drec, f);
    	}
    I'm pretty sure recv() is a blocking function unless otherwise specified. If that's the case, then why is it that recv() returns 0 if none is received? I thought it waits there until it does receive something... meaning drec couldn't = 0 and exit the loop... however it does get 0 then exits the loop.

    If it helps the code which I'm using on my server side to send the file is:

    Code:
    	while(!feof(f)){
    		dread = fread(buf,sizeof(char),PSIZE,f);
    		dsent = send(newfd, buf, dread, 0);
    	}
    could it be that drec is really -1 and exiting the loop that way... since the server cut the connection... i really don't know...

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Yes, on error recv() returns -1, or if the connection was closed properly then recv returns 0. You do have the documentation for the function don't you?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    71
    So, if my server turns off the connection properly while the client is currently blocking on recv, recv will return 0? but if the connection were to just suddenly terminate it returns -1? I've been getting my information from multiple different sources so it's kinda... jumbled...

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Homunculus View Post
    could it be that drec is really -1 and exiting the loop that way... since the server cut the connection... i really don't know...
    Wellllll... you could try printing the value of drec and see....

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Well, what sources? Did you try your compiler's help file? If you are using windows there is always: MSDN-recv() and if you are on *nix check the man pages. These are the definitive sources along with your compiler specific help files.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recv() second arg
    By Ducky in forum Networking/Device Communication
    Replies: 4
    Last Post: 07-04-2009, 02:48 PM
  2. Question about recv
    By carrotcake1029 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-26-2009, 01:10 PM
  3. When recv() can't
    By cboard_member in forum Windows Programming
    Replies: 6
    Last Post: 02-12-2006, 06:14 PM
  4. Simple Question recv
    By MicroFiend in forum Networking/Device Communication
    Replies: 4
    Last Post: 08-28-2004, 02:28 PM
  5. recv()
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-24-2004, 04:32 PM

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