2016-06-27 15:50:03 -07:00
|
|
|
// UDP Client test program
|
|
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <unistd.h>
|
2016-06-27 16:25:50 -07:00
|
|
|
#include <string.h>
|
2016-06-27 15:50:03 -07:00
|
|
|
|
|
|
|
|
int main(int argc, char * argv[])
|
|
|
|
|
{
|
2016-07-03 12:01:30 -05:00
|
|
|
if(argc < 3) {
|
|
|
|
|
printf("usage: udp_client <addr> <port>\n");
|
2016-06-29 11:57:05 -07:00
|
|
|
return 0;
|
|
|
|
|
}
|
2016-07-17 16:00:24 -07:00
|
|
|
int sock = -1, port = atoi(argv[1]);
|
2016-06-27 15:50:03 -07:00
|
|
|
ssize_t n_sent;
|
|
|
|
|
struct sockaddr_in server;
|
|
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
|
|
if(sock == -1) {
|
|
|
|
|
sock = socket(AF_INET , SOCK_DGRAM , 0);
|
|
|
|
|
if (sock == -1) {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-03 12:01:30 -05:00
|
|
|
// Construct address
|
|
|
|
|
server.sin_addr.s_addr = inet_addr(argv[1]);
|
2016-06-27 15:50:03 -07:00
|
|
|
server.sin_family = AF_INET;
|
2016-06-27 15:59:07 -07:00
|
|
|
server.sin_port = htons(port);
|
2016-07-03 12:01:30 -05:00
|
|
|
// Connect to server
|
2016-06-27 15:50:03 -07:00
|
|
|
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) {
|
|
|
|
|
printf("api_test: error while connecting.\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-29 17:47:15 -07:00
|
|
|
// TX
|
2016-07-17 16:00:24 -07:00
|
|
|
char *msg = "Welcome to the Machine";
|
2016-06-27 15:59:07 -07:00
|
|
|
int count = 0;
|
2016-06-27 15:50:03 -07:00
|
|
|
|
2016-06-27 15:59:07 -07:00
|
|
|
while(1) {
|
|
|
|
|
count++;
|
2016-06-27 16:25:50 -07:00
|
|
|
usleep(1000000);
|
2016-07-03 12:01:30 -05:00
|
|
|
n_sent = send(sock,msg,sizeof(msg),0);
|
2016-06-27 15:50:03 -07:00
|
|
|
|
2016-06-27 15:59:07 -07:00
|
|
|
if (n_sent<0) {
|
|
|
|
|
perror("Problem sending data");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2016-07-03 12:01:30 -05:00
|
|
|
if (n_sent!=sizeof(msg))
|
2016-06-27 15:59:07 -07:00
|
|
|
printf("Sendto sent %d bytes\n",(int)n_sent);
|
|
|
|
|
printf("n_sent = %d, count = %d\n", n_sent,count);
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-03 12:01:30 -05:00
|
|
|
// RX from server
|
2016-06-27 15:59:07 -07:00
|
|
|
/*
|
2016-06-27 15:50:03 -07:00
|
|
|
socklen_t recv_addr_len;
|
|
|
|
|
// Clear address info for RX test
|
|
|
|
|
server.sin_addr.s_addr = inet_addr("");
|
|
|
|
|
server.sin_port = htons(-1);
|
|
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
|
n_sent=recvfrom(sock,buf,sizeof(buf),0,(struct sockaddr *)&server,&recv_addr_len);
|
|
|
|
|
printf("Got a datagram from %s port %d\n", inet_ntoa(server.sin_addr), ntohs(server.sin_port));
|
|
|
|
|
if (n_sent<0) {
|
|
|
|
|
perror("Error receiving data");
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
printf("RXed: %s\n", buf);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-27 15:59:07 -07:00
|
|
|
*/
|
2016-06-27 15:50:03 -07:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|