Thread: Errors with chat client send() recv()

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    51

    Errors with chat client send() recv()

    Code:
    if (FD_ISSET(i, &read_fds)) 
    	    		{ // we got one!!
    				
    				
    
    				if (i == STDIN_FILENO) /* Check keyboard */
    		    		{	        		
    		        		/*fputs(identifier, stdout);*/
    					fgets(msg, sizeof msg, stdin);
    					/*send(sockfd, newLine, strlen(newLine), 0);
    					send(sockfd, identifier, strlen(identifier), 0);*/
    					int count = send(sockfd, msg, strlen(msg), 0);
    					msg[count] = '\0';
    		    		}
    				else if (i == sockfd)
    				{
    					int count = recv(sockfd, buf, sizeof buf -1, 0);
    					printf("%s", buf);
    					buf[count] = '\0';
    					
    					
    				}
    			}
    This code isn't working. It is an extract from my client.c file.

    The first message works fine but subsequent messages, if shorter, will show the latter part of the previous message on the line below. I added a line to print out everytime recv was called and confirmed it was only once, so the problem is elsewhere

    Anyone any suggestions?

    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Looks like a classic case of "I've been staring at this screen WAY too long now"

    Code:
    					printf("%s", buf);
    					buf[count] = '\0';
    Do you see a problem here?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Im quite confused by your description.

    The first message works fine but subsequent messages,
    Is the "first message" in your code above? Are you talking about send messages or receive messages thats the problem?

    I added a line to print out everytime recv was called and confirmed it was only once, so the problem is elsewhere
    What? So is the problem in the above code or not?

    Anyways, I would definitely change this
    Code:
    printf("%s", buf);
    buf[count] = '\0';
    to
    Code:
    buf[count] = '\0';
    printf("%s", buf);

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Quote Originally Posted by MK27 View Post
    Looks like a classic case of "I've been staring at this screen WAY too long now"

    Code:
    					printf("%s", buf);
    					buf[count] = '\0';
    Do you see a problem here?
    You have identified the error 100%. I was throwing a inabiliyToSeeTheCodeInFrontOfMe exception.

    Thank you, once again.

    I just had a quick 5 minute break to try and refresh.....not sure its worked but fingers crossed

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    51
    Quote Originally Posted by nadroj View Post
    Im quite confused by your description.
    Yes sorry my description was poor. But it's all sorted now. My brain is slowly shutting down

    I appreciate your reply though.

    It is only through this great forum that I am making more sense of C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a file to send to an http client
    By xixpsychoxix in forum Networking/Device Communication
    Replies: 4
    Last Post: 06-22-2008, 09:05 AM
  2. Sending 2 things simultaniously to same client.
    By antex in forum Networking/Device Communication
    Replies: 9
    Last Post: 04-16-2007, 01:37 PM
  3. send and recv
    By DeadManWalking in forum C Programming
    Replies: 1
    Last Post: 12-04-2005, 09:17 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. receiving problems in network chat client
    By chris285 in forum C++ Programming
    Replies: 5
    Last Post: 01-11-2005, 05:10 AM