-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
132 lines (107 loc) · 3.58 KB
/
server.c
File metadata and controls
132 lines (107 loc) · 3.58 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
//
#include<errno.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<netinet/in.h>
//
#define BACKLOG 10
#define HOST_LEN 30
#define SERV_LEN 30
#define HOST_LEN 30
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_RESET "\x1b[0m"
void ERROR(){
printf(ANSI_COLOR_RED "ERROR(): %s\n\n"ANSI_COLOR_RESET,strerror(errno));
exit(EXIT_FAILURE);
}
void display_string(char*inp,int output_size){
for(size_t i=0;i<output_size;++i)printf("%c",inp[i]);
}
int main(int argc,char**argv){
printf("\n");
if(argc<2){
printf(ANSI_COLOR_RED"Please, chose a port for mount the server...\n\n"ANSI_COLOR_RESET);
exit(EXIT_FAILURE);
}
if(argc<3){
printf(ANSI_COLOR_RED"Please, choose the amount message you want receive\n\n"ANSI_COLOR_RESET);
exit(EXIT_FAILURE);
}
if(argc>3){
printf(ANSI_COLOR_RED"No there's nothing beyond of the port and the number of messages\n\n"ANSI_COLOR_RESET);
exit(EXIT_FAILURE);
}
printf(ANSI_COLOR_BLUE"Mounting the server\n");
struct addrinfo*relay,hints;
memset(&hints,0,sizeof(hints));
hints.ai_flags=AI_PASSIVE;
hints.ai_family=AF_UNSPEC;
hints.ai_socktype=SOCK_STREAM;
printf(ANSI_COLOR_YELLOW"getaddrinfo(): ");
if(getaddrinfo(NULL,argv[1],&hints,&relay))ERROR();
printf(ANSI_COLOR_GREEN"Ok\n");
printf(ANSI_COLOR_YELLOW"socket(): ");
int sock;
if((sock=socket(AF_INET,SOCK_STREAM,0))==-1)ERROR();
printf(ANSI_COLOR_GREEN"OK\n");
printf(ANSI_COLOR_YELLOW"setsockopt():");
int yes=1;
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR | SO_REUSEPORT,&yes,sizeof(yes))==1)ERROR();
printf(ANSI_COLOR_GREEN "OK\n");
printf(ANSI_COLOR_YELLOW"bind(): ");
if(bind(sock,relay->ai_addr,relay->ai_addrlen)==-1)ERROR();
printf(ANSI_COLOR_GREEN"OK\n");
printf(ANSI_COLOR_YELLOW"listen(): ");
if(listen(sock,BACKLOG)==-1)ERROR();
printf(ANSI_COLOR_GREEN"OK\n\n"ANSI_COLOR_RESET);
printf("Server wake up with success in port %s\n",argv[1]);
char localhost[HOST_LEN];
if(gethostname(localhost,sizeof(localhost))==-1)ERROR();
printf("The your name's host is %s\n",localhost);
printf("Waiting for connexions...\n\n");
struct sockaddr_storage their_connection;
int new_sock;
socklen_t len_store=sizeof their_connection;
if((new_sock=accept(sock,(struct sockaddr*)&their_connection,(socklen_t*)&len_store))==-1)ERROR();
printf(ANSI_COLOR_GREEN"Connection found!\n");
close(sock);
struct sockaddr_in who;
socklen_t size_who=sizeof(who);
if(getpeername(new_sock,(struct sockaddr*)&who,&size_who)==-1){
close(new_sock);
close(sock);
ERROR();
}
char ip[INET_ADDRSTRLEN],hostname[HOST_LEN],service[SERV_LEN];
inet_ntop(AF_INET,&who.sin_addr,ip,INET_ADDRSTRLEN);
if(getnameinfo((struct sockaddr*)&who,sizeof(who),hostname,HOST_LEN,service,SERV_LEN,0)==-1)ERROR();
printf(ANSI_COLOR_BLUE"INFORMATIONS ABOUT SENDER\n<-------------------------->\n"ANSI_COLOR_RESET);
printf("ADDRESS:%s:%s::%s\nNAMES_HOST:IP::PORT",hostname,ip,service);
printf(ANSI_COLOR_BLUE"\n*--------------------------*\n"ANSI_COLOR_RESET);
for(int i=0;i<atoi(argv[2]);++i){
int len_msg;
char buffer[1024];
if((len_msg=recv(new_sock,buffer,1023,0))<1){
close(new_sock);
close(sock);
ERROR();
}
printf(ANSI_COLOR_YELLOW " MESSAGE CONTENT\n"ANSI_COLOR_RESET);
display_string(buffer,len_msg);
printf(ANSI_COLOR_BLUE"\nx--------------------------x\n"ANSI_COLOR_RESET);
}
printf("\n");
close(new_sock);
return 0;
}