欢迎来到天天文库
浏览记录
ID:19038243
大小:22.60 KB
页数:9页
时间:2018-09-28
《性能测试脚本socket_udp协议》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、一.UDP和Socket通信步骤1.UDPServer程序1、编写UDPServer程序的步骤(1)使用socket()来建立一个UDPsocket,第二个参数为SOCK_DGRAM。(2)初始化sockaddr_in结构的变量,并赋值。sockaddr_in结构定义:structsockaddr_in{uint8_tsin_len;sa_family_tsin_family;in_port_tsin_port;structin_addrsin_addr;charsin_zero[8];};这里使用“08”作为服务程序的端口,使用“INADDR_ANY”作为绑定的IP
2、地址即任何主机上的地址。(3)使用bind()把上面的socket和定义的IP地址和端口绑定。这里检查bind()是否执行成功,如果有错误就退出。这样可以防止服务程序重复运行的问题。(4)进入无限循环程序,使用recvfrom()进入等待状态,直到接收到客户程序发送的数据,就处理收到的数据,并向客户程序发送反馈。这里是直接把收到的数据发回给客户程序。2、udpserv.c程序内容:#include#include#include#include#include3、io.h>#include#defineMAXLINE80#defineSERV_PORT8888voiddo_echo(intsockfd,structsockaddr*pcliaddr,socklen_tclilen){intn;socklen_tlen;charmesg[MAXLINE];otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary4、.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommi5、tteeSecretary,Ministersandfor(;;){len=clilen;/*waitingforreceivedata*/n=recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len);/*sentdatabacktoclient*/sendto(sockfd,mesg,n,0,pcliaddr,len);}}intmain(void){intsockfd;structsockaddr_inservaddr,cliaddr;sockfd=socket(AF_INET,SOCK_DGRAM,0);/*createasoc6、ket*//*initservaddr*/bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(SERV_PORT);/*bindaddressandporttosocket*/if(bind(sockfd,(structsockaddr*)&servaddr,sizeof(servaddr))==-1){perror("binderror");exit(1);}do7、_echo(sockfd,(structsockaddr*)&cliaddr,sizeof(cliaddr));return0;}2.UDPClient程序1、编写UDPClient程序的步骤otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganiza
3、io.h>#include#defineMAXLINE80#defineSERV_PORT8888voiddo_echo(intsockfd,structsockaddr*pcliaddr,socklen_tclilen){intn;socklen_tlen;charmesg[MAXLINE];otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary
4、.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommi
5、tteeSecretary,Ministersandfor(;;){len=clilen;/*waitingforreceivedata*/n=recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len);/*sentdatabacktoclient*/sendto(sockfd,mesg,n,0,pcliaddr,len);}}intmain(void){intsockfd;structsockaddr_inservaddr,cliaddr;sockfd=socket(AF_INET,SOCK_DGRAM,0);/*createasoc
6、ket*//*initservaddr*/bzero(&servaddr,sizeof(servaddr));servaddr.sin_family=AF_INET;servaddr.sin_addr.s_addr=htonl(INADDR_ANY);servaddr.sin_port=htons(SERV_PORT);/*bindaddressandporttosocket*/if(bind(sockfd,(structsockaddr*)&servaddr,sizeof(servaddr))==-1){perror("binderror");exit(1);}do
7、_echo(sockfd,(structsockaddr*)&cliaddr,sizeof(cliaddr));return0;}2.UDPClient程序1、编写UDPClient程序的步骤otherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939servedasrecorderoftheWestNorthOrganiza
此文档下载收益归作者所有