updated api tests
This commit is contained in:
@@ -1,92 +0,0 @@
|
||||
/*
|
||||
* udpclient.c - A simple UDP client
|
||||
* usage: udpclient <host> <port>
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#define BUFSIZE 1024
|
||||
|
||||
/*
|
||||
* error - wrapper for perror
|
||||
*/
|
||||
void error(char *msg) {
|
||||
perror(msg);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int sockfd, portno, n;
|
||||
int serverlen;
|
||||
struct sockaddr_in serveraddr;
|
||||
struct hostent *server;
|
||||
char *hostname;
|
||||
char buf[BUFSIZE];
|
||||
|
||||
/* check command line arguments */
|
||||
if (argc != 3) {
|
||||
fprintf(stderr,"usage: %s <hostname> <port>\n", argv[0]);
|
||||
exit(0);
|
||||
}
|
||||
hostname = argv[1];
|
||||
portno = atoi(argv[2]);
|
||||
|
||||
/* socket: create the socket */
|
||||
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (sockfd < 0)
|
||||
error("ERROR opening socket");
|
||||
|
||||
/* gethostbyname: get the server's DNS entry */
|
||||
server = gethostbyname(hostname);
|
||||
if (server == NULL) {
|
||||
fprintf(stderr,"ERROR, no such host as %s\n", hostname);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/* build the server's Internet address */
|
||||
bzero((char *) &serveraddr, sizeof(serveraddr));
|
||||
serveraddr.sin_family = AF_INET;
|
||||
bcopy((char *)server->h_addr,
|
||||
(char *)&serveraddr.sin_addr.s_addr, server->h_length);
|
||||
serveraddr.sin_port = htons(portno);
|
||||
|
||||
/* get a message from the user */
|
||||
char *msg = "A message to the server!\0";
|
||||
fcntl(sockfd, F_SETFL, O_NONBLOCK);
|
||||
long count = 0;
|
||||
while(1)
|
||||
{
|
||||
count++;
|
||||
printf("\nTX(%lu)...\n", count);
|
||||
usleep(10000);
|
||||
//bzero(buf, BUFSIZE);
|
||||
//printf("\nPlease enter msg: ");
|
||||
//fgets(buf, BUFSIZE, stdin);
|
||||
|
||||
/* send the message to the server */
|
||||
serverlen = sizeof(serveraddr);
|
||||
printf("A\n");
|
||||
n = sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr *)&serveraddr, serverlen);
|
||||
printf("B\n");
|
||||
//if (n < 0)
|
||||
// error("ERROR in sendto");
|
||||
|
||||
/* print the server's reply */
|
||||
printf("C\n");
|
||||
memset(buf, 0, sizeof(buf));
|
||||
printf("D\n");
|
||||
n = recvfrom(sockfd, buf, BUFSIZE, 0, (struct sockaddr *)&serveraddr, (socklen_t *)&serverlen);
|
||||
printf("E\n");
|
||||
//if (n < 0)
|
||||
// printf("ERROR in recvfrom: %d", n);
|
||||
printf("Echo from server: %s", buf);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
73
tests/api_test/udpclient6.c
Executable file
73
tests/api_test/udpclient6.c
Executable file
@@ -0,0 +1,73 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <stdlib.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define MAXBUF 65536
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
int sock;
|
||||
int status;
|
||||
struct addrinfo sainfo, *psinfo;
|
||||
struct sockaddr_in6 sin6;
|
||||
int sin6len;
|
||||
char buffer[MAXBUF];
|
||||
|
||||
sin6len = sizeof(struct sockaddr_in6);
|
||||
|
||||
if(argc < 2)
|
||||
printf("Specify a port number\n"), exit(1);
|
||||
|
||||
sock = socket(PF_INET6, SOCK_DGRAM,0);
|
||||
|
||||
memset(&sin6, 0, sizeof(struct sockaddr_in6));
|
||||
sin6.sin6_port = htons(0);
|
||||
sin6.sin6_family = AF_INET6;
|
||||
sin6.sin6_addr = in6addr_any;
|
||||
|
||||
status = bind(sock, (struct sockaddr *)&sin6, sin6len);
|
||||
|
||||
if(-1 == status)
|
||||
perror("bind"), exit(1);
|
||||
|
||||
memset(&sainfo, 0, sizeof(struct addrinfo));
|
||||
memset(&sin6, 0, sin6len);
|
||||
|
||||
sainfo.ai_flags = 0;
|
||||
sainfo.ai_family = PF_INET6;
|
||||
sainfo.ai_socktype = SOCK_DGRAM;
|
||||
sainfo.ai_protocol = IPPROTO_UDP;
|
||||
status = getaddrinfo("ip6-localhost", argv[1], &sainfo, &psinfo);
|
||||
|
||||
switch (status)
|
||||
{
|
||||
case EAI_FAMILY: printf("family\n");
|
||||
break;
|
||||
case EAI_SOCKTYPE: printf("stype\n");
|
||||
break;
|
||||
case EAI_BADFLAGS: printf("flag\n");
|
||||
break;
|
||||
case EAI_NONAME: printf("noname\n");
|
||||
break;
|
||||
case EAI_SERVICE: printf("service\n");
|
||||
break;
|
||||
}
|
||||
sprintf(buffer,"Ciao");
|
||||
|
||||
status = sendto(sock, buffer, strlen(buffer), 0,
|
||||
(struct sockaddr *)psinfo->ai_addr, sin6len);
|
||||
printf("buffer : %s \t%d\n", buffer, status);
|
||||
|
||||
// free memory
|
||||
freeaddrinfo(psinfo);
|
||||
psinfo = NULL;
|
||||
|
||||
shutdown(sock, 2);
|
||||
close(sock);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,83 +0,0 @@
|
||||
// UDP Server test program
|
||||
|
||||
#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>
|
||||
|
||||
#define MAXBUF 1024*1024
|
||||
|
||||
void echo( int sd ) {
|
||||
char bufin[MAXBUF];
|
||||
struct sockaddr_in remote;
|
||||
int n;
|
||||
socklen_t 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("DGRAM from %s:%d\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
|
||||
|
||||
if (n<0) {
|
||||
perror("Error receiving data");
|
||||
} else {
|
||||
printf("GOT %d BYTES (count = %ld)\n", n, count);
|
||||
// Got something, just send it back
|
||||
// sendto(sd,bufin,n,0,(struct sockaddr *)&remote,len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
if(argc < 2) {
|
||||
printf("usage: udp_server <port>\n");
|
||||
return 0;
|
||||
}
|
||||
int ld, port = atoi(argv[1]);
|
||||
socklen_t len;
|
||||
struct sockaddr_in skaddr;
|
||||
struct sockaddr_in skaddr2;
|
||||
|
||||
// Create socket
|
||||
if ((ld = socket( PF_INET, SOCK_DGRAM, 0)) < 0) {
|
||||
printf("error creating socket\n");
|
||||
return 0;
|
||||
}
|
||||
// Create address
|
||||
skaddr.sin_family = AF_INET;
|
||||
skaddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
skaddr.sin_port = htons(port);
|
||||
// Bind to address
|
||||
if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) {
|
||||
printf("error binding\n");
|
||||
return 0;
|
||||
}
|
||||
// find out what port we were assigned
|
||||
len = sizeof( skaddr2 );
|
||||
if (getsockname(ld, (struct sockaddr *) &skaddr2, &len)<0) {
|
||||
printf("error getsockname\n");
|
||||
return 0;
|
||||
}
|
||||
// Display address:port to verify it was sent over RPC correctly
|
||||
port = ntohs(skaddr2.sin_port);
|
||||
int ip = 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("bound to address: %d.%d.%d.%d : %d\n", d[0],d[1],d[2],d[3], port);
|
||||
|
||||
// RX
|
||||
echo(ld);
|
||||
return(0);
|
||||
}
|
||||
46
tests/api_test/udpserver6.c
Executable file
46
tests/api_test/udpserver6.c
Executable file
@@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAXBUF 65536
|
||||
|
||||
int main()
|
||||
{
|
||||
int sock;
|
||||
int status;
|
||||
struct sockaddr_in6 sin6;
|
||||
int sin6len;
|
||||
char buffer[MAXBUF];
|
||||
|
||||
sock = socket(PF_INET6, SOCK_DGRAM,0);
|
||||
|
||||
sin6len = sizeof(struct sockaddr_in6);
|
||||
|
||||
memset(&sin6, 0, sin6len);
|
||||
|
||||
/* just use the first address returned in the structure */
|
||||
|
||||
sin6.sin6_port = htons(0);
|
||||
sin6.sin6_family = AF_INET6;
|
||||
sin6.sin6_addr = in6addr_any;
|
||||
|
||||
status = bind(sock, (struct sockaddr *)&sin6, sin6len);
|
||||
if(-1 == status)
|
||||
perror("bind"), exit(1);
|
||||
|
||||
status = getsockname(sock, (struct sockaddr *)&sin6, &sin6len);
|
||||
|
||||
printf("%d\n", ntohs(sin6.sin6_port));
|
||||
|
||||
status = recvfrom(sock, buffer, MAXBUF, 0,
|
||||
(struct sockaddr *)&sin6, &sin6len);
|
||||
printf("buffer : %s\n", buffer);
|
||||
|
||||
shutdown(sock, 2);
|
||||
close(sock);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user