2016-06-27 15:50:03 -07:00
|
|
|
// UDP Server test program
|
|
|
|
|
|
2016-06-29 17:47:15 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <netdb.h>
|
2016-06-27 15:50:03 -07:00
|
|
|
|
|
|
|
|
#define MAXBUF 1024*1024
|
|
|
|
|
|
|
|
|
|
void echo( int sd ) {
|
|
|
|
|
char bufin[MAXBUF];
|
|
|
|
|
struct sockaddr_in remote;
|
2016-07-18 12:24:24 -07:00
|
|
|
int n;
|
|
|
|
|
socklen_t len = sizeof(remote);
|
2016-06-27 15:50:03 -07:00
|
|
|
long count = 0;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
usleep(50);
|
|
|
|
|
count++;
|
2016-07-03 12:01:30 -05:00
|
|
|
// read a datagram from the socket (put result in bufin)
|
2016-06-27 15:50:03 -07:00
|
|
|
n=recvfrom(sd,bufin,MAXBUF,0,(struct sockaddr *)&remote,&len);
|
2016-07-03 12:01:30 -05:00
|
|
|
// print out the address of the sender
|
|
|
|
|
printf("DGRAM from %s:%d\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
|
2016-06-27 15:50:03 -07:00
|
|
|
|
|
|
|
|
if (n<0) {
|
|
|
|
|
perror("Error receiving data");
|
|
|
|
|
} else {
|
2016-07-18 12:24:24 -07:00
|
|
|
printf("GOT %d BYTES (count = %ld)\n", n, count);
|
2016-07-03 12:01:30 -05:00
|
|
|
// Got something, just send it back
|
|
|
|
|
// sendto(sd,bufin,n,0,(struct sockaddr *)&remote,len);
|
2016-06-27 15:50:03 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
2016-06-30 17:51:07 -07:00
|
|
|
|
2016-06-27 15:50:03 -07:00
|
|
|
if(argc < 2) {
|
|
|
|
|
printf("usage: udp_server <port>\n");
|
2016-07-18 12:24:24 -07:00
|
|
|
return 0;
|
2016-06-27 15:50:03 -07:00
|
|
|
}
|
2016-07-18 12:24:24 -07:00
|
|
|
int ld, port = atoi(argv[1]);
|
|
|
|
|
socklen_t len;
|
2016-06-27 15:50:03 -07:00
|
|
|
struct sockaddr_in skaddr;
|
2016-07-03 12:01:30 -05:00
|
|
|
struct sockaddr_in skaddr2;
|
2016-06-30 15:35:13 -07:00
|
|
|
|
2016-07-03 12:01:30 -05:00
|
|
|
// Create socket
|
|
|
|
|
if ((ld = socket( PF_INET, SOCK_DGRAM, 0)) < 0) {
|
|
|
|
|
printf("error creating socket\n");
|
2016-07-18 12:24:24 -07:00
|
|
|
return 0;
|
2016-06-27 15:50:03 -07:00
|
|
|
}
|
2016-07-03 12:01:30 -05:00
|
|
|
// Create address
|
2016-06-27 15:50:03 -07:00
|
|
|
skaddr.sin_family = AF_INET;
|
2016-07-03 12:01:30 -05:00
|
|
|
skaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
skaddr.sin_port = htons(port);
|
|
|
|
|
// Bind to address
|
2016-06-27 15:50:03 -07:00
|
|
|
if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) {
|
|
|
|
|
printf("error binding\n");
|
2016-07-18 12:24:24 -07:00
|
|
|
return 0;
|
2016-06-27 15:50:03 -07:00
|
|
|
}
|
2016-07-03 12:01:30 -05:00
|
|
|
// find out what port we were assigned
|
2016-07-18 12:24:24 -07:00
|
|
|
len = sizeof( skaddr2 );
|
|
|
|
|
if (getsockname(ld, (struct sockaddr *) &skaddr2, &len)<0) {
|
2016-06-27 15:50:03 -07:00
|
|
|
printf("error getsockname\n");
|
2016-07-18 12:24:24 -07:00
|
|
|
return 0;
|
2016-06-27 15:50:03 -07:00
|
|
|
}
|
2016-07-03 12:01:30 -05:00
|
|
|
// Display address:port to verify it was sent over RPC correctly
|
2016-07-17 16:00:24 -07:00
|
|
|
port = ntohs(skaddr2.sin_port);
|
2016-06-30 16:10:51 -07:00
|
|
|
int ip = skaddr2.sin_addr.s_addr;
|
2016-06-30 15:35:13 -07:00
|
|
|
unsigned char d[4];
|
|
|
|
|
d[0] = ip & 0xFF;
|
|
|
|
|
d[1] = (ip >> 8) & 0xFF;
|
|
|
|
|
d[2] = (ip >> 16) & 0xFF;
|
|
|
|
|
d[3] = (ip >> 24) & 0xFF;
|
2016-07-18 12:24:24 -07:00
|
|
|
printf("bound to address: %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], port);
|
2016-06-30 15:35:13 -07:00
|
|
|
|
2016-07-03 12:01:30 -05:00
|
|
|
// RX
|
2016-06-30 16:10:51 -07:00
|
|
|
echo(ld);
|
2016-06-27 15:50:03 -07:00
|
|
|
return(0);
|
|
|
|
|
}
|