send and receive byte does not match

This is a discussion on send and receive byte does not match within the Networking/Device Communication forums, part of the General Programming Boards category; hi I was working on a UDP client and echo server. Let say my buffer is 20 byte only. I ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    send and receive byte does not match

    hi

    I was working on a UDP client and echo server. Let say my buffer is 20 byte only. I can send 20 byte form the server, and the server receives 20 byte successfully. But the problem arise when the server wants to echo the same thing inside the received buffer back to the client in which it will send 21 byte. another strange thing is that the client will accept 20 byte only..Is there some thing that I need to pay attention to with the sendto and recvfrom?


    here is the server side
    Code:
    while (1) {
    		fflush(stdout);
    		
    		/*if ( (sd2=accept(sd, (struct sockaddr *)&cad, &alen)) < 0) {
    			fprintf(stderr, "accept failed\n");
    			exit(1);
    		}*/				
    		//printf("this is the child\n");
    		/* The sd has data available to be read */
    		//close(sd);
    		//clear buffer
    		memset(buf, 0, sizeof(buf));
    		n = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *)&cad, &alen);
    		
    		//while there is still data
    		while(n > 0){
    			printf("received size: %d\n", n);
    			printf("%s\n", buf);
    			sleep(1);
    			n = sendto(sd, buf, strlen(buf), 0, (struct sockaddr *)&cad, sizeof(cad));
    			printf("Byte send back to client: %d\n", n);
    			//clear buffer
    			memset(buf, 0, sizeof(buf));
    			//receive data into buffer again
    			n = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *)&cad, &alen);
    		}
    }
    and here is the client side

    Code:
    	n = read(fileno(ifp), buf, sizeof(buf));
    
    	while(n > 0)
    	{
    
    		//read from file
    		printf("Number of byte read from file: %d\n",n);
         		printf("%s\n", buf);
    		sendto(sd,buf,strlen(buf), 0, (struct sockaddr *)&sad, sizeof(sad));
    		
    		//clear the buffer to avoid any garbage
    		memset(buf, 0, sizeof(buf));
    		
    		alen = sizeof(sad);
    		//read the echo from server		
    		n = recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr *)&sad, &alen);
    		printf("number of byte received from server: %d\n", n);
    		printf("%s\n", buf);
    
    		//write it out to to the output file		
    		n = write(fileno(outputfiledesc), buf, n);
    		
    		memset(buf, 0, sizeof(buf));
    		//get character, print and send it out
    		n = read(fileno(ifp), buf, sizeof(buf));
    		
    	}
    another strange problem is that when I run wireshark to check the packets, it saids that checksum incorrect? Can someone point me into the right direction?

    thank you for all the help

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    I'm not immediately seeing your error. You say it's sending 1 byte more - what is it? garbage? More details.

    Also, you need to ensure that your data is terminated with a nul before passing it to printf(), or you'll get whatever garbage comes after your string. You come close with your memset(), but you're 1 off - you might clear a 20 byte buffer, but then you tell recvfrom() that the buffer is twenty bytes - if all 20 are filled, then your buffer is no longer nul terminated, and printf will overrun the end.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get RSSI value, send to sensor, sensor receive package, repackage it?
    By techissue2008 in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-04-2009, 10:13 AM
  2. Replies: 3
    Last Post: 05-17-2007, 07:09 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Send and receive through serial Port
    By overspray in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2004, 05:15 PM

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