使用winsock和C语言,进行Socket通讯编程,设计多线程服务端和客户端,要求客户端能同时自由地接收和发送数据,请生成代码
时间: 2024-02-20 10:57:15 浏览: 103
好的,以下是一个基本的多线程服务端和客户端的代码示例,使用Winsock和C语言实现:
服务端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <process.h>
#pragma comment(lib,"ws2_32.lib") //链接ws2_32.lib库
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
//服务端线程函数
unsigned __stdcall handle_client(void* data);
int main(int argc, char* argv[])
{
WSADATA wsa;
SOCKET server_sock, client_sock;
struct sockaddr_in server, client;
int addr_len = sizeof(struct sockaddr_in);
unsigned int thread_id;
char buffer[BUFFER_SIZE];
//初始化Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("WSAStartup failed. Error Code : %d", WSAGetLastError());
return 1;
}
//创建服务端套接字
if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
return 1;
}
//设置服务器地址和端口
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(8888);
//绑定服务端套接字
if (bind(server_sock, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR)
{
printf("Bind failed with error code : %d", WSAGetLastError());
return 1;
}
//监听连接请求
if (listen(server_sock, MAX_CLIENTS) == SOCKET_ERROR)
{
printf("Listen failed with error code : %d", WSAGetLastError());
return 1;
}
printf("Waiting for incoming connections...\n");
//接收连接请求并创建新线程处理
while (1)
{
//接收连接请求
if ((client_sock = accept(server_sock, (struct sockaddr*)&client, &addr_len)) == INVALID_SOCKET)
{
printf("Accept failed with error code : %d", WSAGetLastError());
return 1;
}
printf("Client connected : %s\n", inet_ntoa(client.sin_addr));
//创建新线程处理
if (_beginthreadex(NULL, 0, handle_client, (void*)client_sock, 0, &thread_id) == 0)
{
printf("Create thread failed with error code : %d", WSAGetLastError());
return 1;
}
}
//关闭服务端套接字
closesocket(server_sock);
//释放Winsock
WSACleanup();
return 0;
}
//服务端线程函数
unsigned __stdcall handle_client(void* data)
{
SOCKET client_sock = (SOCKET)data;
char buffer[BUFFER_SIZE];
int recv_len;
while (1)
{
//接收客户端数据
memset(buffer, 0, BUFFER_SIZE);
recv_len = recv(client_sock, buffer, BUFFER_SIZE, 0);
if (recv_len == SOCKET_ERROR)
{
printf("Receive failed with error code : %d", WSAGetLastError());
break;
}
else if (recv_len == 0)
{
printf("Client disconnected\n");
break;
}
printf("Received : %s\n", buffer);
//发送数据回客户端
if (send(client_sock, buffer, recv_len, 0) == SOCKET_ERROR)
{
printf("Send failed with error code : %d", WSAGetLastError());
break;
}
}
//关闭客户端套接字
closesocket(client_sock);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <process.h>
#pragma comment(lib,"ws2_32.lib") //链接ws2_32.lib库
#define BUFFER_SIZE 1024
//客户端线程函数
unsigned __stdcall handle_server(void* data);
int main(int argc, char* argv[])
{
WSADATA wsa;
SOCKET sock;
struct sockaddr_in server;
unsigned int thread_id;
char buffer[BUFFER_SIZE];
int recv_len = 0;
//初始化Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
{
printf("WSAStartup failed. Error Code : %d", WSAGetLastError());
return 1;
}
//创建客户端套接字
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
printf("Could not create socket : %d", WSAGetLastError());
return 1;
}
//设置服务器地址和端口
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_port = htons(8888);
//连接服务器
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0)
{
printf("Connect failed with error code : %d", WSAGetLastError());
return 1;
}
printf("Connected to server : %s\n", inet_ntoa(server.sin_addr));
//创建新线程接收服务端数据
if (_beginthreadex(NULL, 0, handle_server, (void*)sock, 0, &thread_id) == 0)
{
printf("Create thread failed with error code : %d", WSAGetLastError());
return 1;
}
//发送数据到服务端
while (1)
{
memset(buffer, 0, BUFFER_SIZE);
fgets(buffer, BUFFER_SIZE, stdin);
send(sock, buffer, strlen(buffer), 0);
}
//关闭客户端套接字
closesocket(sock);
//释放Winsock
WSACleanup();
return 0;
}
//客户端线程函数
unsigned __stdcall handle_server(void* data)
{
SOCKET sock = (SOCKET)data;
char buffer[BUFFER_SIZE];
int recv_len;
while (1)
{
//接收服务端数据
memset(buffer, 0, BUFFER_SIZE);
recv_len = recv(sock, buffer, BUFFER_SIZE, 0);
if (recv_len == SOCKET_ERROR)
{
printf("Receive failed with error code : %d", WSAGetLastError());
break;
}
else if (recv_len == 0)
{
printf("Server disconnected\n");
break;
}
printf("Server : %s\n", buffer);
}
return 0;
}
```
注意:这只是一个基本的示例代码,仅供参考。实际使用时,需要根据具体需求进行修改和优化。同时,需要注意多线程编程可能会带来线程安全等问题,需要进行适当的处理。
阅读全文