请用C语言帮我编写一个socket服务器程序,使用tcp协议,这个服务器可连接多个客户端,当收到其中一个客户端的消息时,将这个消息发送给其他连接的客户端。程序编写完成后,请告诉我一个将C语言程序编译为可执行文件的方法
时间: 2024-05-09 19:20:36 浏览: 74
以下是一个基本的TCP socket服务器程序,实现了可连接多个客户端,并能将一个客户端发送的消息广播给其他客户端:
```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 PORT 8888
#define MAX_CLIENTS 10
#define BUFFER_SIZE 1024
int main() {
int server_fd, client_fds[MAX_CLIENTS], max_fd, activity, i, valread;
struct sockaddr_in address;
char buffer[BUFFER_SIZE];
fd_set read_fds;
// 创建服务器socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// 设置服务器socket地址和端口
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// 绑定服务器socket到地址和端口
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听服务器socket,最多有MAX_CLIENTS个客户端连接
if (listen(server_fd, MAX_CLIENTS) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
printf("Server started on port %d.\n", PORT);
// 初始化客户端socket数组
for (i = 0; i < MAX_CLIENTS; i++) {
client_fds[i] = 0;
}
while (1) {
// 将服务器socket和所有客户端socket添加到read_fds中
FD_ZERO(&read_fds);
FD_SET(server_fd, &read_fds);
max_fd = server_fd;
for (i = 0; i < MAX_CLIENTS; i++) {
int fd = client_fds[i];
if (fd > 0) {
FD_SET(fd, &read_fds);
if (fd > max_fd)
max_fd = fd;
}
}
// 等待任何socket有数据到达
activity = select(max_fd + 1, &read_fds, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) {
perror("select failed");
}
// 如果服务器socket有数据到达,说明有新客户端连接
if (FD_ISSET(server_fd, &read_fds)) {
int new_socket;
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
// 将新客户端socket添加到客户端socket数组中
for (i = 0; i < MAX_CLIENTS; i++) {
if (client_fds[i] == 0) {
client_fds[i] = new_socket;
printf("New client connected, socket fd is %d, ip is %s, port is %d.\n", new_socket, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
break;
}
}
}
// 遍历所有客户端socket,查看是否有数据到达
for (i = 0; i < MAX_CLIENTS; i++) {
int fd = client_fds[i];
if (FD_ISSET(fd, &read_fds)) {
if ((valread = read(fd, buffer, BUFFER_SIZE)) == 0) {
// 客户端关闭连接
getpeername(fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
printf("Client disconnected, socket fd is %d, ip is %s, port is %d.\n", fd, inet_ntoa(address.sin_addr), ntohs(address.sin_port));
close(fd);
client_fds[i] = 0;
} else {
// 将客户端发送的消息广播给其他客户端
for (int j = 0; j < MAX_CLIENTS; j++) {
int dest_fd = client_fds[j];
if (dest_fd > 0 && dest_fd != fd) {
send(dest_fd, buffer, valread, 0);
}
}
}
}
}
}
return 0;
}
```
要将C语言程序编译为可执行文件,可以使用gcc编译器。在Linux命令行中执行以下命令:
```
gcc server.c -o server
```
其中server.c是程序源代码文件名,server是生成的可执行文件名。在当前目录下将生成server可执行文件。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)