-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainServer.c
57 lines (43 loc) · 1.8 KB
/
mainServer.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
#include <sys/socket.h> //socket(), bind()
#include <stdlib.h> //exit()
#include <netinet/in.h> // htons() ???
#include <stdio.h> //perror()
#include <arpa/inet.h> //inet_pton(), htons()
#include <unistd.h>//read()
#include "dataStructures.h"
#include "server.h"
struct details client;
int main() {
//-----------------------------------------------------------------------------------------------init main server
int mainSocket;
struct sockaddr_in address; //specializzazione ipv4 della struct generica sockaddr
socklen_t slen = sizeof(address);
//creazione della socket udp
mainSocket = createSocket();
//settaggio dei valori della struct
address = createStruct(PORT); //create main socket on well known port
//il server deve anche fare un bind della socket a una porta nota
bindSocket(mainSocket, (struct sockaddr *) &address, slen);
//-----------------------------------------------------------------------------------------------main server loop
//il server inizia a servire le richieste in un ciclo continuo
for(;;)
{
handshake SYN;
acceptConnection(mainSocket, &SYN, (struct sockaddr *) &(client.addr), &slen);
//arriva un messaggio e salvo i dati del client nella struct
//fork to allow child process to serve the client
pid_t processPid = fork();
if(processPid == -1)
{
perror("error in fork\n");
//exit(EXIT_FAILURE);
}
if(processPid == 0)//child process
{
printf("*----------------------------*\n un client vorrebbe connettersi\n*----------------------------*\n\n\n");
listenFunction(mainSocket, &client, &SYN);
}
//if I am the parent process, I continue waiting for connections on this port
}
exit(EXIT_SUCCESS);
}