|
|
|
|
@ -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[]) |
|
|
|
|
{ |
|
|
|
|
if(argc < 3) { |
|
|
|
|
printf("usage: udp_client <addr> <port>\n"); |
|
|
|
|
return 0; |
|
|
|
|
/*
|
|
|
|
|
* 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); |
|
|
|
|
} |
|
|
|
|
int sock = -1, port = atoi(argv[1]); |
|
|
|
|
ssize_t n_sent; |
|
|
|
|
struct sockaddr_in server; |
|
|
|
|
char buf[64];
|
|
|
|
|
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"); |
|
|
|
|
|
|
|
|
|
if(sock == -1) { |
|
|
|
|
sock = socket(AF_INET , SOCK_DGRAM , 0); |
|
|
|
|
if (sock == -1) { |
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
/* 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); |
|
|
|
|
} |
|
|
|
|
// 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) { |
|
|
|
|
/* 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++; |
|
|
|
|
usleep(1000000); |
|
|
|
|
n_sent = send(sock,msg,sizeof(msg),0); |
|
|
|
|
printf("\nTX(%lu)...\n", count); |
|
|
|
|
usleep(100000); |
|
|
|
|
//bzero(buf, BUFSIZE);
|
|
|
|
|
//printf("\nPlease enter msg: ");
|
|
|
|
|
//fgets(buf, BUFSIZE, stdin);
|
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
/* 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 1; |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|