请给出相应实现的c代码,用1多线程实现
时间: 2024-02-24 15:56:51 浏览: 44
C多线程程序实现
下面是使用C语言实现客户端与服务端的双向通信的代码,使用了多线程实现:
服务端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#define PORT 8888
#define MAX_MSG_LENGTH 1024
void *client_handler(void *arg);
int main(int argc, char const *argv[])
{
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len = sizeof(client_addr);
pthread_t tid;
// 创建套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(EXIT_FAILURE);
}
// 设置服务器地址
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// 绑定套接字到服务器地址
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
{
perror("bind");
exit(EXIT_FAILURE);
}
// 监听客户端连接请求
if (listen(server_fd, 5) == -1)
{
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server started, waiting for connections...\n");
while (1)
{
// 接收客户端连接请求
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len)) == -1)
{
perror("accept");
continue;
}
printf("New connection from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// 创建线程处理客户端请求
if (pthread_create(&tid, NULL, client_handler, (void *)&client_fd) != 0)
{
perror("pthread_create");
close(client_fd);
continue;
}
}
return 0;
}
void *client_handler(void *arg)
{
int client_fd = *(int *)arg;
char buf[MAX_MSG_LENGTH];
ssize_t n;
while (1)
{
// 接收客户端消息
if ((n = recv(client_fd, buf, MAX_MSG_LENGTH, 0)) == -1)
{
perror("recv");
break;
}
else if (n == 0)
{
printf("Connection closed by client\n");
break;
}
// 处理客户端消息
buf[n] = '\0';
printf("Received message from client: %s\n", buf);
// 发送响应消息给客户端
if (send(client_fd, buf, n, 0) == -1)
{
perror("send");
break;
}
}
close(client_fd);
pthread_exit(NULL);
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define SERVER_ADDR "127.0.0.1"
#define PORT 8888
#define MAX_MSG_LENGTH 1024
int main(int argc, char const *argv[])
{
int client_fd;
struct sockaddr_in server_addr;
char buf[MAX_MSG_LENGTH];
ssize_t n;
// 创建套接字
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(EXIT_FAILURE);
}
// 设置服务器地址
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = inet_addr(SERVER_ADDR);
// 连接服务器
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
{
perror("connect");
exit(EXIT_FAILURE);
}
printf("Connected to server %s:%d\n", SERVER_ADDR, PORT);
while (1)
{
// 读取用户输入
printf("Enter message to send: ");
fgets(buf, MAX_MSG_LENGTH, stdin);
// 发送消息到服务器
if (send(client_fd, buf, strlen(buf), 0) == -1)
{
perror("send");
break;
}
// 接收服务器响应
if ((n = recv(client_fd, buf, MAX_MSG_LENGTH, 0)) == -1)
{
perror("recv");
break;
}
else if (n == 0)
{
printf("Connection closed by server\n");
break;
}
// 处理服务器响应
buf[n] = '\0';
printf("Received message from server: %s\n", buf);
}
close(client_fd);
return 0;
}
```
在上面的代码中,服务端使用了一个无限循环接收客户端连接请求,并为每个客户端连接创建一个新的线程处理客户端请求。客户端则是读取用户输入,发送消息到服务器,并接收服务器的响应消息。
阅读全文