c语言实现使用 socket API 来实现一个并发回应服务器与客户端的设计。
时间: 2023-07-15 08:14:55 浏览: 87
这里提供一个简单的示例代码,实现了一个基于 TCP 协议的并发回应服务器和客户端之间的通信。代码使用了 Linux 系统下的 socket API。
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int client_sockets[MAX_CLIENTS] = {0};
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *client_handler(void *arg) {
int client_socket = *(int*)arg;
char buffer[BUFFER_SIZE] = {0};
while (1) {
memset(buffer, 0, BUFFER_SIZE);
int read_size = read(client_socket, buffer, BUFFER_SIZE);
if (read_size == 0) {
printf("Client disconnected: %d\n", client_socket);
break;
}
printf("Received from client %d: %s\n", client_socket, buffer);
pthread_mutex_lock(&mutex);
for (int i = 0; i < MAX_CLIENTS; i++) {
if (client_sockets[i] != 0 && client_sockets[i] != client_socket) {
write(client_sockets[i], buffer, strlen(buffer));
}
}
pthread_mutex_unlock(&mutex);
}
close(client_socket);
pthread_exit(NULL);
}
int main(int argc, char const *argv[]) {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
pthread_t thread_id;
int opt = 1;
memset(client_sockets, 0, sizeof(client_sockets));
// Create server socket
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Set server socket options
if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt))) {
perror("Setsockopt failed");
exit(EXIT_FAILURE);
}
// Bind server socket to given port
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(atoi(argv[1]));
if (bind(server_socket, (struct sockaddr *)&server_addr,
sizeof(server_addr)) < 0) {
perror("Bind failed");
exit(EXIT_FAILURE);
}
// Listen for incoming connections
if (listen(server_socket, MAX_CLIENTS) < 0) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
printf("Server started.\n");
while (1) {
socklen_t client_addr_size = sizeof(client_addr);
if ((client_socket = accept(server_socket,
(struct sockaddr *)&client_addr,
&client_addr_size)) < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
pthread_mutex_lock(&mutex);
for (int i = 0; i < MAX_CLIENTS; i++) {
if (client_sockets[i] == 0) {
client_sockets[i] = client_socket;
printf("New client connected: %d\n", client_socket);
pthread_create(&thread_id, NULL, client_handler,
(void*)&client_socket);
break;
}
}
pthread_mutex_unlock(&mutex);
if (client_socket < 0) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
}
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BUFFER_SIZE 1024
int main(int argc, char const *argv[]) {
int client_socket;
struct sockaddr_in server_addr;
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
if (inet_pton(AF_INET, argv[1], &server_addr.sin_addr) <= 0) {
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
if (connect(client_socket, (struct sockaddr *)&server_addr,
sizeof(server_addr)) < 0) {
perror("Connection failed");
exit(EXIT_FAILURE);
}
printf("Connected to server.\n");
char buffer[BUFFER_SIZE] = {0};
while (1) {
memset(buffer, 0, BUFFER_SIZE);
printf("Enter message: ");
fgets(buffer, BUFFER_SIZE, stdin);
write(client_socket, buffer, strlen(buffer));
memset(buffer, 0, BUFFER_SIZE);
int read_size = read(client_socket, buffer, BUFFER_SIZE);
if (read_size == 0) {
printf("Server disconnected.\n");
break;
}
printf("Received from server: %s", buffer);
}
close(client_socket);
return 0;
}
```
在终端上运行服务器端程序:
```
$ ./server 8888
```
然后在另一个终端上运行客户端程序:
```
$ ./client 127.0.0.1 8888
```
客户端程序会连接服务器,并且等待用户输入消息。当用户输入消息并按下回车键时,消息会发送给服务器,服务器会将消息广播给所有已连接的客户端。客户端程序也会接收服务器的回复,直到用户关闭客户端程序。
阅读全文