updated test clients/servers

This commit is contained in:
Joseph Henry
2016-08-11 23:30:58 -07:00
parent 575670a59c
commit c0a89129fa
2 changed files with 90 additions and 75 deletions

View File

@@ -49,17 +49,19 @@ int main(int argc , char *argv[])
// RX
int msglen = 1024;
unsigned long count = 0;
while(1)
{
count++;
int bytes_read = read(client_sock, client_message, msglen);
printf("RX = (%d): ", bytes_read);
printf("[%lu] RX = (%d): ", count, bytes_read);
for(int i=0; i<bytes_read; i++) {
printf("%c", client_message[i]);
}
// TX
int bytes_written = write(client_sock, "Server here!", msglen);
printf("\nTX = %d\n", bytes_written);
int bytes_written = write(client_sock, "Server here!", 12);
printf("\t\nTX = %d\n", bytes_written);
}
return 0;
}

View File

@@ -1,79 +1,92 @@
// UDP Client test program
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/socket.h>
/*
* udpclient.c - A simple UDP client
* usage: udpclient <host> <port>
*/
#include <stdio.h>
#include <unistd.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>
int atoi(const char *str);
#define BUFSIZE 1024
int main(int argc, char * argv[])
/*
* 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)
{
if(argc < 3) {
printf("usage: udp_client <addr> <port>\n");
count++;
printf("\nTX(%lu)...\n", count);
usleep(100000);
//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, &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, &serveraddr, &serverlen);
printf("E\n");
//if (n < 0)
// printf("ERROR in recvfrom: %d", n);
printf("Echo from server: %s", buf);
}
return 0;
}
int sock = -1, port = atoi(argv[1]);
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;
}
}
// Construct address
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_family = AF_INET;
server.sin_port = htons(port);
// Connect to server
if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) {
printf("api_test: error while connecting.\n");
return 1;
}
// TX
char *msg = "Welcome to the Machine";
int count = 0;
while(1) {
count++;
usleep(1000000);
n_sent = send(sock,msg,sizeof(msg),0);
if (n_sent<0) {
perror("Problem sending data");
return 1;
}
if (n_sent!=sizeof(msg))
printf("sendto sent %d bytes\n",(int)n_sent);
printf("n_sent = %ld, count = %d\n", n_sent,count);
}
// RX from server
/*
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;
}