分类: LINUX
2013-03-02 14:08:32
作者:tingkman@163.com
博客:tingkman.blog.chinaunix.net
本文的部分内容可能来源于网络,该内容归原作者所有,如果侵犯到您的权益,请及时通知我,我将立即删除,原创内容copyleft归所有,使用GPL发布,可以自由拷贝,转载。但转载请保持文档的完整性,注明原作者及原链接,严禁用于任何商业用途。欢迎加入嵌入式交流群:3462094
=====================================================================================
由于项目需要,需要做一个udp通讯测试,主要用于测试系统的网络处理性能,将udp网络流量加到70M,通过接收并发送udp数据包,查看是否有丢包现象,内核是否被挂,
来判断硬件的网络的处理性能。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
udp 客户端文件client1.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERV_PORT 15811
#define MAXLINE 100
int main(int argc, char **argv)
{
int sockfd,so_broadcast;
struct sockaddr_in servaddr;
if( argc!=2 )
perror("usage:udpclient");
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
char sendline[MAXLINE];
char recvline[MAXLINE];
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
//?O??1?2¥ê?D?
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &so_broadcast, sizeof(so_broadcast)))
{
perror("setsockopt");
return ;
}
if (sockfd == -1)
{
perror("socket");
return ;
}
while(fgets(sendline, MAXLINE,stdin)!=NULL)
{
//????·¢?íμ?IP£??éò?è?192.168.18.255£-£-?à2¥£?£?ò???á?·?·????é
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
//servaddr.sin_addr.s_addr = inet_addr(argv[1]);
sendto(sockfd,sendline,strlen(sendline),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
memset(recvline,0,sizeof(recvline));
int n=recvfrom(sockfd,recvline,MAXLINE,0,NULL,NULL);
if(n>0)
{
printf("\nwe receive date is ");
fputs(recvline,stdout);
}
}
exit(0);
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
udp 服务端文件server1.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define SERV_PORT 15811
#define MAXLINE 100
void dg_echo(int sockfd, struct sockaddr *pcliaddr, socklen_t clilen)
{
int n;
socklen_t len;
char mesg[MAXLINE];
for(;;)
{
len = clilen;
memset(mesg,0,sizeof(mesg));
n=recvfrom(sockfd,mesg,MAXLINE,0,pcliaddr,&len);
printf("Recv:%s\n",mesg);
sendto(sockfd,mesg,n,0,pcliaddr,len);
printf("and we send:%s to back\n",mesg);
}
}
int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr,cliaddr;
sockfd=socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);
bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
dg_echo(sockfd, (struct sockaddr *)&cliaddr,sizeof(cliaddr));
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------
编译程序:
gcc server1.c -o server1
gcc client1.c -o client1
打开两个终端运行程序
./server1 ./client1 127.0.0.1
在client端输入要发送的内容 tingkman
服务端打印:
Recv:tingkman
and we send:tingkman
to back
Recv:fanjin
and we send:fanjin
to back
客户端打印:
we receive date is tingkman--------------------------------------------------------------------------------------------------------------------------------------------------------------------