-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservidor_udp.c
58 lines (40 loc) · 1.09 KB
/
servidor_udp.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
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define P_SIZE sizeof(struct psuma)
struct psuma {
uint16_t v1;
uint16_t v2;
uint32_t res;
};
int main()
{
int n;
int sd;
int lon;
char buffer[P_SIZE];
struct sockaddr_in servidor;
struct sockaddr_in cliente;
struct psuma *suma;
servidor.sin_family = AF_INET;
servidor.sin_port = htons(4444);
servidor.sin_addr.s_addr = INADDR_ANY;
sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (bind(sd, (struct sockaddr *) &servidor, sizeof(servidor)) < 0) {
perror("Error en bind");
exit(-1);
}
while (1) {
lon = sizeof(cliente);
n = recvfrom(sd, buffer, P_SIZE, 0, (struct sockaddr *) &cliente, &lon);
suma = (struct psuma *) buffer;
printf("Recibo: %d %d %d \n", ntohs(suma->v1), ntohs(suma->v2), ntohl(suma->res));
suma->res = htonl(ntohs(suma->v1) + ntohs(suma->v2));
printf("Envío: %d %d %d \n", ntohs(suma->v1), ntohs(suma->v2), ntohl(suma->res));
sendto(sd, buffer, P_SIZE, 0, (struct sockaddr *) &cliente, lon);
}
close(sd);
}