-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
76 lines (60 loc) · 1.44 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
#include <sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"serverUtils.h"
#define PORT 8082
int main()
{
printf("Initializing connection...\n");
struct sockaddr_in server, client;
int client_addr_len, client_sd;
//-----------------------------SOCKET------------------------------
int sd = socket(AF_INET, SOCK_STREAM,0);
if(sd==-1)
{ perror("Socket creation failed\n");
exit(0);
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
//-----------------------------BIND---------------------------------
printf("Binding socket...\n");
int retB=bind(sd,(struct sockaddr *)(&server), sizeof(server));
if(retB<0){
perror("binding port failed\n");
exit(0);
}
//----------------------------LISTEN--------------------------------
int retL=listen(sd,10);
if(retL==-1){
perror("listen failed\n");
exit(0);
}
printf("Listening...\n");
client_addr_len = sizeof(client);
//-----------------------------ACCEPT-------------------------------
while(1){
client_sd = accept(sd, (struct sockaddr *)&client, &client_addr_len);
if(!fork()){
close(sd);
client_service(client_sd);
exit(0);
}
else
{
close(client_sd);
}
}
//CLOSE
close(client_sd);
close(sd);
printf("Connection Closed..\n");
return 0;
}