c语言并发回应服务器与客户端的设计代码
时间: 2023-12-13 20:04:02 浏览: 105
以下是一个简单的C语言并发回应服务器与客户端的设计代码示例:
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8888
#define MAX_CLIENTS 10
#define MAX_BUFFER_SIZE 1024
int client_sockets[MAX_CLIENTS];
int num_clients = 0;
pthread_mutex_t mutex;
void *client_thread(void *arg) {
int client_socket = *(int *)arg;
char buffer[MAX_BUFFER_SIZE];
int n;
while (1) {
n = recv(client_socket, buffer, MAX_BUFFER_SIZE, 0);
if (n <= 0) {
// Client disconnected
printf("Client disconnected\n");
pthread_mutex_lock(&mutex);
for (int i = 0; i < num_clients; i++) {
if (client_sockets[i] == client_socket) {
client_sockets[i] = client_sockets[num_clients - 1];
num_clients--;
break;
}
}
pthread_mutex_unlock(&mutex);
close(client_socket);
pthread_exit(NULL);
} else {
buffer[n] = '\0';
printf("Received message: %s", buffer);
// Process request and send response
char response[MAX_BUFFER_SIZE];
sprintf(response, "Server received message: %s", buffer);
send(client_socket, response, strlen(response), 0);
}
}
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
int addr_len = sizeof(client_addr);
pthread_t client_threads[MAX_CLIENTS];
// Initialize mutex
pthread_mutex_init(&mutex, NULL);
// Create server socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("Failed to create server socket");
exit(EXIT_FAILURE);
}
// Bind server socket to port
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Failed to bind server socket to port");
exit(EXIT_FAILURE);
}
// Listen for connections
if (listen(server_socket, MAX_CLIENTS) < 0) {
perror("Failed to listen for connections");
exit(EXIT_FAILURE);
}
// Accept client connections
while (1) {
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, (socklen_t *)&addr_len);
if (client_socket < 0) {
perror("Failed to accept client connection");
continue;
}
printf("Client connected: %s\n", inet_ntoa(client_addr.sin_addr));
// Create client thread
pthread_mutex_lock(&mutex);
client_sockets[num_clients++] = client_socket;
pthread_create(&client_threads[num_clients - 1], NULL, client_thread, &client_socket);
pthread_mutex_unlock(&mutex);
}
// Clean up
close(server_socket);
pthread_mutex_destroy(&mutex);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1"
#define PORT 8888
#define MAX_BUFFER_SIZE 1024
int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[MAX_BUFFER_SIZE];
int n;
// Create client socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("Failed to create client socket");
exit(EXIT_FAILURE);
}
// Connect to server
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
server_addr.sin_port = htons(PORT);
if (connect(client_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Failed to connect to server");
exit(EXIT_FAILURE);
}
// Send message to server
printf("Enter message: ");
fgets(buffer, MAX_BUFFER_SIZE, stdin);
send(client_socket, buffer, strlen(buffer), 0);
// Receive response from server
n = recv(client_socket, buffer, MAX_BUFFER_SIZE, 0);
if (n < 0) {
perror("Failed to receive response from server");
exit(EXIT_FAILURE);
} else {
buffer[n] = '\0';
printf("Received response: %s", buffer);
}
// Clean up
close(client_socket);
return 0;
}
```
注意:以上代码仅供参考,需要根据实际情况进行修改和优化。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)