使用Winsocket和C语言,进行SOCKET通讯编程,设计服务端和客户端程序,要求两端能够自由收发讯息
时间: 2024-02-04 21:01:45 浏览: 87
好的,这是一个经典的 Socket 编程问题,我可以为您提供一些基本的思路和代码示例。
首先,服务端和客户端都需要创建一个 Socket 对象,以便进行通信。在 C 语言中,可以使用 Winsock 库来实现 Socket 编程。以下是服务端和客户端的代码示例。
服务端代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
int main() {
// 初始化 Winsock 库
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// 创建 Socket 对象
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSocket == INVALID_SOCKET) {
printf("Error creating socket: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 绑定 Socket 对象到本地地址和端口
struct sockaddr_in localAddr;
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = INADDR_ANY;
localAddr.sin_port = htons(12345);
iResult = bind(listenSocket, (struct sockaddr*) &localAddr, sizeof(localAddr));
if (iResult == SOCKET_ERROR) {
printf("Bind failed: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 开始监听客户端连接请求
iResult = listen(listenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR) {
printf("Listen failed: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 等待客户端连接
printf("Waiting for client connection...\n");
SOCKET clientSocket = accept(listenSocket, NULL, NULL);
if (clientSocket == INVALID_SOCKET) {
printf("Accept failed: %d\n", WSAGetLastError());
closesocket(listenSocket);
WSACleanup();
return 1;
}
// 接收和发送数据
char recvBuf[1024];
char sendBuf[1024];
while (1) {
// 接收客户端发来的数据
iResult = recv(clientSocket, recvBuf, sizeof(recvBuf), 0);
if (iResult > 0) {
recvBuf[iResult] = '\0';
printf("Received: %s\n", recvBuf);
// 发送数据给客户端
sprintf(sendBuf, "Hello, %s!", recvBuf);
iResult = send(clientSocket, sendBuf, strlen(sendBuf), 0);
if (iResult == SOCKET_ERROR) {
printf("Send failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
} else if (iResult == 0) {
printf("Connection closed.\n");
break;
} else {
printf("Recv failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
// 关闭 Socket 对象
closesocket(clientSocket);
closesocket(listenSocket);
WSACleanup();
return 0;
}
```
客户端代码示例:
```
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
int main() {
// 初始化 Winsock 库
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
// 创建 Socket 对象
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (clientSocket == INVALID_SOCKET) {
printf("Error creating socket: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
// 连接到服务端
struct sockaddr_in serverAddr;
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
serverAddr.sin_port = htons(12345);
iResult = connect(clientSocket, (struct sockaddr*) &serverAddr, sizeof(serverAddr));
if (iResult == SOCKET_ERROR) {
printf("Connect failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
// 接收和发送数据
char recvBuf[1024];
char sendBuf[1024];
while (1) {
// 发送数据给服务端
printf("Enter message: ");
fgets(sendBuf, sizeof(sendBuf), stdin);
sendBuf[strlen(sendBuf)-1] = '\0';
iResult = send(clientSocket, sendBuf, strlen(sendBuf), 0);
if (iResult == SOCKET_ERROR) {
printf("Send failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
// 接收服务端发来的数据
iResult = recv(clientSocket, recvBuf, sizeof(recvBuf), 0);
if (iResult > 0) {
recvBuf[iResult] = '\0';
printf("Received: %s\n", recvBuf);
} else if (iResult == 0) {
printf("Connection closed.\n");
break;
} else {
printf("Recv failed: %d\n", WSAGetLastError());
closesocket(clientSocket);
WSACleanup();
return 1;
}
}
// 关闭 Socket 对象
closesocket(clientSocket);
WSACleanup();
return 0;
}
```
以上代码示例只是基本的实现,您可以根据自己的需要进行进一步的修改和扩展。
阅读全文