用c语言编写一个聊天服务器和聊天的客户端,实现基本的即时聊天功能。服务器在一个端口上进行监听,接收客户端的连接请求;客户端连接到服务器,发布自己的消息;服务器接收客户端的消息并转发给其他客户端。
时间: 2024-03-12 14:44:36 浏览: 39
好的,以下是使用C语言实现基本即时聊天功能的服务器和客户端程序。
服务器程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <pthread.h>
#define SERVER_PORT 8888
#define MAX_CLIENTS 10
int client_count = 0;
int client_sockets[MAX_CLIENTS];
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void *handle_client(void *arg) {
int client_socket = *(int*)arg;
char buffer[1024];
int read_size;
while ((read_size = recv(client_socket, buffer, sizeof(buffer), 0)) > 0) {
buffer[read_size] = '\0';
printf("Received message: %s", buffer);
pthread_mutex_lock(&mutex);
// 转发消息给其他客户端
for (int i = 0; i < client_count; i++) {
if (client_sockets[i] != client_socket) {
send(client_sockets[i], buffer, strlen(buffer), 0);
}
}
pthread_mutex_unlock(&mutex);
}
// 关闭连接
close(client_socket);
pthread_mutex_lock(&mutex);
// 从客户端列表中删除
for (int i = 0; i < client_count; i++) {
if (client_sockets[i] == client_socket) {
client_sockets[i] = 0;
break;
}
}
client_count--;
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
// 创建socket对象
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
// 绑定IP和端口
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
if (bind(server_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
// 开始监听
if (listen(server_socket, MAX_CLIENTS) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server is listening on port %d\n", SERVER_PORT);
// 等待客户端连接
while (1) {
socklen_t client_len = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr*)&client_addr, &client_len);
if (client_socket < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
printf("New client connected: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
pthread_mutex_lock(&mutex);
// 添加到客户端列表
for (int i = 0; i < MAX_CLIENTS; i++) {
if (client_sockets[i] == 0) {
client_sockets[i] = client_socket;
client_count++;
break;
}
}
pthread_mutex_unlock(&mutex);
// 创建新线程处理客户端消息
pthread_t thread;
if (pthread_create(&thread, NULL, handle_client, (void*)&client_socket) < 0) {
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
return 0;
}
```
客户端程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 8888
int main() {
int client_socket;
struct sockaddr_in server_addr;
// 创建socket对象
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
// 连接服务器
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
server_addr.sin_port = htons(SERVER_PORT);
if (connect(client_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(EXIT_FAILURE);
}
// 发送消息
char buffer[1024];
printf("Enter message: ");
fgets(buffer, sizeof(buffer), stdin);
send(client_socket, buffer, strlen(buffer), 0);
// 接收服务器返回的消息
memset(buffer, 0, sizeof(buffer));
recv(client_socket, buffer, sizeof(buffer), 0);
printf("Received message: %s", buffer);
// 关闭连接
close(client_socket);
return 0;
}
```
这是一个比较基础的聊天服务器和客户端程序,您可以根据自己的需求进行改进和扩展。
阅读全文