用c语言编写一个聊天服务器和聊天的客户端,实现基本的即时聊天功能。服务器在一个端口上进行监听,接收客户端的连接请求;客户端连接到服务器,发布自己的消息;服务器接收客户端的消息并转发给其他客户端。
时间: 2024-03-12 18:44:43 浏览: 93
好的,这个任务可以分为服务器和客户端两部分来实现。下面是一些简单的代码示例和思路:
服务器端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int client_sockets[MAX_CLIENTS];
int num_clients = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void send_message(int sender, char *message) {
pthread_mutex_lock(&mutex);
for (int i = 0; i < num_clients; i++) {
if (client_sockets[i] != sender) {
send(client_sockets[i], message, strlen(message), 0);
}
}
pthread_mutex_unlock(&mutex);
}
void *handle_client(void *arg) {
int client_socket = *(int *)arg;
char buffer[BUFFER_SIZE];
char message[BUFFER_SIZE + 64];
int read_size;
while ((read_size = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0) {
buffer[read_size] = '\0';
sprintf(message, "Client %d: %s", client_socket, buffer);
send_message(client_socket, message);
}
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);
}
int main(int argc, char *argv[]) {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
pthread_t tid;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(8888);
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(1);
}
if (listen(server_socket, MAX_CLIENTS) < 0) {
perror("listen");
exit(1);
}
printf("Server started on port %d\n", ntohs(server_addr.sin_port));
while (1) {
socklen_t client_len = sizeof(client_addr);
if ((client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_len)) < 0) {
perror("accept");
exit(1);
}
pthread_mutex_lock(&mutex);
if (num_clients < MAX_CLIENTS) {
client_sockets[num_clients++] = client_socket;
pthread_create(&tid, NULL, handle_client, &client_socket);
printf("New client connected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
} else {
printf("Max clients reached, connection rejected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
close(client_socket);
}
pthread_mutex_unlock(&mutex);
}
close(server_socket);
return 0;
}
```
客户端:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>
#define BUFFER_SIZE 1024
int server_socket;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *receive_message(void *arg) {
char buffer[BUFFER_SIZE];
int read_size;
while ((read_size = recv(server_socket, buffer, BUFFER_SIZE, 0)) > 0) {
buffer[read_size] = '\0';
printf("%s", buffer);
}
close(server_socket);
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
struct sockaddr_in server_addr;
pthread_t tid;
char buffer[BUFFER_SIZE];
if (argc < 2) {
printf("Usage: %s <server IP>\n", argv[0]);
exit(1);
}
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
server_addr.sin_port = htons(8888);
if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(1);
}
if (connect(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(1);
}
pthread_create(&tid, NULL, receive_message, NULL);
while (fgets(buffer, BUFFER_SIZE, stdin)) {
pthread_mutex_lock(&mutex);
send(server_socket, buffer, strlen(buffer), 0);
pthread_mutex_unlock(&mutex);
}
close(server_socket);
return 0;
}
```
以上代码只是简单的示例,需要根据实际情况进行调整和优化。同时需要注意处理各种异常情况和错误,以保证程序的稳定性和可靠性。
阅读全文