-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.c
73 lines (59 loc) · 1.65 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
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include "spiffy.h"
#define PACKETLEN 1500
#define BUFLEN 100
typedef struct header_s {
short magicnum;
char version;
char packet_type;
short header_len;
short packet_len;
u_int seq_num;
u_int ack_num;
} header_t;
typedef struct data_packet {
header_t header;
char data[BUFLEN];
} data_packet_t;
int main(int argc, char **argv) {
struct sockaddr_in addr, from;
socklen_t fromlen;
char buf[PACKETLEN];
int sock;
struct sockaddr_in myaddr;
fd_set readfds;
struct user_iobuf *userbuf;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
data_packet_t *curr;
if (argc < 3) {
printf("usage: %s <node id> <port>\n", argv[0]);
return 1;
}
bzero(&myaddr, sizeof(myaddr));
myaddr.sin_family = AF_INET;
inet_aton("127.0.0.1", &myaddr.sin_addr);
myaddr.sin_port = htons(atoi(argv[2]));
if (bind(fd, (struct sockaddr *) &myaddr, sizeof(myaddr)) == -1) {
perror("peer_run could not bind socket");
}
/* init spiffy */
spiffy_init(atoi(argv[1]), (struct sockaddr *) &myaddr, sizeof(myaddr));
while (1) {
int nfds;
FD_SET(fd, &readfds);
nfds = select(fd+1, &readfds, NULL, NULL, NULL);
if (nfds > 0) {
spiffy_recvfrom(fd, buf, BUFLEN, 0, (struct sockaddr *) &from, &fromlen);
curr = (data_packet_t*)buf;
printf("MAGIC: %d\n", ntohs((curr->header).magicnum));
fflush(stdout);
}
// check timers
}
return 0;
}