diff options
Diffstat (limited to 'tests/api_test')
| -rw-r--r-- | tests/api_test/tcpclient4.c (renamed from tests/api_test/tcp_client.c) | 0 | ||||
| -rw-r--r-- | tests/api_test/tcpclient6.c (renamed from tests/api_test/client6.c) | 0 | ||||
| -rw-r--r-- | tests/api_test/tcpserver4.c (renamed from tests/api_test/tcp_server.c) | 6 | ||||
| -rw-r--r-- | tests/api_test/tcpserver6.c (renamed from tests/api_test/server6.c) | 0 | ||||
| -rwxr-xr-x | tests/api_test/udp_server6.c (renamed from tests/api_test/udp_server.c) | 0 | ||||
| -rwxr-xr-x | tests/api_test/udpclient.c (renamed from tests/api_test/udp_client.c) | 0 | ||||
| -rwxr-xr-x | tests/api_test/udpclient4.c | 92 | ||||
| -rwxr-xr-x | tests/api_test/udpserver.c | 83 |
8 files changed, 177 insertions, 4 deletions
diff --git a/tests/api_test/tcp_client.c b/tests/api_test/tcpclient4.c index c8ec00c..c8ec00c 100644 --- a/tests/api_test/tcp_client.c +++ b/tests/api_test/tcpclient4.c diff --git a/tests/api_test/client6.c b/tests/api_test/tcpclient6.c index a4aadd4..a4aadd4 100644 --- a/tests/api_test/client6.c +++ b/tests/api_test/tcpclient6.c diff --git a/tests/api_test/tcp_server.c b/tests/api_test/tcpserver4.c index a501964..295a9ea 100644 --- a/tests/api_test/tcp_server.c +++ b/tests/api_test/tcpserver4.c @@ -11,13 +11,11 @@ int atoi(const char *str); int main(int argc , char *argv[]) { if(argc < 2) { - printf("usage: tcp_server <4/6> <port>\n"); - printf("\t - where 4/6 represent IP version\n"); + printf("usage: tcp_server <port>\n"); return 0; } - - int sock, client_sock, c, read_size, ipv = atoi(argv[1]), port = atoi(argv[2]); + int sock, client_sock, c, read_size, port = atoi(argv[1]); char client_message[2000]; char str[100]; diff --git a/tests/api_test/server6.c b/tests/api_test/tcpserver6.c index 740b04c..740b04c 100644 --- a/tests/api_test/server6.c +++ b/tests/api_test/tcpserver6.c diff --git a/tests/api_test/udp_server.c b/tests/api_test/udp_server6.c index c6bd4b3..c6bd4b3 100755 --- a/tests/api_test/udp_server.c +++ b/tests/api_test/udp_server6.c diff --git a/tests/api_test/udp_client.c b/tests/api_test/udpclient.c index c8cc4c1..c8cc4c1 100755 --- a/tests/api_test/udp_client.c +++ b/tests/api_test/udpclient.c diff --git a/tests/api_test/udpclient4.c b/tests/api_test/udpclient4.c new file mode 100755 index 0000000..c8cc4c1 --- /dev/null +++ b/tests/api_test/udpclient4.c @@ -0,0 +1,92 @@ +/* + * 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; +} diff --git a/tests/api_test/udpserver.c b/tests/api_test/udpserver.c new file mode 100755 index 0000000..c6bd4b3 --- /dev/null +++ b/tests/api_test/udpserver.c @@ -0,0 +1,83 @@ +// 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); +} |
