From d0b328814ac21096df5aaf7501536f8f72033070 Mon Sep 17 00:00:00 2001 From: Matt Strapp Date: Sat, 5 Dec 2020 11:12:59 -0600 Subject: P4 working --- P4/util.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/P4/util.c b/P4/util.c index 2e2be63..77342c8 100644 --- a/P4/util.c +++ b/P4/util.c @@ -16,7 +16,7 @@ #include "util.h" //Global socket for all things -int socket; +int sock; /********************************************** * init @@ -29,17 +29,23 @@ int socket; - if init encounters any errors, it will call exit(). ************************************************/ void init(int port) { + if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) { + perror("Cannot create socket"); + exit(EXIT_FAILURE); + } + //Socket describer struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(port); //Allow port to be released int enable = 1; - if (setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (char *)&enable, sizeof(int)) == -1) { + if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&enable, sizeof(int)) == -1) { perror("Cannot set socket option"); exit(EXIT_FAILURE); } - if (bind(socket, (struct sockaddr*) &addr, sizeof(addr)) == -1) { + //Bind socket and open the port + if (bind(sock, (struct sockaddr*) &addr, sizeof(addr)) == -1) { perror("Cannot bind socket"); exit(EXIT_FAILURE); } -- cgit v1.2.3