linux建立服务端和多个客户端连接互相通信,用c语言
时间: 2023-05-22 10:01:54 浏览: 94
可以使用socket编程实现。在服务端,可以使用socket()函数创建一个socket,然后使用bind()函数将socket绑定到一个IP地址和端口号上,接着使用listen()函数监听客户端的连接请求。在客户端,可以使用socket()函数创建一个socket,然后使用connect()函数连接到服务端。连接成功后,服务端可以使用accept()函数接受客户端的连接请求,然后使用send()和recv()函数与客户端进行通信。客户端可以使用send()和recv()函数与服务端进行通信。需要注意的是,socket编程需要处理错误和异常情况,以保证程序的稳定性和可靠性。
以下是一个简单的示例代码:
服务端:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8080
#define MAX_CLIENTS 10
int main() {
int server_fd, new_socket, valread;
struct sockaddr_in address;
int addrlen = sizeof(address);
char buffer[1024] = {0};
int clients[MAX_CLIENTS] = {0};
int max_clients = MAX_CLIENTS;
int activity, i, sd, max_sd;
// create server socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// set server socket options
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt failed");
exit(EXIT_FAILURE);
}
// bind server socket to IP address and port
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);
}
// listen for client connections
if (listen(server_fd, 3) < 0) {
perror("listen failed");
exit(EXIT_FAILURE);
}
// accept client connections and handle data
while (1) {
// clear socket set
fd_set readfds;
FD_ZERO(&readfds);
// add server socket to set
FD_SET(server_fd, &readfds);
max_sd = server_fd;
// add client sockets to set
for (i = 0; i < max_clients; i++) {
sd = clients[i];
if (sd > 0) {
FD_SET(sd, &readfds);
}
if (sd > max_sd) {
max_sd = sd;
}
}
// wait for activity on any socket
activity = select(max_sd + 1, &readfds, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) {
perror("select failed");
}
// handle activity on server socket
if (FD_ISSET(server_fd, &readfds)) {
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen)) < 0) {
perror("accept failed");
exit(EXIT_FAILURE);
}
// add new client socket to array
for (i = 0; i < max_clients; i++) {
if (clients[i] == 0) {
clients[i] = new_socket;
break;
}
}
}
// handle activity on client sockets
for (i = 0; i < max_clients; i++) {
sd = clients[i];
if (FD_ISSET(sd, &readfds)) {
if ((valread = read(sd, buffer, 1024)) == 0) {
// client disconnected
close(sd);
clients[i] = 0;
} else {
// handle client data
buffer[valread] = '\0';
printf("Client %d: %s", sd, buffer);
send(sd, buffer, strlen(buffer), 0);
}
}
}
}
return 0;
}
```
客户端:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
#define SERVER_ADDR "127.0.0.1"
int main() {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char buffer[1024] = {0};
// create client socket
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
// set server address and port
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
if (inet_pton(AF_INET, SERVER_ADDR, &serv_addr.sin_addr) <= 0) {
perror("inet_pton failed");
exit(EXIT_FAILURE);
}
// connect to server
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("connect failed");
exit(EXIT_FAILURE);
}
// send and receive data
while (1) {
printf("Enter message: ");
fgets(buffer, 1024, stdin);
send(sock, buffer, strlen(buffer), 0);
valread = read(sock, buffer, 1024);
buffer[valread] = '\0';
printf("Server: %s", buffer);
}
return 0;
}
```
阅读全文