This repository was archived by the owner on Sep 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathservidor.c
More file actions
54 lines (40 loc) · 1.27 KB
/
servidor.c
File metadata and controls
54 lines (40 loc) · 1.27 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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <arpa/inet.h>
#include <sys/socket.h>
int main(void) {
struct sockaddr_in direccionServidor;
direccionServidor.sin_family = AF_INET;
direccionServidor.sin_addr.s_addr = INADDR_ANY;
direccionServidor.sin_port = htons(8080);
int servidor = socket(AF_INET, SOCK_STREAM, 0);
int activado = 1;
setsockopt(servidor, SOL_SOCKET, SO_REUSEADDR, &activado, sizeof(activado));
if (bind(servidor, (void*) &direcciónServidor, sizeof(direccionServidor)) != 0) {
perror("Falló el bind");
return 1;
}
printf("Estoy escuchando\n");
listen(servidor, 100);
//------------------------------
struct sockaddr_in direcciónCliente;
unsigned int tamañoDirección;
int cliente = accept(servidor, (void*) &direcciónCliente, &tamañoDirección);
printf("Recibí una conexión en %d!!\n", cliente);
send(cliente, "Hola NetCat!", 13, 0);
send(cliente, ":)\n", 4, 0);
//------------------------------
char* buffer = malloc(1000);
while (1) {
int bytesRecibidos = recv(cliente, buffer, 1000, 0);
if (bytesRecibidos <= 0) {
perror("El chabón se desconectó o bla.");
return 1;
}
buffer[bytesRecibidos] = '\0';
printf("Me llegaron %d bytes con %s\n", bytesRecibidos, buffer);
}
free(buffer);
return 0;
}