/* *客户端程序 */ #include <winsock2.h> #include <stdio.h> #include "string.h" #pragma comment(lib,"ws2_32.lib") //把ws2_32.lib加到Link页的连接库 #define IP "127.0.0.1" //通信的IP地址 #define PORT 15501 //通信的端口 char *buf; #define N 20 struct student { char name[N]; char sex[N]; int age; char number[N]; char classes[N]; char major[N]; }student; void message() { int m ; struct student *p ; p=(struct student *)malloc(sizeof(struct student)); while(1) { printf("Please input the message: /n"); printf("请输入学生姓名:"); scanf("%s",p->name); printf("请输入性别:"); scanf("%s",p->sex); printf("请输入年龄:"); scanf("%d",&p->age); printf("请输入学号:"); scanf("%s",p->number); printf("请输入班级:"); scanf("%s",p->classes); printf("请输入专业 :"); scanf("%s",p->major); buf = (char *)p; m = getchar(); if(m == 10) { break; } } } void send() { WSADATA WSAData; //加载套接字库 if(WSAStartup(MAKEWORD(2,0)/*指定加载的Winsock库的版本,2 表示主版本号;0 表示副版本号*/,&WSAData /*返回值,指向WSADATA结构的指针*/)==SOCKET_ERROR) { printf("Socket initialize fail!/n"); return; } SOCKET sock; if((sock=socket(AF_INET/*指定地址族*/,SOCK_STREAM/*制定Socket类型为产生流式套接字*/,0))==SOCKET_ERROR) { printf("Socket create fail!/n"); WSACleanup(); return; } struct sockaddr_in ClientAddr; ClientAddr.sin_family=AF_INET; ClientAddr.sin_port=htons(PORT); ClientAddr.sin_addr.s_addr=inet_addr(IP); if(connect(sock,(LPSOCKADDR)&ClientAddr/*设定连接的服务器端的地址信息*/,sizeof(ClientAddr))==SOCKET_ERROR) { //进行服务器连接 printf("Connect fail!/n"); closesocket(sock); WSACleanup(); return; } send(sock/*已经建立连接的套接字*/,buf,2048,0); //向服务器发送数据 closesocket(sock); WSACleanup();//释放应用程序加载套接字库分配的资源,终止对WinSock动态库的使用 } void main() { while(1) { message(); send(); if(buf[0]=='e' && buf[1]=='n' && buf[2]=='d') { printf("Program end./n"); return; } } }
/* *服务器端程序 */ #include <stdio.h> #include <winsock2.h> #include <windows.h> #include <malloc.h> #pragma comment(lib,"ws2_32.lib") //把ws2_32.lib加到Link页的连接库 #define IP "127.0.0.1" //通信的IP地址 #define PORT 15501 //通信的端口 #define ERROR 0 #define N 20 struct student { char name[N]; char sex[N]; int age; char number[N]; char classes[N]; char major[N]; }student; void main() { WSADATA WSAData; if(WSAStartup(MAKEWORD(2,0),&WSAData)==SOCKET_ERROR) { //启动winsock printf("Socket initialize fail!/n"); exit(1); } SOCKET sock; if((sock=socket(AF_INET,SOCK_STREAM,0))==ERROR) { //建立套接字 printf("Socket create!/n"); WSACleanup(); exit(1); } struct sockaddr_in ServerAddr; ServerAddr.sin_family=AF_INET; ServerAddr.sin_port=htons(PORT); ServerAddr.sin_addr.s_addr=INADDR_ANY; if(bind(sock,(LPSOCKADDR)&ServerAddr,sizeof(ServerAddr))==SOCKET_ERROR) { //对端口进行监听 printf("Bind fail!/n"); closesocket(sock); WSACleanup(); exit(1); } printf("Socket port #%d/n",ntohs(ServerAddr.sin_port)); if(listen(sock,10)==SOCKET_ERROR) { //设置最大不被拒绝的请求数 printf("Listen fail!/n"); closesocket(sock); WSACleanup(); exit(1); } SOCKET msgsock; char buf[2048]; struct student *p; p=(struct student *)malloc(sizeof(struct student));//申请一个动态存储区域,它的大小为sizeof(struct book) while(1) { if((msgsock=accept(sock,(LPSOCKADDR)0,(int *)0))==INVALID_SOCKET) { //接受客户端连接 printf("Accept fail!/n"); continue; } printf("Have client connected./n"); memset(buf,0,sizeof(buf)); recv(msgsock,buf,2048,0); //接收客户端数据 if(buf[0]=='e' && buf[1]=='n' && buf[2]=='d') { printf("Program end./n"); return; } p = (struct student *)buf; printf("name = %s/n",p->name); printf("sex = %s/n",p->sex); printf("age = %d/n",p->age); printf("nuber = %s/n",p->number); printf("classes = %s/n",p->classes); printf("major = %s/n",p->major); closesocket(msgsock); } closesocket(sock); //关闭套接字 WSACleanup(); //关闭winsock }