-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_server.c
86 lines (71 loc) · 2.48 KB
/
sample_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#define PORT 8889
#define BUFFER_SIZE 1024
#define TIMEOUT_SECONDS 5
int main() {
int server_fd, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_len = sizeof(client_addr);
char buffer[BUFFER_SIZE];
// Create socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
// Prepare the server address structure
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
// Bind socket to address
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("Bind failed");
exit(EXIT_FAILURE);
}
// Listen for connections
if (listen(server_fd, 5) == -1) {
perror("Listen failed");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d\n", PORT);
while (1) {
// Accept incoming connection
if ((client_socket = accept(server_fd, (struct sockaddr *)&client_addr, &addr_len)) == -1) {
perror("Accept failed");
exit(EXIT_FAILURE);
}
// Set timeout for the socket
struct timeval timeout;
timeout.tv_sec = TIMEOUT_SECONDS;
timeout.tv_usec = 0;
if (setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)) == -1) {
perror("Set socket timeout failed");
exit(EXIT_FAILURE);
}
printf("Connection accepted from %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
// Read data from client
ssize_t bytes_received = recv(client_socket, buffer, BUFFER_SIZE, 0);
if (bytes_received == -1) {
perror("Receive error");
close(client_socket);
continue;
} else if (bytes_received == 0) {
printf("Client disconnected\n");
close(client_socket);
continue;
}
// Process received data
printf("Received data from client: %.*s\n", (int)bytes_received, buffer);
// Send response back to client
const char *response = "Server received your message";
send(client_socket, response, strlen(response), 0);
// close(client_socket);
}
return 0;
}