proper cleanup in psk/client-tcp.c

pull/265/head
Lealem Amedie 2021-07-06 16:44:11 -06:00
parent c8d596269d
commit 0c0e156f7b
1 changed files with 11 additions and 7 deletions

View File

@ -48,7 +48,7 @@ int main(int argc, char **argv)
/* must include an ip address or this will flag */ /* must include an ip address or this will flag */
if (argc != 2) { if (argc != 2) {
printf("Usage: tcpClient <IPaddress>\n"); printf("Usage: tcpClient <IPaddress>\n");
return 1; return -1;
} }
/* create a stream socket using tcp,internet protocal IPv4, /* create a stream socket using tcp,internet protocal IPv4,
@ -66,35 +66,39 @@ int main(int argc, char **argv)
if (ret != 1) { if (ret != 1) {
printf("Not a Valid network address"); printf("Not a Valid network address");
return 1; return -1;
} }
/* attempts to make a connection on a socket */ /* attempts to make a connection on a socket */
ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)); ret = connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
if (ret != 0) { if (ret != 0) {
return 1; ret = -1;
goto exit;
} }
/* takes inputting string and outputs it to the server */ /* takes inputting string and outputs it to the server */
/* write string to the server */ /* write string to the server */
if (write(sockfd, sendline, strlen(sendline)) != strlen(sendline)) { if (write(sockfd, sendline, strlen(sendline)) != strlen(sendline)) {
printf("Write Error to Server\n"); printf("Write Error to Server\n");
return 1; ret = -1;
goto exit;
} }
/* flags if the server stopped before the client could end */ /* flags if the server stopped before the client could end */
if (read(sockfd, recvline, MAXLINE) == 0) { if (read(sockfd, recvline, MAXLINE) == 0) {
printf("Client: Server Terminated Prematurely!\n"); printf("Client: Server Terminated Prematurely!\n");
return 1; ret = -1;
goto exit;
} }
printf("Server Message: %s\n", recvline); printf("Server Message: %s\n", recvline);
ret = 0;
exit:
/* close socket and connection */ /* close socket and connection */
close(sockfd); close(sockfd);
ret = 0;
return ret; return ret;
} }