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 ) {
|
|
|
|
|
int len,n;
|
|
|
|
|
char bufin[MAXBUF];
|
|
|
|
|
struct sockaddr_in remote;
|
|
|
|
|
len = sizeof(remote);
|
|
|
|
|
long count = 0;
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
usleep(50);
|
|
|
|
|
count++;
|
|
|
|
|
|
|
|
|
|
/* read a datagram from the socket (put result in bufin) */
|
|
|
|
|
n=recvfrom(sd,bufin,MAXBUF,0,(struct sockaddr *)&remote,&len);
|
|
|
|
|
|
|
|
|
|
/* print out the address of the sender */
|
|
|
|
|
printf("Got a datagram from %s port %d\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
|
|
|
|
|
|
|
|
|
|
if (n<0) {
|
|
|
|
|
perror("Error receiving data");
|
|
|
|
|
} else {
|
|
|
|
|
printf("GOT %d BYTES (count = %d)\n",n, count);
|
|
|
|
|
/* Got something, just send it back */
|
|
|
|
|
//sendto(sd,bufin,n,0,(struct sockaddr *)&remote,len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* server main routine */
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
if(argc < 2) {
|
|
|
|
|
printf("usage: udp_server <port>\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int port = atoi(argv[1]);
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
int ld;
|
|
|
|
|
struct sockaddr_in skaddr;
|
2016-06-30 15:35:13 -07:00
|
|
|
|
|
|
|
|
struct sockaddr_in skaddr2;
|
|
|
|
|
|
2016-06-27 15:50:03 -07:00
|
|
|
int length;
|
|
|
|
|
|
|
|
|
|
// create socket
|
|
|
|
|
if ((ld = socket( PF_INET, SOCK_DGRAM, 0 )) < 0) {
|
|
|
|
|
printf("Problem creating socket\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create address
|
|
|
|
|
skaddr.sin_family = AF_INET;
|
2016-06-30 15:35:13 -07:00
|
|
|
//skaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
skaddr.sin_addr.s_addr = inet_addr("10.5.5.2");
|
2016-06-27 15:50:03 -07:00
|
|
|
skaddr.sin_port = htons(0);
|
|
|
|
|
|
|
|
|
|
// bind to address
|
|
|
|
|
if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) {
|
|
|
|
|
printf("error binding\n");
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* find out what port we were assigned and print it out */
|
|
|
|
|
|
2016-06-30 15:35:13 -07:00
|
|
|
length = sizeof( skaddr2 );
|
|
|
|
|
if (getsockname(ld, (struct sockaddr *) &skaddr2, &length)<0) {
|
2016-06-27 15:50:03 -07:00
|
|
|
printf("error getsockname\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-30 15:35:13 -07:00
|
|
|
int port = skaddr2.sin_port;
|
|
|
|
|
int ip = ntohs(skaddr2.sin_addr.s_addr);
|
|
|
|
|
unsigned char d[4];
|
|
|
|
|
d[0] = ip & 0xFF;
|
|
|
|
|
d[1] = (ip >> 8) & 0xFF;
|
|
|
|
|
d[2] = (ip >> 16) & 0xFF;
|
|
|
|
|
d[3] = (ip >> 24) & 0xFF;
|
|
|
|
|
printf(" handleBind(): %d.%d.%d.%d : %d -> Assigned: %d\n", d[0],d[1],d[2],d[3], port);
|
|
|
|
|
|
2016-06-27 15:50:03 -07:00
|
|
|
/* echo every datagram */
|
2016-06-30 15:35:13 -07:00
|
|
|
//echo(ld);
|
2016-06-27 15:50:03 -07:00
|
|
|
return(0);
|
|
|
|
|
}
|