-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
119 lines (111 loc) · 3.86 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
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
/*
*This work is licensed under a Creative Commons Attribution 4.0 International License.
*Author: Benedetto Marco Serinelli
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#define PORT 53000 // port number
#define BACKLOG 10 //number of connections
#define BUFSIZE 256 //size of the message buffer
void saveInFile(char *buffer);
int main(int argc, char *argv[]) {
int socket_fd;
//create an endpoint for communication
socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if( socket_fd == -1 )
perror("Socket creation failed");
else
printf("Socket created\n");
/* struttura server */
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
//set family address
addr.sin_family = AF_INET;
//set port
addr.sin_port = htons(PORT);
//INADDR_ANY is a constant that equal zero
addr.sin_addr.s_addr = htons(INADDR_ANY);
//bind a name to a socket
if(bind(socket_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
perror("Error to bind socket");
else
printf("Bind OK\n");
//listen for connections on a socket
if(listen(socket_fd, BACKLOG) < 0)
perror("Listen for connections on a socket failed");
else {
printf("Listen for connections on a socket... OK\n");
printf("Server listens for connections on a port %d.\n", PORT);
}
//client struct
struct sockaddr_in client_addr;
socklen_t len = sizeof(client_addr);
int quit = 0;
int client;
while(!quit) {
//accept client connection
client = accept(socket_fd, (struct sockaddr *)&client_addr, &len);
if(client < 0)
perror("Client connection was refused. Can not accept client connection");
else {
printf("Client connection is accepted\n");
char client_addr_c[INET_ADDRSTRLEN] = "";
inet_ntop(AF_INET, &(client_addr.sin_addr), client_addr_c, sizeof(client_addr));
printf("New client connection from[IP:PORT] %s:%d.\n", client_addr_c, ntohs(client_addr.sin_port));
}
char buf[BUFSIZE];
char buf_reply[BUFSIZE] = "Hi client :D";
ssize_t n = 0;
//receive message from client
n = recv(client, buf, BUFSIZE-1, 0);
if(n < 0) {
perror("Message from client can't be received");
} else if(n == 0)
printf("Connection is closed by client.\n");
else {
//add null terminator, abbreviated NULL, at end part of buffer
buf[n] = '\0';
printf("Message was be received by client: %s. Message size in byte: %ld.\n", buf, sizeof(buf));
saveInFile(buf); //save received message in file
//send reply to client
n = send(client, buf_reply, BUFSIZE - 1, 0);
if(n < 0)
perror("Message can't be sent");
else if(n == 0)
printf("Connection was be closed by client\n");
else
printf("Reply client with message. Message size in byte %d \n", (int)n);
}
//close client socket
close(client);
}
}
void saveInFile(char *buffer) {
printf("Message: %s\n", buffer);
FILE *fp = fopen("connection.txt", "a");
if (fp != NULL) {
//add timestamp before mesage
time_t ltime; /* calendar time */
ltime = time(NULL); /* get current cal time */
//printf("%s",asctime(localtime(<ime)));
fputs(asctime(localtime(<ime)), fp);
fputs(buffer, fp);
fputs("\n", fp);
fclose(fp);
} else {
//create file
FILE *fpNEW = fopen("connection.txt", "w");
if (fpNEW != NULL) {
perror("Impossible to create a file");
} else {
fclose(fpNEW);
saveInFile(buffer);
}
}
}