Update client-udp.c

Moved functionality into main
Added comments
pull/55/head
abrahamsonn 2017-06-02 11:04:12 -06:00 committed by GitHub
parent 1a1474af9e
commit d40cd308c7
1 changed files with 32 additions and 33 deletions

View File

@ -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 <IP address>\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;
}