From ae1ea7d33d1a7f3501dbb4d616a69bb796e23af2 Mon Sep 17 00:00:00 2001 From: Conner Date: Wed, 31 May 2017 09:51:18 -0600 Subject: [PATCH] removed additional methods so that the code reads linearly in server-tcp and client-tcp --- psk/client-tcp.c | 38 ++++++++++++++------------------------ psk/server-tcp.c | 48 +++++++++++++++++++++--------------------------- 2 files changed, 35 insertions(+), 51 deletions(-) diff --git a/psk/client-tcp.c b/psk/client-tcp.c index 6d679de4..d01454d0 100644 --- a/psk/client-tcp.c +++ b/psk/client-tcp.c @@ -37,31 +37,12 @@ * this function will send the inputted string to the server and then * recieve the string from the server outputing it to the termial */ -int SendReceive(int sockfd) -{ - char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ - char recvline[MAXLINE]; /* string received from the server */ - - /* write string to the server */ - if (write(sockfd, sendline, strlen(sendline)) != strlen(sendline)) { - printf("Write Error to Server\n"); - return 1; - } - - /* flags if the server stopped before the client could end */ - if (read(sockfd, recvline, MAXLINE) == 0) { - printf("Client: Server Terminated Prematurely!\n"); - return 1; - } - - printf("Server Message: %s\n", recvline); - - return 0; -} int main(int argc, char **argv) { int sockfd, ret; + char sendline[MAXLINE]="Hello Server"; /* string to send to the server */ + char recvline[MAXLINE]; /* string received from the server */ struct sockaddr_in servaddr; /* must include an ip address or this will flag */ @@ -96,11 +77,20 @@ int main(int argc, char **argv) } /* takes inputting string and outputs it to the server */ - ret = SendReceive(sockfd); - if (ret != 0){ - printf("Send Recieve Error"); + /* write string to the server */ + if (write(sockfd, sendline, strlen(sendline)) != strlen(sendline)) { + printf("Write Error to Server\n"); return 1; } + + /* flags if the server stopped before the client could end */ + if (read(sockfd, recvline, MAXLINE) == 0) { + printf("Client: Server Terminated Prematurely!\n"); + return 1; + } + + printf("Server Message: %s\n", recvline); + /* close socket and connection */ close(sockfd); diff --git a/psk/server-tcp.c b/psk/server-tcp.c index 3a05b9e5..9a2c3315 100644 --- a/psk/server-tcp.c +++ b/psk/server-tcp.c @@ -42,34 +42,17 @@ void err_sys(const char *err, ...) printf("Fatal error : %s\n", err); } -/* - * Handles response to client. - */ -void respond(int sockfd) -{ - int n; /* length of string read */ - char buf[MAXLINE]; /* string read from client */ - char response[22] = "I hear ya for shizzle"; - memset(buf, 0, MAXLINE); - n = read(sockfd, buf, MAXLINE); - if (n > 0) { - printf("%s\n", buf); - if (write(sockfd, response, 22) > 22) { - err_sys("write error"); - } - } - if (n < 0) { - err_sys("respond: read error"); - } -} - int main() { - int listenfd, connfd; - int opt; + int listenfd, connfd; + int opt; + int n; /* length of string read */ + char buff[MAXLINE]; + char buf[MAXLINE]; /* string read from client */ + char response[22] = "I hear ya for shizzle"; struct sockaddr_in cliAddr, servAddr; - char buff[MAXLINE]; - socklen_t cliLen; + + socklen_t cliLen; /* find a socket , 0 for using TCP option */ listenfd = socket(AF_INET, SOCK_STREAM, 0); @@ -109,8 +92,19 @@ int main() printf("Connection from %s, port %d\n", inet_ntop(AF_INET, &cliAddr.sin_addr, buff, sizeof(buff)), ntohs(cliAddr.sin_port)); - - respond(connfd); + + /* empty response buffer to avoid unexpected output */ + memset(buf, 0, MAXLINE); + n = read(connfd, buf, MAXLINE); + if (n > 0) { + printf("%s\n", buf); + if (write(connfd, response, 22) > 22) { + err_sys("write error"); + } + } + if (n < 0) { + err_sys("respond: read error"); + } /* closes the connections after responding */ if (close(connfd) == -1) { err_sys("close error");