#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <netinet/in.h>
#include <signal.h>
#include <string.h>
#include <arpa/inet.h>
#include <linux/poll.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#define BACKLOG 20
#define UDPSENDDESTPORT 4000
#define TCPSENDDESTPORT 5000
#define SENDBUFFERLEN 65536
#define RECVBUFFERLEN 65536
#define ERROR -1
extern int volatile g_stop;
int net_udp_sendto(char *ip_addr,unsigned int pack_cnt,unsigned int pack_size)
{
int i=0,count=1;
int nRet;
int dwUdpSendSd;
socklen_t addrlen,optval1,optval,optlen;
struct sockaddr_in strUdpSendaddr;
char *SendBuf;
if ((pack_size > SENDBUFFERLEN) || pack_cnt ==0 ||pack_size==0 )
{
return 1;
}
else
{
printf("The packet sendto =%s\n",ip_addr);
printf("The packet number =%d\n",pack_cnt);
printf("The packet size =%d\n",pack_size);
}
SendBuf = malloc(SENDBUFFERLEN);
for (i=0;i<SENDBUFFERLEN;i++)
{
SendBuf[i] = (i&0xff);
}
if ((dwUdpSendSd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("UdpUncastTest1 : Creat socket error\n");
free(SendBuf);
return 1;
}
addrlen = sizeof(struct sockaddr_in);
optlen = sizeof(socklen_t);
memset((void *)&strUdpSendaddr, 0, addrlen);
strUdpSendaddr.sin_family = AF_INET;
strUdpSendaddr.sin_addr.s_addr = inet_addr(ip_addr);
strUdpSendaddr.sin_port = htons(UDPSENDDESTPORT);
if (getsockopt(dwUdpSendSd, SOL_SOCKET, SO_SNDBUF, (void*)&optval1, &optlen))
{
close(dwUdpSendSd);
free(SendBuf);
return 1;
}
optval = 32768;//32768 16384 8192 4096 2048 1024
if (setsockopt(dwUdpSendSd, SOL_SOCKET, SO_SNDBUF, (const void*)&optval, optlen) < 0)
{
close(dwUdpSendSd);
free(SendBuf);
return 1;
}
if (getsockopt(dwUdpSendSd, SOL_SOCKET, SO_SNDBUF, (void *)&optval1, &optlen))
{
close(dwUdpSendSd);
free(SendBuf);
return 1;
}
//printf("The sendbuffer size =%d \n",optval);
printf("Send Begin ...\n");
do
{
*((unsigned int *)SendBuf) = count;
nRet = sendto(dwUdpSendSd, SendBuf, pack_size, 0, (struct sockaddr *)&strUdpSendaddr, addrlen);
if (nRet == ERROR)
{
printf("UdpSend Task 1 Error. %x\n",nRet);
break;
}
else if(nRet < pack_size)
{
printf("YUdpSend Error. %d\n",nRet);
break;
}
count++;
}while(count <= pack_cnt);
count--;
printf("Udp Send End !\n");
printf("Send packets number = %d\n",count);
close(dwUdpSendSd);
free(SendBuf);
return 0;
}
int net_udp_recvfrom(void)
{
int nRet;
int dwUdpRecvSd;
socklen_t addrlen;
struct sockaddr_in strUdpRecvaddr,strUdpSendaddr;
char *RecvBuf;
fd_set readfd;
RecvBuf = malloc(RECVBUFFERLEN);
if(RecvBuf)
{
memset(RecvBuf,0,RECVBUFFERLEN);
}
if ((dwUdpRecvSd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("UdpUncastTest1 : Creat socket error\n");
free(RecvBuf);
return 1;
}
addrlen = sizeof(struct sockaddr_in);
memset((void *)&strUdpRecvaddr, 0, addrlen);
strUdpRecvaddr.sin_family = AF_INET;
strUdpRecvaddr.sin_addr.s_addr = htonl(INADDR_ANY);
strUdpRecvaddr.sin_port = htons(UDPSENDDESTPORT);
bzero(&(strUdpRecvaddr.sin_zero), 8);
if(bind(dwUdpRecvSd,(struct sockaddr *)&strUdpRecvaddr,addrlen)<0)
{
printf("Bind Error:%s\n",strerror(errno));
return 1;
}
FD_ZERO(&readfd);
FD_SET(dwUdpRecvSd,&readfd);
while(1)
{
if(select(dwUdpRecvSd+1,&readfd,NULL,NULL,NULL) > 0)
{
memset((void *)&strUdpSendaddr, 0,addrlen);
nRet=recvfrom(dwUdpRecvSd,RecvBuf,RECVBUFFERLEN,0,(struct sockaddr*)&strUdpSendaddr,&addrlen);
if(nRet < 0)
{
printf("Recv date error:%s\n",strerror(errno));
break;
}
else
{
/*显示client端的网络地址*/
printf("The packet Recvfrom =%s\n",inet_ntoa(strUdpSendaddr.sin_addr));
/*显示客户端发来的字串*/
printf("The packet number =%d\n",(unsigned int)RecvBuf[0]);
printf("The packet size =%d\n",nRet);
}
}
if(g_stop)
{
break;
}
}
printf("Udp Recv End !\n");
close(dwUdpRecvSd);
free(RecvBuf);
return 0;
}
int net_tcp_send(char *ip_addr,unsigned int pack_cnt,unsigned int pack_size)
{
int i=0,count=1;
int nRet;
int dwTcpSendSd;
int addrlen;
struct sockaddr_in strTcpSendaddr,strTcpRevcaddr;
char *SendBuf;
if ((pack_size > SENDBUFFERLEN) || pack_cnt ==0 ||pack_size==0 )
{
return 1;
}
else
{
printf("The packet sendto =%s\n",ip_addr);
printf("The packet number =%d\n",pack_cnt);
printf("The packet size =%d\n",pack_size);
}
SendBuf = malloc(SENDBUFFERLEN);
for (i=0;i<SENDBUFFERLEN;i++)
{
SendBuf[i] = (i&0xff);
}
if ((dwTcpSendSd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("TcpUncastTest1 : Creat socket error\n");
free(SendBuf);
return 1;
}
addrlen = sizeof(struct sockaddr_in);
/* 初始化本地地址*/
memset((void *)&strTcpSendaddr, 0, addrlen);
strTcpSendaddr.sin_family=AF_INET;
strTcpSendaddr.sin_addr.s_addr=htonl(INADDR_ANY);
strTcpSendaddr.sin_port=0;
if(bind(dwTcpSendSd,(struct sockaddr*)&strTcpSendaddr,addrlen)<0)
{
/*棒定失败 */
printf("Bind Error:%s\n",strerror(errno));
close(dwTcpSendSd);
free(SendBuf);
return 1;
}
/* 初始化接收端地址*/
strTcpRevcaddr.sin_family = AF_INET;
strTcpRevcaddr.sin_addr.s_addr = inet_addr(ip_addr);
strTcpRevcaddr.sin_port = htons(TCPSENDDESTPORT);
if(connect(dwTcpSendSd,(struct sockaddr*)&strTcpRevcaddr,addrlen)!=0)/*请求连接*/
{
/*连接失败 */
printf("Connect Error:%s\n",strerror(errno));
close(dwTcpSendSd);
free(SendBuf);
return 1;
}
printf("Send Begin ...\n");
do
{
*((unsigned int *)SendBuf) = count;
nRet = send(dwTcpSendSd, SendBuf, pack_size, 0);
if (nRet == ERROR)
{
printf("TcpSend Task 1 Error. %x\n",nRet);
break;
}
else if(nRet < pack_size)
{
printf("TcpSend Error. %d\n",nRet);
break;
}
count++;
}while(count <= pack_cnt);
count--;
printf("Tcp Send End !\n");
printf("Send packets number = %d\n",count);
close(dwTcpSendSd);
free(SendBuf);
return 0;
}
int net_tcp_recv(void)
{
int nRet=0;
int dwTcpRecvSd,dwTcpSendSd;
socklen_t addrlen;
struct sockaddr_in strTcpRecvaddr,strTcpSendaddr;
char *RecvBuf;
fd_set readfd;
RecvBuf = malloc(RECVBUFFERLEN);
if(RecvBuf)
{
memset(RecvBuf,0,RECVBUFFERLEN);
}
if ((dwTcpRecvSd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("UdpUncastTest1 : Creat socket error\n");
free(RecvBuf);
return 1;
}
addrlen = sizeof(struct sockaddr_in);
/* 初始化本地地址*/
memset((void *)&strTcpRecvaddr, 0, addrlen);
strTcpRecvaddr.sin_family = AF_INET;
strTcpRecvaddr.sin_addr.s_addr = htonl(INADDR_ANY);
strTcpRecvaddr.sin_port = htons(TCPSENDDESTPORT);
bzero(&(strTcpRecvaddr.sin_zero), 8);
if(bind(dwTcpRecvSd,(struct sockaddr *)&strTcpRecvaddr,addrlen)<0)
{
printf("Bind Error:%s\n",strerror(errno));
goto end;
}
/*侦听客户端请求*/
if(listen(dwTcpRecvSd,BACKLOG)<0)
{
printf("Listen Error:%s\n",strerror(errno));
goto end;
}
FD_ZERO(&readfd);
FD_SET(dwTcpRecvSd,&readfd);
memset((void *)&strTcpSendaddr, 0,addrlen);
while(1)
{
if(select(dwTcpRecvSd+1,&readfd,NULL,NULL,NULL) > 0)
{
/* 等待接收客户连接请求*/
dwTcpSendSd=accept(dwTcpRecvSd,(struct sockaddr*) &strTcpSendaddr,&addrlen);
if(dwTcpSendSd<=0)
{
printf("Accept Error:%s\n",strerror(errno));
}
while(1)
{
/*开始服务*/
nRet = recv(dwTcpSendSd,RecvBuf,TCPSENDDESTPORT,0); /* 接受数据*/
if(nRet < 0)
{
printf("Recv date error:%s\n",strerror(errno));
break;
}