Socket网络编程 : 综合训练
第二章
题目1
5.试分别构造一个UNIX/Linux与Windows环境下循环UDP服务器例程,要求该服务器例程能够反复读取来自客户的任何请求,且一旦客户的请求中包含"time"字段,则该服务器例程将计算服务器的当前时间,并将该时间值作为响应返回给发送请求的客户。(发送close,服务器断开连接)
6.试分别构造一个UNIX/Linux与Windows环境下的UDP客户端例程,要求该客户端例程不但能够将本机时间发送给服务器,同时还能够将接收到的服务器回应的应答消息在本机显示器上进行回显。 (类似5)
win-udp客户端
#include<stdio.h> #include<winsock2.h> int main() { WSADATA wsData; if(WSAStartup(MAKEWORD(2,2),&wsData)!=0) { printf("初始化失败"); exit(-1); } SOCKET msock; msock=socket(AF_INET,SOCK_DGRAM,0); if(msock<0) { printf("创建失败"); exit(-1); } struct sockaddr_in client; client.sin_addr.s_addr=inet_addr("127.0.0.1"); client.sin_family=AF_INET; client.sin_port=htons(2333); #define MY_BUFSIZE 1024 char buf[MY_BUFSIZE]={}; ZeroMemory(buf,sizeof(buf)); scanf("%s",buf); if(sendto(msock,buf,sizeof(buf),0,(struct sockaddr*)&client,sizeof(client))<0) { printf("发送数据失败"); exit(-1); } struct sockaddr_in server; ZeroMemory(buf,strlen(buf)); int len=sizeof(server); if(recvfrom(msock,buf,sizeof(buf),0,(struct sockaddr*)&server,&len)<0) { printf("接收数据失败"); exit(-1); } printf("%s",buf); closesocket(msock); WSACleanup(); return 0; }
win-udp服务器
#include<stdio.h> #include<winsock2.h> #include<time.h> #pragma comment(lib,"ws2_32.lib") int main() { //初始化 WSADATA wsData; if(WSAStartup(MAKEWORD(2,2),&wsData)!=0) { printf("初始化失败"); exit(-1); } //创建socket SOCKET msock; msock=socket(AF_INET,SOCK_DGRAM,0); //绑定端口 struct sockaddr_in saddr; // sockaddr.sin_addr.s_addr=htol(INET_ANY); saddr.sin_addr.s_addr=htonl(INADDR_ANY); saddr.sin_family=AF_INET; saddr.sin_port=htons(2333); if(bind(msock,(struct sockaddr*)&saddr,sizeof(saddr))<0) { printf("端口绑定出错!"); exit(-1); } while(1) { #define MY_BUFFSIZE 1024 char bef[MY_BUFFSIZE]={}; ZeroMemory(bef,sizeof(bef)); struct sockaddr_in clientaddr; int len=sizeof(clientaddr); if(recvfrom(msock,bef,MY_BUFFSIZE,0,(struct sockaddr*)&clientaddr,&len)<0) { printf("接收数据失败"); exit(-1); } if(strcmp(bef,"time")==0) { ZeroMemory(bef,sizeof(bef)); struct tm *p; time_t timep; time(&timep); p =gmtime(&timep); sprintf(bef,"%04d-%02d-%02d %02d:%02d:%02d",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec); } else if(strcmp(bef,"close")==0) { char c[]="ok"; if(sendto(msock,c,strlen(c),0,(struct sockaddr*)&clientaddr,sizeof(clientaddr))<0) { printf("发送数据失败!"); exit(-1); } Sleep(0.5); break; } if(sendto(msock,bef,strlen(bef),0,(struct sockaddr*)&clientaddr,sizeof(clientaddr))<0) { printf("发送数据失败!"); exit(-1); } } closesocket(msock); WSACleanup(); return 0; }
linux-udp客户端
#include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> #include<time.h> int main() { int ssock=socket(AF_INET,SOCK_DGRAM,0); if(ssock<0) { printf("创建失败"); exit(-1); } struct sockaddr_in client; client.sin_addr.s_addr=inet_addr("127.0.0.1"); client.sin_family=AF_INET; client.sin_port=htons(23334); #define MY_BUFSIZE 1024 char buf[MY_BUFSIZE]={}; scanf("%s",buf); if(sendto(ssock,buf,sizeof(buf),0,(struct sockaddr*)&client,sizeof(client))<0) { printf("发送数据失败"); exit(-1); } struct sockaddr_in server; int len=sizeof(server); memset(buf,'\0',sizeof(buf)); if(recvfrom(ssock,buf,sizeof(buf),0,(struct sockaddr*)&server,&len)<0) { printf("接收数据失败"); exit(-1); } printf("%s",buf); close(); return 0; }
linux-udp服务器
#include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> #include<time.h> /*服务端*/ int main() { int ssock=socket(AF_INET,SOCK_DGRAM,0); if(ssock<0) { printf("创建失败"); exit(-1); } struct sockaddr_in client; client.sin_addr.s_addr=htonl(INADDR_ANY); client.sin_family=AF_INET; client.sin_port=htons(23334); if(bind(ssock,(struct sockaddr*)&client,sizeof(client))<0) { printf("绑定端口失败"); exit(-1); } while(1) { #define MY_BUFSIZE 1024 char buf[MY_BUFSIZE]={}; struct sockaddr_in server; int len=sizeof(server); memset(buf,'\0',sizeof(buf)); if(recvfrom(ssock,buf,sizeof(buf),0,(struct sockaddr*)&server,&len)<0) { printf("接收数据失败"); exit(-1); } if(strcmp(buf,"time")==0) { memset(buf,'\0',sizeof(buf)); struct tm *p; time_t timep; time(&timep); p =gmtime(&timep); sprintf(buf,"%04d-%02d-%02d %02d:%02d:%02d",1900+(p->tm_year),1+(p->tm_mon),p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec); } else if(strcmp(buf,"close")==0) { char c[]="ok"; if(sendto(ssock,c,strlen(c),0,(struct sockaddr*)&server,sizeof(server))<0) { printf("发送数据失败!"); exit(-1); } sleep(0.5); break; } if(sendto(ssock,buf,sizeof(buf),0,(struct sockaddr*)&server,sizeof(server))<0) { printf("发送数据失败"); exit(-1); } } close(); return 0; }
题目2
7.试分别构造一个UNIX/Linux与Windows环境下的循环TCP服务器例程,要求该服务器例程能够反复读取来自客户的任何连接请求,且一旦客户的请求中包含“time”字段,则该服务器例程将计算服务器的当前时间,并将该时间值作为响应返回给发送请求的客户。
8.试分别构造个UNIX/Linux 与Windows 环境下的TCP客户端例程,要求该客户端例程不但能够将本机时间发送给服务器,同时还能够将接收到的服务器回应的应答消息在本机显示器上进行回显。(类似7)
linux-tcp客户端
#include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> int main() { int ssock=socket(AF_INET,SOCK_STREAM,0); if(ssock<0) { printf("创建失败"); exit(-1); } struct sockaddr_in client; client.sin_addr.s_addr=inet_addr("127.0.0.1"); client.sin_family=AF_INET; client.sin_port=htons(2333); if(connect(ssock,(struct sockaddr*)&client,sizeof(client))<0) { printf("连接失败"); exit(-1); } #define MY_BUFSIZE 1024 char buf[MY_BUFSIZE]={}; memset(buf,'\0',sizeof(buf)); scanf("%s",buf); if(send(ssock,buf,strlen(buf),0)<0) { printf("发送数据失败"); exit(-1); } memset(buf,'\0',sizeof(buf)); if(recv(ssock,buf,sizeof(buf),0)<0) { printf("接收数据失败"); exit(-1); } printf("%s",buf); close(); return 0; }
linux-tcp服务器
#include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> #include<sys/types.h> #include<string.h> #include<time.h> /*服务端*/ int main() { int ssock=socket(AF_INET,SOCK_STREAM,0); if(ssock<0) { printf("创建失败"); exit(-1); } struct sockaddr_in client; client.sin_addr.s_addr=htonl(INADDR_ANY); client.sin_family=AF_INET; client.sin_port=htons(2333); if(bind(ssock,(struct sockaddr*)&client,sizeof(client))<0) { printf("绑定端口失败"); exit(-1); } if(listen(ssock,20)<0) { printf("监听失败"); } while(1) { struct sockaddr_in server; int len=sizeof(server); int new_client=accept(ssock,(struct sockeaddr*)&server,&len); if(new_client<0) { printf("接收连接失败"); } #define MY_BUFSIZE 1024 char buf[MY_BUFSIZE]={}; memset(buf,'\0',sizeof(buf)); if(recv(new_client,buf,sizeof(buf),0)<0) { printf("接收数据失败"); exit(-1); } if(strcmp(buf,"time")==0) { memset(buf,'\0',sizeof(buf)); struct tm *p; time_t timep; time(&timep); p =gmtime(&timep); sprintf(buf,"%04d-%02d-%02d %02d:%02d:%02d",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec); } else if(strcmp(buf,"close")==0) { char c[]="ok"; if(send(new_client,c,strlen(c),0)<0) { printf("发送数据失败!"); exit(-1); } sleep(0.5); break; } if(send(new_client,buf,sizeof(buf),0)<0) { printf("发送数据失败"); exit(-1); } close(new_client); } close(ssock); return 0; }
win-tcp客户端
#include<stdio.h> #include<winsock2.h> int main() { WSADATA wsData; if(WSAStartup(MAKEWORD(2,2),&wsData)!=0) { printf("初始化失败"); exit(-1); } SOCKET msock; msock=socket(AF_INET,SOCK_STREAM,0); if(msock<0) { printf("创建失败"); exit(-1); } struct sockaddr_in client; client.sin_addr.s_addr=inet_addr("127.0.0.1"); client.sin_family=AF_INET; client.sin_port=htons(2333); if(connect(msock,(struct sockaddr *)&client,sizeof(struct sockaddr))<0) { printf("建立连接失败"); exit(-1); } #define MY_BUFSIZE 1024 char buf[MY_BUFSIZE]={}; ZeroMemory(buf,sizeof(buf)); scanf("%s",buf); if(send(msock,buf,sizeof(buf),0)<0) { printf("发送数据失败"); exit(-1); } struct sockaddr_in server; ZeroMemory(buf,strlen(buf)); int len=sizeof(server); if(recv(msock,buf,sizeof(buf),0)<0) { printf("接收数据失败"); exit(-1); } printf("%s",buf); closesocket(msock); WSACleanup(); return 0; }
win-tcp服务器
#include<stdio.h> #include<winsock2.h> #include<time.h> #pragma comment(lib,"ws2_32.lib") int main() { //初始化 WSADATA wsData; if(WSAStartup(MAKEWORD(2,2),&wsData)!=0) { printf("初始化失败"); exit(-1); } //创建socket SOCKET msock; msock=socket(AF_INET,SOCK_STREAM,0); if(msock<0) { printf("创建失败"); } //绑定端口 struct sockaddr_in saddr; // sockaddr.sin_addr.s_addr=htol(INET_ANY); saddr.sin_addr.s_addr=htonl(INADDR_ANY); saddr.sin_family=AF_INET; saddr.sin_port=htons(2333); if(bind(msock,(struct sockaddr*)&saddr,sizeof(saddr))<0) { printf("端口绑定出错!"); exit(-1); } if(listen(msock,20)<0) { printf("监听失败"); exit(-1); } while(1) { #define MY_BUFFSIZE 1024 struct sockaddr_in clientaddr; int len=sizeof(clientaddr); SOCKET csock; csock=accept(msock,(struct sockaddr*)&clientaddr,&len); if(csock<0) { printf("接收失败"); exit(-1); } printf("%d",csock); char bef[MY_BUFFSIZE]={}; ZeroMemory(bef,sizeof(bef)); if(recv(csock,bef,MY_BUFFSIZE,0)<0) { printf(">>%s",bef); printf("接收数据失败!"); exit(-1); } if(strcmp(bef,"time")==0) { ZeroMemory(bef,sizeof(bef)); struct tm *p; time_t timep; time(&timep); p =gmtime(&timep); sprintf(bef,"%04d-%02d-%02d %02d:%02d:%02d",1900+p->tm_year,1+p->tm_mon,p->tm_mday,8+p->tm_hour,p->tm_min,p->tm_sec); } else if(strcmp(bef,"close")==0) { char c[]="ok"; if(sendto(msock,c,strlen(c),0,(struct sockaddr*)&clientaddr,sizeof(clientaddr))<0) { printf("发送数据失败!"); exit(-1); } Sleep(0.5); break; } if(send(csock,bef,strlen(bef),0)<0) { printf("发送数据失败!"); exit(-1); } closesocket(csock); } closesocket(msock); WSACleanup(); return 0; }
第三章
题目1
6.试分别构造一个UNIX/Linux 和Windows环境下的多进程的例程,该例程能实现以下功能:首先,创建1万2个新进程,然后再由每个新创建出的进程分别负责打印出100 1条Hello World字符串到控制台。
linux客户端
#include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> #include<time.h> /*客户端*/ int main() { int ssock=socket(AF_INET,SOCK_STREAM,0); if(ssock<0) { printf("创建失败"); exit(-1); } struct sockaddr_in client; client.sin_addr.s_addr=inet_addr("127.0.0.1"); client.sin_family=AF_INET; client.sin_port=htons(23333); if(connect(ssock,(struct sockaddr*)&client,sizeof(client))<0) { printf("建立连接失败"); exit(-1); } close(ssock); return 0; }
linux服务器
//服务器 #include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<sys/types.h> #include<unistd.h> #include<sys/wait.h> #include<string.h> #include<netinet/in.h> #include<arpa/inet.h> const int NUM_OF_PID=2; void myhandle(int msock) { struct sockaddr_in csock; memset(&csock,0,sizeof(csock)); int len=sizeof(csock); int cssock=accept(msock,(struct sockaddr*)&csock,&len); if(cssock<0) { printf("%ld:接受连接失败\n",getpid()); //exit(-1); } printf("\n"); printf("%ld:连接客户端是:%s:%d\n",getpid(),inet_ntoa(csock.sin_addr),ntohs(csock.sin_port)); printf("%d:say hello world!\n",getpid()); close(msock); } int main() { int ssock=socket(AF_INET,SOCK_STREAM,0); if(ssock<0) { printf("创建失败"); exit(-1); } struct sockaddr_in server; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_family=AF_INET; server.sin_port=htons(23333); if(bind(ssock,(struct sockaddr*)&server,sizeof(server))<0) { printf("绑定失败"); exit(-1); } //在父进程里listen if(listen(ssock,20)<0) { printf("监听失败"); exit(-1); } signal(SIGCLD,SIG_IGN); #define MAX 30 pid_t pid[MAX]={}; for(int i=0;i<NUM_OF_PID;++i) { pid[i]=fork(); if(pid[i]==0) { myhandle(ssock); } } close(ssock); return 0; }
题目2
7.试分别构造一个UNIX/Linux和Windows环境下的多线程的例程,该例程能实现以下功能:首先, 创建1万个新线程,然后再由每个新创建出的线程分别负责打印出100条Hello World字符串到控制台。
linux客户端
#include<stdio.h> #include<sys/socket.h> #include<netinet/in.h> #include<stdlib.h> #include<string.h> #include<time.h> /*客户端*/ int main() { int ssock=socket(AF_INET,SOCK_STREAM,0); if(ssock<0) { printf("创建失败"); exit(-1); } struct sockaddr_in client; client.sin_addr.s_addr=inet_addr("127.0.0.1"); client.sin_family=AF_INET; client.sin_port=htons(23333); if(connect(ssock,(struct sockaddr*)&client,sizeof(client))<0) { printf("建立连接失败"); exit(-1); } close(ssock); return 0; }
linux服务器
//服务器 #include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<pthread.h> #include<netinet/in.h> #include<arpa/inet.h> #include<sys/types.h> const int NUM_OF_PID=2; void *handle(void *arg) { struct sockaddr_in *msock; msock = (struct sockaddr_in *)arg; printf("连接客户端是:%s:%d\n",inet_ntoa(msock->sin_addr),ntohs(msock->sin_port)); printf("%d:say hello world!\n",pthread_self()); pthread_exit(NULL); } int main() { int ssock=socket(AF_INET,SOCK_STREAM,0); if(ssock<0) { printf("创建失败"); exit(-1); } struct sockaddr_in server; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_family=AF_INET; server.sin_port=htons(23333); if(bind(ssock,(struct sockaddr*)&server,sizeof(server))<0) { printf("绑定失败"); exit(-1); } if(listen(ssock,20)<0) { printf("监听失败"); exit(-1); } #define MAX 30 pthread_t pid; pthread_attr_t pattr; pthread_attr_init(&pattr); pthread_attr_setdetachstate(&pattr,PTHREAD_CREATE_DETACHED); while(1) { struct sockaddr_in csock; int len=sizeof(csock); int cssock=accept(ssock,(struct sockaddr*)&csock,&len); if(cssock<0) { printf("接受连接失败"); exit(-1); } pthread_create(&pid,&pattr,(void *)handle,(void *)&csock); } pthread_attr_destroy(&pattr); close(ssock); return 0; }