4 changed files with 195 additions and 30 deletions
@ -0,0 +1,79 @@
|
||||
// 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> |
||||
|
||||
int main(int argc, char * argv[]) |
||||
{ |
||||
printf("cpp_udp_socket_client_test():\n"); |
||||
ssize_t n_sent; |
||||
int sock = -1; |
||||
struct sockaddr_in server; |
||||
char buf[64];
|
||||
|
||||
|
||||
|
||||
if(sock == -1) { |
||||
sock = socket(AF_INET , SOCK_DGRAM , 0); |
||||
if (sock == -1) { |
||||
return 1; |
||||
} |
||||
} |
||||
server.sin_addr.s_addr = inet_addr("10.242.142.99"); |
||||
server.sin_family = AF_INET; |
||||
server.sin_port = htons(9997); |
||||
|
||||
//char *buf = "Welcome to the Machine\n";
|
||||
memcpy(buf, "Welcome to the Machine", sizeof("Welcome to the Machine"));
|
||||
|
||||
printf("sizeof(buf) = %d\n", sizeof(buf));
|
||||
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) { |
||||
printf("api_test: error while connecting.\n"); |
||||
return 1;
|
||||
} |
||||
|
||||
|
||||
char data[1024]; |
||||
memset(data, 0, sizeof(data)); |
||||
int count =0; |
||||
while(1) |
||||
{ |
||||
count++; |
||||
usleep(1000); |
||||
n_sent = send(sock,data,sizeof(data),0); |
||||
|
||||
if (n_sent<0) { |
||||
perror("Problem sending data"); |
||||
return 1; |
||||
} |
||||
if (n_sent!=sizeof(buf)) |
||||
printf("Sendto sent %d bytes\n",(int)n_sent); |
||||
|
||||
printf("n_sent = %d, count = %d\n", n_sent,count); |
||||
}
|
||||
return 1; |
||||
|
||||
|
||||
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); |
||||
} |
||||
} |
||||
return 1; |
||||
} |
||||
|
||||
|
||||
@ -0,0 +1,95 @@
|
||||
// UDP Server test program
|
||||
|
||||
#include <stdio.h> /* standard C i/o facilities */ |
||||
#include <stdlib.h> /* needed for atoi() */ |
||||
#include <unistd.h> /* defines STDIN_FILENO, system calls,etc */ |
||||
#include <sys/types.h> /* system data type definitions */ |
||||
#include <sys/socket.h> /* socket specific definitions */ |
||||
#include <netinet/in.h> /* INET constants and stuff */ |
||||
#include <arpa/inet.h> /* IP address conversion stuff */ |
||||
#include <netdb.h> /* gethostbyname */ |
||||
|
||||
|
||||
|
||||
/* this routine echos any messages (UDP datagrams) received */ |
||||
|
||||
#define MAXBUF 1024*1024 |
||||
|
||||
void echo( int sd ) { |
||||
int len,n; |
||||
char bufin[MAXBUF]; |
||||
struct sockaddr_in remote; |
||||
|
||||
/* need to know how big address struct is, len must be set before the
|
||||
call to recvfrom!!! */ |
||||
|
||||
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; |
||||
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; |
||||
skaddr.sin_addr.s_addr = htonl(INADDR_ANY); |
||||
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 */ |
||||
|
||||
length = sizeof( skaddr ); |
||||
if (getsockname(ld, (struct sockaddr *) &skaddr, &length)<0) { |
||||
printf("error getsockname\n"); |
||||
exit(1); |
||||
} |
||||
printf("server UDP port = %d\n",ntohs(skaddr.sin_port)); |
||||
|
||||
/* echo every datagram */ |
||||
echo(ld); |
||||
return(0); |
||||
} |
||||
Loading…
Reference in new issue