用C语言编写服务器端和客户端的代码实现windows系统的可一对一通信Socket编程
时间: 2024-03-17 16:41:37 浏览: 103
好的,我可以为您提供一个简单的C语言代码示例,用于实现Windows系统上的一对一通信Socket编程。
首先,您需要编写服务器端的代码。以下是一个简单的示例:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") // 链接ws2_32.lib库
int main() {
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
struct sockaddr_in serverAddr, clientAddr;
int clientAddrLen = sizeof(clientAddr);
char recvBuf[1024];
// 初始化Winsock库
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return -1;
}
// 创建服务器端socket
serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (serverSocket == INVALID_SOCKET) {
printf("Create server socket failed.\n");
WSACleanup();
return -1;
}
// 绑定服务器端socket到本地IP地址和端口号
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(serverSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("Bind server socket failed.\n");
closesocket(serverSocket);
WSACleanup();
return -1;
}
// 开始监听客户端连接请求
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR) {
printf("Listen server socket failed.\n");
closesocket(serverSocket);
WSACleanup();
return -1;
}
printf("Server is listening...\n");
// 接受客户端连接请求
clientSocket = accept(serverSocket, (struct sockaddr*)&clientAddr, &clientAddrLen);
if (clientSocket == INVALID_SOCKET) {
printf("Accept client socket failed.\n");
closesocket(serverSocket);
WSACleanup();
return -1;
}
printf("Client is connected: %s:%d\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
// 接收客户端发送的消息
while (1) {
memset(recvBuf, 0, sizeof(recvBuf));
int ret = recv(clientSocket, recvBuf, sizeof(recvBuf), 0);
if (ret > 0) {
printf("Received message from client: %s\n", recvBuf);
}
else if (ret == 0) {
printf("Client is disconnected.\n");
break;
}
else {
printf("Receive message from client failed.\n");
break;
}
}
// 关闭socket并清理Winsock库
closesocket(clientSocket);
closesocket(serverSocket);
WSACleanup();
return 0;
}
```
接下来是客户端的代码示例:
```c
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib") // 链接ws2_32.lib库
int main() {
WSADATA wsaData;
SOCKET clientSocket;
struct sockaddr_in serverAddr;
char sendBuf[1024];
// 初始化Winsock库
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed.\n");
return -1;
}
// 创建客户端socket
clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
printf("Create client socket failed.\n");
WSACleanup();
return -1;
}
// 连接服务器端socket
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if (connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("Connect server socket failed.\n");
closesocket(clientSocket);
WSACleanup();
return -1;
}
printf("Connected to server.\n");
// 发送消息到服务器端
while (1) {
printf("Please input message to send: ");
fgets(sendBuf, sizeof(sendBuf), stdin);
if (send(clientSocket, sendBuf, strlen(sendBuf), 0) == SOCKET_ERROR) {
printf("Send message to server failed.\n");
break;
}
}
// 关闭socket并清理Winsock库
closesocket(clientSocket);
WSACleanup();
return 0;
}
```
请注意,这只是一个简单的示例,您可能需要根据您的需求进行修改和扩展。
阅读全文