From d40cd308c78b74d90a4a68c0b5a3e2e2e8779824 Mon Sep 17 00:00:00 2001 From: abrahamsonn Date: Fri, 2 Jun 2017 11:04:12 -0600 Subject: [PATCH] Update client-udp.c Moved functionality into main Added comments --- dtls/client-udp.c | 65 +++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/dtls/client-udp.c b/dtls/client-udp.c index 505d21c4..27ea4d26 100644 --- a/dtls/client-udp.c +++ b/dtls/client-udp.c @@ -35,40 +35,16 @@ #define MAXLINE 4096 #define SERV_PORT 11111 -void DatagramClient(FILE* clientInput, int sockfd, - const struct sockaddr* servAddr, socklen_t servLen); - - -/* send and recieve message function */ -void DatagramClient (FILE* clientInput, int sockfd, - const struct sockaddr* servAddr, socklen_t servLen) -{ - - int n; - char sendLine[MAXLINE], recvLine[MAXLINE +1]; - - while (fgets(sendLine, MAXLINE, clientInput) != NULL) { - - if ( ( sendto(sockfd, sendLine, strlen(sendLine) - 1, 0, servAddr, - servLen)) == -1) { - printf("error in sending"); - } - - - if ( (n = recvfrom(sockfd, recvLine, MAXLINE, 0, NULL, NULL)) == -1) { - printf("Error in receiving"); - } - - recvLine[n] = '\0'; - fputs(recvLine, stdout); - } -} - int main(int argc, char** argv) { - int sockfd; - struct sockaddr_in servAddr; + int sockfd; + int recvlen; + struct sockaddr_in servAddr; + const struct sockaddr* servAddr_in; + socklen_t servLen; + char sendLine[MAXLINE]; + char recvLine[MAXLINE + 1]; if (argc != 2) { printf("usage: udpcli \n"); @@ -86,8 +62,31 @@ int main(int argc, char** argv) servAddr.sin_port = htons(SERV_PORT); inet_pton(AF_INET, argv[1], &servAddr.sin_addr); - DatagramClient(stdin, sockfd, (struct sockaddr*) &servAddr, - sizeof(servAddr)); +/****************************************************************************/ +/* Code for sending the datagram to the server */ + servAddr_in = &servAddr; + servLen = sizeof(servAddr); + + /* Loop while user is giving input or until EOF is read */ + while (fgets(sendLine, MAXLINE, stdin) != NULL) { + + /* Attempt to send sendLine to the server */ + if ( ( sendto(sockfd, sendLine, strlen(sendLine) - 1, 0, servAddr_in, + servLen)) == -1) { + printf("Error in sending.\n"); + } + + /* Attempt to receive recvLine from the server */ + if ( (recvlen = recvfrom(sockfd, recvLine, MAXLINE, 0, NULL, NULL)) + == -1) { + printf("Error in receiving.\n"); + } + + recvLine[recvlen] = '\0'; + fputs(recvLine, stdout); + } +/* */ +/****************************************************************************/ return 0; }