-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.h
More file actions
48 lines (43 loc) · 1.21 KB
/
server.h
File metadata and controls
48 lines (43 loc) · 1.21 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
#include <signal.h>
#include <iostream>
#include <time.h>
#include <sys/mman.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h> //strlen
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h> //write
#include <string>
//constant definition
#define cmdMaxLen 10
#define filenameMaxLen 255
#define serverPort 9090
#define MAX_CONNECTION 30
#define serverIp "127.0.0.1"
bool prepareSocket(struct sockaddr_in& serverAddr, int& server_sock){
//Prepare the sockaddr_in structure
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons( serverPort );
server_sock = socket(AF_INET, SOCK_STREAM, 0);
if(server_sock == -1){
perror("Could not create socket. Error");
return 0;
}
//Bind
if( bind(server_sock,(struct sockaddr *)&serverAddr , sizeof(serverAddr)) < 0){
//print the error message
perror("Bind failed. Error");
return 0;
}
int listen_ret = listen(server_sock, MAX_CONNECTION);
if(listen_ret == -1){
perror("Could not listen. Error");
return 0;
}
printf("All good, listening...\n");
return 1;
}