linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: 朱雪婷 <zxt@mail.ndsc.com.cn>
To: linux-c-programming <linux-c-programming@vger.kernel.org>
Subject: what does sendto() return?
Date: Wed, 15 Mar 2006 16:25:11 +0800	[thread overview]
Message-ID: <MAIL5aYY61LEdyt3cew00000688@mail.ndsc.com.cn> (raw)

[-- 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);
		}	
	
}

                 reply	other threads:[~2006-03-15  8:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=MAIL5aYY61LEdyt3cew00000688@mail.ndsc.com.cn \
    --to=zxt@mail.ndsc.com.cn \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).