logo

Navigation:First_Community->Software->Linux development Goto:New TopicSettingSearch
Linux网络编程--5. 用户数据报发送
Posted by: imac Date: March 29, 2008 02:06AM

MegaEntry - Social networking and discussion site!

    我们前面已经学习网络程序的一个很大的部分,由这个部分的知识,我们实际上可以写出大部分的基于TCP协议的网络程序了.现在在Linux下的大部分程序都是用我们上面所学的知识来写的.我们可以去找一些源程序来参考一下.这一章,我们简单的学习一下基于UDP协议的网络程序. 

5.1 两个常用的函数 

   int recvfrom(int sockfd,void *buf,int len,unsigned int flags,struct sockaddr * from int *fromlen)    int sendto(int sockfd,const void *msg,int len,unsigned int flags,struct sockaddr *to int tolen)

sockfd,buf,len的意义和read,write一样,分别表示套接字描述符,发送或接收的缓冲区及大小.recvfrom负责从sockfd接收数据,如果from不是NULL,那么在from里面存储了信息来源的情况,如果对信息的来源不感兴趣,可以将from和fromlen设置为NULL.sendto负责向to发送信息.此时在to里面存储了收信息方的详细资料. 

MegaEntry - Social networking and discussion site!

5.2 一个实例 

/*           服务端程序  server.c           */

#include  #include  #include  #include 

CopyRight owned by the original author.--(www.MegaEntry.com)

#include  #define SERVER_PORT     8888 #define MAX_MSG_SIZE    1024

void udps_respon(int sockfd) {         struct sockaddr_in addr;

CopyRight owned by the original author.--(www.MegaEntry.com)

        int     addrlen,n;         char    msg[MAX_MSG_SIZE];                  while(1)         {       /* 从网络上度,写到网络上面去   */                 n=recvfrom(sockfd,msg,MAX_MSG_SIZE,0,

CopyRight owned by the original author.--(www.MegaEntry.com)

                        (struct sockaddr*)&addr,&addrlen);                 msg[n]=0;                 /* 显示服务端已经收到了信息  */                 fprintf(stdout, "I have received %s ",msg);                 sendto(sockfd,msg,n,0,(struct sockaddr*)&addr,addrlen);         }

CopyRight owned by the original author.--(www.MegaEntry.com)

}

int main(void) {         int sockfd;         struct sockaddr_in      addr;         

CopyRight owned by the original author.--(www.MegaEntry.com)

        sockfd=socket(AF_INET,SOCK_DGRAM,0);         if(sockfd<0)         {                 fprintf(stderr, "Socket Error:%s ",strerror(errno));                 exit(1);         }

CopyRight owned by the original author.--(www.MegaEntry.com)

        bzero(&addr,sizeof(struct sockaddr_in));         addr.sin_family=AF_INET;         addr.sin_addr.s_addr=htonl(INADDR_ANY);         addr.sin_port=htons(SERVER_PORT);         if(bind(sockfd,(struct sockaddr *)&ddr,sizeof(struct sockaddr_in))<0)         {

MegaEntry - Social networking and discussion site!

                fprintf(stderr, "Bind Error:%s ",strerror(errno));                 exit(1);         }         udps_respon(sockfd);         close(sockfd); }

CopyRight owned by the original author.--(www.MegaEntry.com)

/*          客户端程序             */ #include  #include  #include  #include 

CopyRight owned by the original author.--(www.MegaEntry.com)

#include  #include  #define MAX_BUF_SIZE    1024

void udpc_requ(int sockfd,const struct sockaddr_in *addr,int len) {         char buffer[MAX_BUF_SIZE];

CopyRight owned by the original author.--(www.MegaEntry.com)

        int n;         while(1)                 {        /*   从键盘读入,写到服务端   */                 fgets(buffer,MAX_BUF_SIZE,stdin);                 sendto(sockfd,buffer,strlen(buffer),0,addr,len);                 bzero(buffer,MAX_BUF_SIZE);

CopyRight owned by the original author.--(www.MegaEntry.com)

                /*   从网络上读,写到屏幕上    */                 n=recvfrom(sockfd,buffer,MAX_BUF_SIZE,0,NULL,NULL);                 buffer[n]=0;                 fputs(buffer,stdout);         } }

MegaEntry - Social networking and discussion site!

int main(int argc,char **argv) {         int sockfd,port;         struct sockaddr_in      addr;                  if(argc!=3)

CopyRight owned by the original author.--(www.MegaEntry.com)

        {                 fprintf(stderr, "Usage:%s server_ip server_port ",argv[0]);                 exit(1);         }                  if((port=atoi(argv[2]))<0)

MegaEntry - Social networking and discussion site!

        {                 fprintf(stderr, "Usage:%s server_ip server_port ",argv[0]);                 exit(1);         }                  sockfd=socket(AF_INET,SOCK_DGRAM,0);

CopyRight owned by the original author.--(www.MegaEntry.com)

        if(sockfd<0)         {                 fprintf(stderr, "Socket  Error:%s ",strerror(errno));                 exit(1);         }                /*      填充服务端的资料      */

CopyRight owned by the original author.--(www.MegaEntry.com)

        bzero(&addr,sizeof(struct sockaddr_in));         addr.sin_family=AF_INET;         addr.sin_port=htons(port);         if(inet_aton(argv[1],&addr.sin_addr)<0)         {                 fprintf(stderr, "Ip error:%s ",strerror(errno));

MegaEntry - Social networking and discussion site!

                exit(1);         }         udpc_requ(sockfd,&addr,sizeof(struct sockaddr_in));         close(sockfd); }

########### 编译文件 Makefile        ##########

MegaEntry - Social networking and discussion site!

all:server client server:server.c         gcc -o server server.c client:client.c         gcc -o client client.c clean:

CopyRight owned by the original author.--(www.MegaEntry.com)

        rm -f server         rm -f client         rm -f core

上面的实例如果大家编译运行的话,会发现一个小问题的. 在我机器上面,我先运行服务端,然后运行客户端.在客户端输入信息,发送到服务端, 在服务端显示已经收到信息,但是客户端没有反映.再运行一个客户端,向服务端发出信息 却可以得到反应.我想可能是第一个客户端已经阻塞了.如果谁知道怎么解决的话,请告诉我,谢谢. 由于UDP协议是不保证可靠接收数据的要求,所以我们在发送信息的时候,系统并不能够保证我们发出的信息都正确无误的到达目的地.一般的来说我们在编写网络程序的时候都是选用TCP协议的. 



Reply To This Message
Subject: 

Copyright 2005-2006 megaentry,All Rights Reserved
IE 6.0 or above is perfect