-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.c
114 lines (72 loc) · 2.27 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#define MAXIMUM_CONNECTION 100
#define BUFFER_SIZE 1024
/* to run the code use ./server 127.0.0.1 5050 */
void signalHandler(int signal_num)
{
printf("\nYou just pressed <control-C>, however you can't close server, try <control-Z> ");
signal(SIGINT, SIG_IGN);
}
int main(int argc, char *argv[])
{
if (argv[1] == NULL)
{
printf("\033[1;31m");
printf("IP address is empty\n");
exit(0);
}
if (argv[2] == NULL)
{
printf("\033[1;31m");
printf("Port address is empty\n");
exit(0);
}
const char *ipaddress = argv[1];
const int port = atoi(argv[2]);
// signalling to ignore control-C
signal(SIGINT, signalHandler);
int sockfd, clientSock, bytes, index = 0;
struct sockaddr_in serverAddr, clientAddr;
memset(&serverAddr, '\0', sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(port);
serverAddr.sin_addr.s_addr =inet_addr(ipaddress);
sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) >= 0)
{
printf("\033[0;32m");
printf("\n successfully binded the address to socket");
}
listen(sockfd, MAXIMUM_CONNECTION);
char buffer[BUFFER_SIZE], message[BUFFER_SIZE];
while (1)
{
socklen_t clientAddrLen = sizeof(clientAddr);
if (clientSock = accept(sockfd, (struct sockaddr *)&clientAddr, &clientAddrLen))
{
printf("\033[0;32m");
printf("\n client accepted");
}
memset(buffer, '\0', sizeof(buffer));
memset(message, '\0', sizeof(message));
bytes = recv(clientSock, buffer, BUFFER_SIZE - 1, 0);
buffer[BUFFER_SIZE] = '\0';
printf("\n Received data: ");
fputs(buffer, stdout);
printf("\n Type data to send: ");
while ((message[index++] = getchar()) != '\n')
;
message[BUFFER_SIZE] = '\n';
send(clientSock, message, sizeof(message), 0);
}
shutdown(sockfd, SHUT_RDWR);
exit(1);
}