完成一对一的Socket客户端与服务器程序(85-90分) (1)输出每个步骤过程及必要信息,如:监听消息,ip是多少,端口是多少。如出错,则提示具体哪步出错及出错信息 (2)实现客户端的服务器的对话 (3)客户端输入quit后服务器关闭
时间: 2024-01-14 15:02:03 浏览: 61
以下是一对一的Socket客户端与服务器程序,具有85分-90分的基本功能:
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#define MAXBUFF 1024
#define PORT 9999
int main() {
WSADATA wsaData;
SOCKET serverSock, clientSock;
struct sockaddr_in serverAddr, clientAddr;
char recvBuff[MAXBUFF], sendBuff[MAXBUFF];
int clientAddrLen = sizeof(clientAddr);
int recvLen, sendLen;
// 初始化Winsock库
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed with error: %d\n", WSAGetLastError());
return 1;
}
// 创建服务器套接字
serverSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSock == INVALID_SOCKET) {
printf("socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 绑定服务器套接字到端口号
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddr.sin_port = htons(PORT);
if (bind(serverSock, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("bind failed with error: %d\n", WSAGetLastError());
closesocket(serverSock);
WSACleanup();
return 1;
}
// 开始监听客户端连接请求
if (listen(serverSock, SOMAXCONN) == SOCKET_ERROR) {
printf("listen failed with error: %d\n", WSAGetLastError());
closesocket(serverSock);
WSACleanup();
return 1;
}
printf("Server is listening on port %d...\n", PORT);
// 接受客户端连接请求
clientSock = accept(serverSock, (struct sockaddr*)&clientAddr, &clientAddrLen);
if (clientSock == INVALID_SOCKET) {
printf("accept failed with error: %d\n", WSAGetLastError());
closesocket(serverSock);
WSACleanup();
return 1;
}
printf("Client connected from %s:%d.\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
// 与客户端进行对话
while (1) {
// 接收客户端消息
memset(recvBuff, 0, MAXBUFF);
recvLen = recv(clientSock, recvBuff, MAXBUFF, 0);
if (recvLen == SOCKET_ERROR) {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(clientSock);
closesocket(serverSock);
WSACleanup();
return 1;
}
printf("Received message from client: %s\n", recvBuff);
// 判断是否退出
if (strcmp(recvBuff, "quit\n") == 0) {
printf("Client disconnected.\n");
break;
}
// 发送消息给客户端
printf("Enter message to send to client: ");
fgets(sendBuff, MAXBUFF, stdin);
sendLen = send(clientSock, sendBuff, strlen(sendBuff), 0);
if (sendLen == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(clientSock);
closesocket(serverSock);
WSACleanup();
return 1;
}
}
// 关闭套接字,清理Winsock库
closesocket(clientSock);
closesocket(serverSock);
WSACleanup();
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")
#define MAXBUFF 1024
#define PORT 9999
int main() {
WSADATA wsaData;
SOCKET clientSock;
struct sockaddr_in serverAddr;
char recvBuff[MAXBUFF], sendBuff[MAXBUFF];
int recvLen, sendLen;
// 初始化Winsock库
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed with error: %d\n", WSAGetLastError());
return 1;
}
// 创建客户端套接字
clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSock == INVALID_SOCKET) {
printf("socket failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 连接服务器端口号
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(PORT);
if (connect(clientSock, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("connect failed with error: %d\n", WSAGetLastError());
closesocket(clientSock);
WSACleanup();
return 1;
}
printf("Connected to server on port %d.\n", PORT);
// 与服务器进行对话
while (1) {
// 发送消息给服务器
printf("Enter message to send to server: ");
fgets(sendBuff, MAXBUFF, stdin);
sendLen = send(clientSock, sendBuff, strlen(sendBuff), 0);
if (sendLen == SOCKET_ERROR) {
printf("send failed with error: %d\n", WSAGetLastError());
closesocket(clientSock);
WSACleanup();
return 1;
}
// 接收服务器消息
memset(recvBuff, 0, MAXBUFF);
recvLen = recv(clientSock, recvBuff, MAXBUFF, 0);
if (recvLen == SOCKET_ERROR) {
printf("recv failed with error: %d\n", WSAGetLastError());
closesocket(clientSock);
WSACleanup();
return 1;
}
printf("Received message from server: %s\n", recvBuff);
// 判断是否退出
if (strcmp(sendBuff, "quit\n") == 0) {
printf("Closing connection.\n");
break;
}
}
// 关闭套接字,清理Winsock库
closesocket(clientSock);
WSACleanup();
return 0;
}
```
这段代码实现了基本的客户端和服务器端的对话,并支持客户端输入quit后服务器关闭的功能。客户端和服务器端都在本地运行,服务器监听端口号9999。
阅读全文