-
Notifications
You must be signed in to change notification settings - Fork 8
/
datagrump-receiver.cc
42 lines (33 loc) · 954 Bytes
/
datagrump-receiver.cc
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
#include <stdio.h>
#include "socket.hh"
using namespace Network;
using namespace std;
int main( int argc, char *argv[] )
{
/* check arguments */
if ( argc != 2 ) {
fprintf( stderr, "Usage: %s PORT\n", argv[ 0 ] );
exit( 1 );
}
try {
/* Create UDP socket for incoming datagrams. */
Network::Socket sock;
/* Listen on UDP port. */
sock.bind( Address( "0" /* all IP addresses */,
argv[ 1 ] /* port */ ) );
fprintf( stderr, "Listening on port %s...\n", argv[ 1 ] );
/* Loop */
uint64_t sequence_number = 0;
while ( 1 ) {
Packet received_packet = sock.recv();
/* Send back acknowledgment */
Packet ack( received_packet.addr(), sequence_number++, received_packet );
sock.send( ack );
}
} catch ( const string & exception ) {
/* We got an exception, so quit. */
fprintf( stderr, "Exiting on exception: %s\n", exception.c_str() );
exit( 1 );
}
return 0;
}