实验目的:
掌握Winsock2.0套接字编程技术的基本方法。
实验要求:
运用TCP/IP Winsock2.0套接字编程技术,使用VC编写一个面向连接通信的服务端程序与客户端程序,服务器先与端口3000绑定,
然后等待客户连接,连接成功后就可与客户端互相收、发信息。交互中,通信任一方只要发送 bye, 通信过程便自动结束。
服务端:
#include <conio.h> #include <stdio.h> #include <winsock2.h> #pragma comment(lib, "Ws2_32.lib") int main(int, char **) { struct sockaddr addr; int recvLen, errCode; char msg[255] = {0}; SOCKET sock, sersock; WORD wVersionRequested; WSADATA wsaData; LPSTR messageBuffer = NULL; SetConsoleTitle("服务端"); wVersionRequested = MAKEWORD(2, 0); errCode = WSAStartup(wVersionRequested, &wsaData); if (errCode) { printf("没有Windows Socket动态库! "); return WSAGetLastError(); } if (LOBYTE(wsaData.wVersion) != 2 || //不是Winsock 2.0 HIBYTE(wsaData.wVersion) != 0) { printf("需要Windows Socket 2! "); WSACleanup(); return WSAGetLastError(); } sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { printf("不能创建Socket! "); getch(); WSACleanup(); return 0; } ((sockaddr_in *)&addr)->sin_family = AF_INET; ((sockaddr_in *)&addr)->sin_port = htons(3000); ((sockaddr_in *)&addr)->sin_addr.s_addr = inet_addr("127.0.0.1"); bind(sock, &addr, sizeof(addr)); listen(sock, 1); printf("等待客户连接! "); recvLen = sizeof(addr); sersock = accept(sock, &addr, &recvLen); if (sersock == INVALID_SOCKET) { errCode = WSAGetLastError(); size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL); printf("%s ", messageBuffer); free(messageBuffer); getch(); closesocket(sock); WSACleanup(); return 0; } printf("有客户连接! 输入bye通信结束 "); do { printf(" 等待客户端输入信息......"); recvLen = recv(sersock, msg, 200, 0); printf(" 客户端:%s", msg); if (!strcmp(msg, "bye")) break; printf(" 服务器:"); gets(msg); send(sersock, msg, strlen(msg) + 1, 0); } while (strcmp(msg, "bye")); closesocket(sersock); closesocket(sock); printf(" 会话结束,按任一键返回 "); getch(); WSACleanup(); return 0; }
客户端:
#include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h> #pragma comment(lib, "Ws2_32.lib") int main(int, char **) { SOCKET sock; struct sockaddr addr; int errCode; char msg[255] = {0}; WORD wVersionRequested; WSADATA wsaData; SetConsoleTitle("客户端"); wVersionRequested = MAKEWORD(2, 0); errCode = WSAStartup(wVersionRequested, &wsaData); if (errCode) { printf("没有Windows Socket动态库! "); return WSAGetLastError(); } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 0) { printf("需要Windows Socket 2! "); WSACleanup(); return WSAGetLastError(); } sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == INVALID_SOCKET) { printf("不能创建Socket! "); getch(); WSACleanup(); return WSAGetLastError(); } ((sockaddr_in *)&addr)->sin_family = AF_INET; ((sockaddr_in *)&addr)->sin_addr.s_addr = inet_addr("127.0.0.1"); ((sockaddr_in *)&addr)->sin_port = htons(3000); printf("与服务器连接...! "); errCode = connect(sock, (sockaddr *)&addr, sizeof(addr)); if (errCode == SOCKET_ERROR) { printf("连接失败! "); getch(); WSACleanup(); return 0; } printf("成功连接到服务器127.0.0.1! 若输入bye则通信结束 "); do { printf(" 客户端:"); gets(msg); send(sock, msg, strlen(msg) + 1, 0); if (!strcmp(msg, "bye")) break; printf("等待服务器响应...... "); recv(sock, msg, 200, 0); printf("服务器:%s", msg); } while (strcmp(msg, "bye")); closesocket(sock); printf(" 会话结束,按任一键返回 "); getch(); WSACleanup(); return 0; }
运行效果:
此为学校实验题目:word排版请用http://www.planetb.ca/syntax-highlight-word
Winsock2.0相关参考网站,拥有大量示例代码:https://www.tenouk.com/