用C语言完成适用于Windows系统的一对一的Socket客户端与服务器程序,并满足一下要求: (1)输出每个步骤过程及必要信息,如:监听消息,ip是多少,端口是多少。如出错,则提示具体哪步出错及出错信息 (2)实现客户端的服务器的对话 (3)客户端输入quit后服务器关闭
时间: 2024-03-06 16:51:29 浏览: 80
C语言编写基于TCP和UDP协议的Socket通信程序示例
好的,我了解您的问题。首先,我需要明确一下您的问题,您希望我为您提供一份C语言完成适用于Windows系统的一对一的Socket客户端与服务器程序的代码,这份代码需要满足以下要求:
1. 输出每个步骤过程及必要信息,如:监听消息,ip是多少,端口是多少。如出错,则提示具体哪步出错及出错信息。
2. 实现客户端的服务器的对话。
3. 客户端输入quit后服务器关闭。
是这样吗?
如果是的话,那么我可以为您提供以下的代码实现。请注意,这只是一份示例代码,您需要根据自己的实际情况进行修改和适配。
首先是服务器端的代码:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
int clientAddrLen = sizeof(clientAddr);
char buffer[1024];
int nret;
// 初始化Socket库
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup failed!\n");
return -1;
}
// 创建Socket
serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET)
{
printf("Failed to create socket! Error code: %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
// 绑定Socket
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(12345);
serverAddr.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
printf("Failed to bind socket! Error code: %d\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
return -1;
}
// 监听Socket
if (listen(serverSocket, 1) == SOCKET_ERROR)
{
printf("Failed to listen socket! Error code: %d\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
return -1;
}
printf("Server is listening on port 12345...\n");
// 接收客户端连接
clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
if (clientSocket == INVALID_SOCKET)
{
printf("Failed to accept client! Error code: %d\n", WSAGetLastError());
closesocket(serverSocket);
WSACleanup();
return -1;
}
printf("Client connected, IP:%s, Port:%d\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
// 循环处理客户端请求
while (1)
{
// 接收客户端消息
nret = recv(clientSocket, buffer, sizeof(buffer), 0);
if (nret == SOCKET_ERROR)
{
printf("Failed to receive data from client! Error code: %d\n", WSAGetLastError());
break;
}
else if (nret == 0)
{
printf("Client closed the connection.\n");
break;
}
printf("Received message: %s\n", buffer);
// 发送回复消息
nret = send(clientSocket, buffer, nret, 0);
if (nret == SOCKET_ERROR)
{
printf("Failed to send data to client! Error code: %d\n", WSAGetLastError());
break;
}
}
// 关闭Socket
closesocket(clientSocket);
closesocket(serverSocket);
// 释放Socket库
WSACleanup();
return 0;
}
```
然后是客户端的代码:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
int main()
{
WSADATA wsaData;
SOCKET clientSocket;
struct sockaddr_in serverAddr;
char buffer[1024];
int nret;
// 初始化Socket库
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup failed!\n");
return -1;
}
// 创建Socket
clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == INVALID_SOCKET)
{
printf("Failed to create socket! Error code: %d\n", WSAGetLastError());
WSACleanup();
return -1;
}
// 连接服务器
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(12345);
serverAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
if (connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR)
{
printf("Failed to connect to server! Error code: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return -1;
}
printf("Connected to server.\n");
// 循环发送消息
while (1)
{
printf("Please input message (input quit to exit): ");
fgets(buffer, sizeof(buffer), stdin);
// 输入quit则退出
if (strcmp(buffer, "quit\n") == 0)
{
break;
}
// 发送消息
nret = send(clientSocket, buffer, strlen(buffer), 0);
if (nret == SOCKET_ERROR)
{
printf("Failed to send data to server! Error code: %d\n", WSAGetLastError());
break;
}
// 接收回复消息
nret = recv(clientSocket, buffer, sizeof(buffer), 0);
if (nret == SOCKET_ERROR)
{
printf("Failed to receive data from server! Error code: %d\n", WSAGetLastError());
break;
}
else if (nret == 0)
{
printf("Server closed the connection.\n");
break;
}
printf("Received message: %s\n", buffer);
}
// 关闭Socket
closesocket(clientSocket);
// 释放Socket库
WSACleanup();
return 0;
}
```
希望这份代码可以对您有所帮助。如果您有其他问题,请随时提出。
阅读全文