-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_server.c
209 lines (168 loc) · 6.31 KB
/
tcp_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//
// Created by Varun Bankar on 21/03/22.
// This code is part of lab assignment 7 of CS F303
// This code has companion client code 'tcp_client.c'
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/time.h>
#define TRUE 1
#define FALSE 0
#define BUFFER_SIZE 1024
#define MAX_CLIENTS 4
// flush buffer - set every byte to null char
void flush_buffer(char *buffer) {
memset(buffer, '\0', BUFFER_SIZE);
}
// reverse string
void reverse_string(char *s) {
size_t start = 0, end = strlen(s) - 1;
char temp;
while (start < end) {
temp = s[start];
s[start] = s[end];
s[end] = temp;
start++;
end--;
}
}
int main(int argc, char *argv[]) {
// port
long server_port = strtol(argv[1], NULL, 0);
// socket descriptors
int connection_sockets[MAX_CLIENTS], connection_socket, server_socket, max_socket_descriptor, activity;
// addresses
struct sockaddr_in address, server_address, client_address;
socklen_t client_address_len = sizeof(client_address);
// bookkeeping
size_t received_bytes, read_bytes, sent_bytes;
int status;
int flag;
// set of socket descriptors
fd_set socket_set;
// message buffer
char buffer[BUFFER_SIZE];
// options
int opt = TRUE;
char *shutdown_signal = "exit";
char *connection_full = "ERROR: Connection full.";
char *ok = "OK";
// initialise all connection sockets to 0
for (int i = 0; i < MAX_CLIENTS; ++i) {
connection_sockets[i] = 0;
}
// create server socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
// set server socket to allow multiple connections
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (const void *) &opt, sizeof(int));
// define server address
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(server_port);
// bind server socket to server port
status = bind(server_socket, (struct sockaddr *) &server_address, sizeof(server_address));
if (status == -1) {
perror("ERROR");
exit(-1);
}
//try to specify maximum of 3 pending connections for the master socket
listen(server_socket, MAX_CLIENTS + 1);
printf("INFO: Server started on port %ld.\n", server_port);
while (TRUE) {
// clear socket set
FD_ZERO(&socket_set);
// add server socket to set
FD_SET(server_socket, &socket_set);
max_socket_descriptor = server_socket;
// add connection sockets to set
for (int i = 0; i < MAX_CLIENTS; ++i) {
connection_socket = connection_sockets[i];
if (connection_socket > 0) {
FD_SET(connection_socket, &socket_set);
}
if (connection_socket > max_socket_descriptor) {
max_socket_descriptor = connection_socket;
}
}
// wait for an activity on one of the sockets
activity = select(max_socket_descriptor + 1, &socket_set, NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR)) {
perror("ERROR");
exit(-1);
}
// activity at server socket
if (FD_ISSET(server_socket, &socket_set)) {
// new connection
connection_socket = accept(server_socket, (struct sockaddr *) &client_address, &client_address_len);
printf("INFO: New connection established (%d, %s:%d).\n", connection_socket,
inet_ntoa(client_address.sin_addr),
ntohs(client_address.sin_port));
// add new connection socket to array of sockets
flag = FALSE;
for (int i = 0; i < MAX_CLIENTS; ++i) {
if (connection_sockets[i] == 0) {
connection_sockets[i] = connection_socket;
flag = TRUE;
break;
}
}
if (flag == FALSE) {
// connection full
flush_buffer(buffer);
memcpy(buffer, connection_full, strlen(connection_full));
send(connection_socket, buffer, strlen(buffer), 0);
close(connection_socket);
printf("INFO: Disconnected (%d, %s:%d) - Connection full.\n", connection_socket,
inet_ntoa(client_address.sin_addr),
ntohs(client_address.sin_port));
} else {
// connection successful
flush_buffer(buffer);
memcpy(buffer, ok, strlen(ok));
send(connection_socket, buffer, strlen(buffer), 0);
}
continue;
}
// activity at connection sockets
for (int i = 0; i < MAX_CLIENTS; ++i) {
connection_socket = connection_sockets[i];
if (FD_ISSET(connection_socket, &socket_set)) {
// receive data from client
flush_buffer(buffer);
received_bytes = recv(connection_socket, buffer, sizeof(buffer), 0);
getpeername(connection_socket, (struct sockaddr *) &client_address, &client_address_len);
// handle exit signal
if (memcmp(buffer, shutdown_signal, strlen(shutdown_signal)) == 0) {
printf("INFO: Disconnecting (%s:%d) -> %s\n", inet_ntoa(client_address.sin_addr),
ntohs(client_address.sin_port),
buffer);
close(connection_socket);
connection_sockets[i] = 0;
continue;
}
// output reversed message
reverse_string(buffer);
printf("INFO: (%s:%d) -> %s\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port),
buffer);
}
}
// read from stdin and send to all connected clients
flush_buffer(buffer);
printf("> ");
scanf("%s", buffer);
for (int j = 0; j < MAX_CLIENTS; ++j) {
if (connection_sockets[j] > 0) {
send(connection_sockets[j], buffer, strlen(buffer), 0);
}
}
}
// close server socket
close(server_socket);
return 0;
}