-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.c
173 lines (142 loc) · 3.76 KB
/
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
/**
* @author Ammar Faizi <[email protected]>
* @license MIT
*
* Simple socket server.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include "data_structure.h"
int server_socket(char *server_ip, uint16_t server_port);
int main(int argc, char *argv[])
{
if (argc != 3) {
printf("Invalid argument!\n");
printf("Usage: %s <bind_ip> <bind_port>\n", argv[0]);
return 1;
}
return server_socket(argv[1], atoi(argv[2]));
}
int server_socket(char *server_ip, uint16_t server_port)
{
ssize_t rwbytes;
int net_fd, client_fd;
char client_ip[32], data_arena[65538]; /* Why should we use heap if we can use stack? */
packet *pkt = (packet *)&data_arena;
struct sockaddr_in server_addr, client_addr;
socklen_t rlen = sizeof(struct sockaddr_in);
/**
* Prepare server bind address data.
*/
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
server_addr.sin_addr.s_addr = inet_addr(server_ip);
/**
* Prepare client address data.
*/
memset(&client_addr, 0, sizeof(client_addr));
/**
* Create TCP socket.
*/
if ((net_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("Socket creation failed");
return 1; /* Don't need to close socket fd since it fails to create. */
}
/**
* Bind socket to address.
*/
if (bind(net_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("Bind failed");
goto close_server_sock;
}
/**
* Listen
*/
if (listen(net_fd, 3) < 0) {
perror("Listen failed");
goto close_server_sock;
}
printf("Listening on %s:%d...\n", server_ip, server_port);
while (1) {
/**
* Accepting client connection.
*/
client_fd = accept(net_fd, (struct sockaddr *)&client_addr, &rlen);
printf(
"Accepting client (%s:%d)...\n",
inet_ntoa(client_addr.sin_addr),
ntohs(client_addr.sin_port)
);
printf("Waiting for client data...\n");
/**
* Read data length.
*/
rwbytes = recv(client_fd, &(pkt->len), sizeof(uint16_t), 0);
if (rwbytes < 0) {
perror("Read error");
goto close_client_sock;
}
/**
* Read data.
*/
rwbytes = recv(client_fd, &(pkt->data), pkt->len, 0);
if (rwbytes < 0) {
perror("Read error");
goto close_client_sock;
}
/* Add null terminator */
pkt->data[pkt->len] = '\0';
printf("Received data \"%s\" (%d bytes).\n", pkt->data, pkt->len);
// Reverse the data and wrap with '##'
{
register int i;
register int j;
register int k;
register char l;
pkt->data[pkt->len + 2] = '#';
pkt->data[pkt->len + 3] = '#';
pkt->data[pkt->len + 4] = '\0';
k = (pkt->len / 2) + 1;
j = pkt->len + 1;
for (i = 0; i < k; i++, j--) {
l = pkt->data[j];
pkt->data[j] = pkt->data[i];
pkt->data[i] = l;
}
pkt->data[0] = '#';
pkt->data[1] = '#';
}
/* Add wrap chars length */
pkt->len += 4;
printf("Sending response \"%s\" (%d bytes) to the client...\n", pkt->data, pkt->len);
{
size_t wtotal = 0;
/* Send data length */
rwbytes = send(client_fd, &(pkt->len), sizeof(uint16_t), 0);
if (rwbytes < 0) {
perror("Write error");
goto close_client_sock;
}
wtotal += rwbytes;
/* Send data */
rwbytes = send(client_fd, pkt->data, pkt->len, 0);
if (rwbytes < 0) {
perror("Write error");
goto close_client_sock;
}
wtotal += rwbytes;
printf("%ld bytes sent!\n", wtotal);
}
close_client_sock:
close(client_fd);
printf("\n");
}
close_server_sock:
close(net_fd);
return 1;
}