what does sendto() return?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi everyone,
I've read manpage of send. It says"The calls return the number of characters sent, or -1 if an error occurred." But when I practice programming about udp socket, the server return 0 when it actually sent data. I am really puzzled. The attachment is a little code I wrote in the server port or you can see it as below. Can anyone help me ?
Thank you .
------------------------------------------------------------------------------------
/*Authors: zxt
 *problem left:
 *in line 38:count=sendto(sd,"test from server",sizeof("test from server"),0,(struct sockaddr *) &s_addr,sizeof(s_addr))<0)
 *when captured with etheral, it is found that the data has been sent, 
 *while the printed message shows that "0 byte send success!" 
 */
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>


int port = 5678;
int sendingcount=3;
char *dest_ip="100.0.0.9";
char bufftosend[40]="test from server";

int main(void)
{
	int sd;
	int i;
	int count;
	struct sockaddr_in s_addr;
	sd=socket(AF_INET,SOCK_DGRAM,0);
	printf("sd==%d\n",sd);
	if(sd==-1)
		{printf("socket created error!\n");
		 exit(0);
		}
	
       memset(&s_addr,0,sizeof(s_addr));
       s_addr.sin_family=AF_INET;
	s_addr.sin_addr.s_addr=inet_addr(dest_ip);
	s_addr.sin_port=htons(port);
	
       for(i=0;i<sendingcount;i++)
		{
		if(count=sendto(sd,bufftosend,sizeof(bufftosend),0,(struct sockaddr *) &s_addr,sizeof(s_addr))<0)
			{
			printf("send error!\n");
			exit(0);
			}
		printf("%d byte send success!\n",count);
		sleep(3);
		}	
	
}

Attachment: server.c
Description: Binary data


[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux