分类: LINUX
2013-06-17 18:40:00
客户端程序
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[]){
char buf[65536];
memset(buf,0,65536);
int sockfd;
int fd_server;
int maxsockfd;
int nread = 0;
int yes = 1;
fd_set fset;
struct sockaddr_in server;//两种常见地址
struct sockaddr addr;
int len = sizeof(struct sockaddr);
/*
server.sin_port = htons(12345);
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl("inet_addr("10.1.1.1")");
*/
struct timeval timeout;
sockfd = socket(AF_INET,SOCK_DGRAM, 0);
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));//i常用设置
server.sin_port = htons(12345);
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(inet_addr("10.1.1.1"));//inet_addr的使用,htonl htons的使用
if(bind(sockfd, (struct sockaddr*)&server, sizeof(struct sockaddr))<0)
{
printf("error:%s\n",strerror(errno));
}
FD_ZERO(&fset);
FD_SET(sockfd,&fset);
timeout.tv_sec = 100;
timeout.tv_usec = 0;
maxsockfd = sockfd + 1;
while(select(maxsockfd,&fset,NULL,NULL,&timeout)< 0){
printf("errno = %d,error:%s\n",errno,strerror(errno));
exit(1);
}
if(FD_ISSET(sockfd,&fset)){
nread = recvfrom(sockfd,buf,65507,0,&addr,&len);//recvfrom 参数 6个,第四个flag与第六个len尤其第六个要尤其主意
printf("nead = %d\n",nread);
}
fd_server = open("/mnt/server.log",O_RDWR|O_CREAT|O_APPEND);//mode O_RDWD O_CREAT O_APPEND需要注意所在头文件fcntl sys/stat
if(fd_server < 0){
printf("errno = %d,error:%s\n",errno,strerror(errno));
exit(1);
}
if((nread = write(fd_server,buf,strlen((char*)buf)))<0){
printf("errno = %d,error:%s\n",errno,strerror(errno));
exit(1);
}
printf("nread = %d\n",nread);
close(fd_server);
close(sockfd);
}
客户端程序:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[]){
char buf[65536];
int sockfd;
int fd_client;
int maxsockfd;
int nread = 0;
int yes = 1;
fd_set fset;
struct sockaddr_in client;
// struct sockaddr addr;
struct sockaddr_in server;
server.sin_port = htons(12345);
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(inet_addr("10.1.1.1"));
struct timeval timeout;
fd_client = open("/mnt/client.log",O_RDWR|O_CREAT|O_APPEND);
if(fd_client < 0){
printf("errno = %d,error:%s\n",errno,strerror(errno));
exit(1);
}
if((nread = read(fd_client,buf,65535))<0){
printf("errno = %d,error:%s\n",errno,strerror(errno));
exit(1);
}
printf("nread = %d\n",nread);
sockfd = socket(AF_INET,SOCK_DGRAM, 0);
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
client.sin_port = htons(12345);
client.sin_family = AF_INET;
client.sin_addr.s_addr = htonl(INADDR_ANY);
bind(sockfd, (struct sockaddr*)&client, sizeof(&client));
FD_ZERO(&fset);
FD_SET(sockfd,&fset);
timeout.tv_sec = 2;
timeout.tv_usec = 0;
maxsockfd = sockfd + 1;
while(select(maxsockfd,NULL,&fset,NULL,&timeout)< 0){
printf("errno = %d,error:%s\n",errno,strerror(errno));
exit(1);
}
if(FD_ISSET(sockfd,&fset)){
nread = sendto(sockfd,buf,65507,0,(struct sockaddr*)&server,sizeof(struct sockaddr));//sendto 与recvfrom在参数上的最大不同在于第六个参数,不是以地址的形式出现
printf("nead = %d\n",nread);
}
close(fd_client);
close(sockfd);
}
本程序设计主要是为了测试sendto最多可发送字节长度:UDP报文的最大长是65535,UDP头长8个字节,IP头长20个字节
故sendto最长可发送长度为65507个字节