-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
97 lines (73 loc) · 2.9 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
#include "common.h"
// Bibliotecas padrao
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
// Bibliotecas relacionadas a sockets
#include <sys/types.h>
#include <sys/socket.h>
#define BUFFER_SIZE 1024
#define MAX_CONNECTIONS_WAITLIST 50
int main(int argc, char **argv){
struct sockaddr_storage storage;
if(serverInit(argv[1], argv[2], &storage) != 0){
printf("Wrong arguments\n");
}
// inicializando um socket IPv4 ou IPv6
int s;
s = socket(storage.ss_family, SOCK_STREAM, 0);
if(s == -1){
exitLog("Socket! error");
}
// fazendo com que o servidor possa reutilizar uma porta quando ela estiver livre
int enable = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) != 0){
exitLog("setsockopt() error");
}
struct sockaddr *addr = (struct sockaddr *)(&storage);
// bind
if(bind(s, addr, sizeof(storage)) != 0){ // verificando erros no bind
exitLog("Bind error!");
}
// listen
if(listen(s, MAX_CONNECTIONS_WAITLIST) != 0){ // verificando erros no listen
exitLog("Listen error!");
}
// imprimindo mensagem de inicializacao
char addrStr[BUFFER_SIZE];
addrToStr(addr, addrStr, BUFFER_SIZE);
printf("%s\n", addrStr);
printf("Waiting for connections...\n");
//accept
while(1){
struct sockaddr_storage clientStorage; // armazenando o endereco do cliente
struct sockaddr * clientAddr = (struct sockaddr *)(&clientStorage);
socklen_t clientAddrLen = sizeof(clientStorage);
// inicializando o socket que usaremos para falar com o cliente
int clientSocket = accept(s, clientAddr, &clientAddrLen);
if (clientSocket == -1){
exitLog("Socket Error");
}
// imprimindo o endereco do cliente
char clientAddrStr[BUFFER_SIZE];
addrToStr(clientAddr, clientAddrStr, BUFFER_SIZE);
printf("[log] Conected to %s\n", clientAddrStr);
// recebendo os dados do cliente
char buf[BUFFER_SIZE];
memset(buf, 0, BUFFER_SIZE); // limpando a memoria para evitar lixo
size_t count = recv(clientSocket, buf, BUFFER_SIZE, 0);// numero de bytes recebidos
char *fileName = strtok(buf, "0"); // usamos 0 para separar o nome do arquivo com o conteudo da msg
char *msg = strtok(NULL, ""); // todo o resto eh a mensagem
writeStringToFile(msg, fileName);
sprintf(buf, "remote endpoint: %.1000s\n", clientAddrStr);
// conferindo a quantidade de dados enviados
count = send(clientSocket, buf, strlen(buf)+1, 0); // numero de bytes transmitidos na rede
if (count != strlen(buf)+1){ // verificando se a mensagem foi transmitida corretamente
exitLog("send error");
}
close(clientSocket); // fechando a conexao
printf("connection closed");
}
exit(EXIT_SUCCESS);
}