Ubuntu下C语言编写一个多人聊天室
时间: 2024-01-23 09:02:17 浏览: 62
要编写一个多人聊天室,需要使用网络编程,即使用套接字(Socket)来实现。在Ubuntu下,可以使用C语言和Linux的Socket API来编写。
以下是一个简单的多人聊天室的示例代码,仅供参考:
server.c:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8888
#define MAX_CLIENTS 30
int main(int argc, char *argv[]) {
int server_fd, client_fd, max_fd, activity, valread, sd;
int clients[MAX_CLIENTS];
struct sockaddr_in address;
char buffer[1024] = {0};
fd_set readfds;
for (int i = 0; i < MAX_CLIENTS; i++) {
clients[i] = 0;
}
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Waiting for connections on port %d...\n", PORT);
while (1) {
FD_ZERO(&readfds);
FD_SET(server_fd, &readfds);
max_fd = server_fd;
for (int i = 0; i < MAX_CLIENTS; i++) {
sd = clients[i];
if (sd > 0) {
FD_SET(sd, &readfds);
}
if (sd > max_fd) {
max_fd = sd;
}
}
activity = select(max_fd + 1, &readfds, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) {
perror("select error");
}
if (FD_ISSET(server_fd, &readfds)) {
if ((client_fd = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
printf("New connection from %s:%d, socket fd is %d\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port), client_fd);
for (int i = 0; i < MAX_CLIENTS; i++) {
if (clients[i] == 0) {
clients[i] = client_fd;
break;
}
}
}
for (int i = 0; i < MAX_CLIENTS; i++) {
sd = clients[i];
if (FD_ISSET(sd, &readfds)) {
if ((valread = read(sd, buffer, 1024)) == 0) {
getpeername(sd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
printf("Host disconnected, ip %s, port %d, socket fd %d\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port), sd);
close(sd);
clients[i] = 0;
}
else {
buffer[valread] = '\0';
printf("Client %d says: %s\n", sd, buffer);
for (int j = 0; j < MAX_CLIENTS; j++) {
if (clients[j] != 0 && clients[j] != sd) {
send(clients[j], buffer, strlen(buffer), 0);
}
}
}
}
}
}
return 0;
}
```
client.c:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8888
int main(int argc, char *argv[]) {
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\nSocket creation error\n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
printf("\nInvalid address\n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection failed\n");
return -1;
}
while (1) {
printf("Enter message: ");
fgets(buffer, 1024, stdin);
if (buffer[0] == '\n') {
continue;
}
send(sock, buffer, strlen(buffer), 0);
printf("Message sent\n");
memset(buffer, 0, sizeof(buffer));
recv(sock, buffer, 1024, 0);
printf("Server response: %s\n", buffer);
}
return 0;
}
```
使用方法:
1. 在终端中编译server.c和client.c:
```
gcc -o server server.c
gcc -o client client.c
```
2. 在终端中启动服务器:
```
./server
```
3. 在另一个终端中启动客户端:
```
./client
```
4. 在客户端输入消息,按Enter键发送消息到服务器,服务器会将消息转发给其他客户端。
阅读全文