-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
81 lines (63 loc) · 1.68 KB
/
server.c
File metadata and controls
81 lines (63 loc) · 1.68 KB
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
/*
* Addison McAuley, 11295940, amm637
* Marwan Mostafa, 11305332, mam024
*/
#include <prog.h>
int main(int argc,char *argv[])
{
char* port; /*port for server to listen on*/
int s; /*main socket*/
int b; /*return value of bind*/
int l; /*return value of listen*/
/*vars for getaddrinfo*/
int status;
struct addrinfo hints;
struct addrinfo *servinfo;
/*checking arg count*/
if (argc != 2){
fprintf(stderr,"inccorect amount of args: only port required\n");
exit(1);
}
/*saving port from cmd arg*/
port = argv[1];
/*setting up structs for getaddrinfo*/
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
/*calling getaddrinfo*/
status = getaddrinfo(NULL,port,&hints, &servinfo);
/*checking return of getaddrinfo*/
if (status != 0){
fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
exit(1);
}
/*seting up main socket*/
s = socket(servinfo->ai_family,
servinfo->ai_socktype,
servinfo->ai_protocol);
/*checking return of socket*/
if (s == -1){
fprintf(stderr,"socket error:");
exit(1);
}
/*bind socket to port*/
b = bind(s,servinfo->ai_addr,servinfo->ai_addrlen);
/*checking bind return value*/
if (b == -1){
fprintf(stderr,"bind error");
exit(1);
}
/*calling listen on main socket*/
l = listen(s,1);
/*checking return value of listen*/
if (l == -1){
fprintf(stderr,"listen error");
exit(1);
}
/*handling conenctions base on implmentation type*/
handle_connections(s);
/*freeing alocated structs*/
freeaddrinfo(servinfo);
return 0;
}