* what does sendto() return?
@ 2006-03-15 8:25 朱雪婷
0 siblings, 0 replies; only message in thread
From: 朱雪婷 @ 2006-03-15 8:25 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 1540 bytes --]
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 #2: server.c --]
[-- Type: application/octet-stream, Size: 1138 bytes --]
/*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);
}
}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2006-03-15 8:25 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-03-15 8:25 what does sendto() return? 朱雪婷
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).