tcp服务器c代码,客户端连接断开之后还可以重新连接服务器
时间: 2023-09-29 16:03:20 浏览: 161
以下是一个简单的TCP服务器的C代码示例,其中包含重新连接客户端的功能。当客户端连接断开时,服务器会等待一段时间然后尝试重新连接客户端,直到达到最大重试次数。在这个示例中,我们使用了一个简单的字符串协议,服务器将接收到的任何字符串转换为大写并将其发送回客户端。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX_RETRIES 10
#define RETRY_INTERVAL 5
void handle_client(int client_sock, struct sockaddr_in client_addr);
void handle_error(const char* message);
int main(int argc, char* argv[]) {
int server_sock, client_sock, port;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
if (argc != 2) {
fprintf(stderr, "Usage: %s port\n", argv[0]);
exit(EXIT_FAILURE);
}
port = atoi(argv[1]);
// create socket
if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
handle_error("socket");
}
// bind to port
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(port);
if (bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) {
handle_error("bind");
}
// listen for connections
if (listen(server_sock, 5) == -1) {
handle_error("listen");
}
printf("Server listening on port %d...\n", port);
// accept connections
while (1) {
client_addr_len = sizeof(client_addr);
if ((client_sock = accept(server_sock, (struct sockaddr*)&client_addr, &client_addr_len)) == -1) {
handle_error("accept");
}
printf("Client connected: %s\n", inet_ntoa(client_addr.sin_addr));
// handle client in a separate thread
if (fork() == 0) {
close(server_sock);
handle_client(client_sock, client_addr);
exit(EXIT_SUCCESS);
}
close(client_sock);
}
return 0;
}
void handle_client(int client_sock, struct sockaddr_in client_addr) {
char buffer[1024];
int retries = 0;
while (1) {
// receive message from client
int bytes_received = recv(client_sock, buffer, sizeof(buffer), 0);
if (bytes_received == 0) {
printf("Client disconnected: %s\n", inet_ntoa(client_addr.sin_addr));
break;
} else if (bytes_received == -1) {
handle_error("recv");
}
// convert message to uppercase
for (int i = 0; i < bytes_received; i++) {
buffer[i] = toupper(buffer[i]);
}
// send message back to client
if (send(client_sock, buffer, bytes_received, 0) == -1) {
handle_error("send");
}
}
// try to reconnect client
close(client_sock);
printf("Retrying connection to client: %s\n", inet_ntoa(client_addr.sin_addr));
while (retries < MAX_RETRIES) {
sleep(RETRY_INTERVAL);
if ((client_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
handle_error("socket");
}
if (connect(client_sock, (struct sockaddr*)&client_addr, sizeof(client_addr)) == 0) {
printf("Client reconnected: %s\n", inet_ntoa(client_addr.sin_addr));
handle_client(client_sock, client_addr);
return;
}
retries++;
printf("Connection attempt #%d failed\n", retries);
}
printf("Maximum retries reached, giving up on client: %s\n", inet_ntoa(client_addr.sin_addr));
}
void handle_error(const char* message) {
perror(message);
exit(EXIT_FAILURE);
}
```
阅读全文