diff options
author | Matt Strapp <strap012@umn.edu> | 2020-12-05 11:12:59 -0600 |
---|---|---|
committer | Matt Strapp <strap012@umn.edu> | 2020-12-05 11:12:59 -0600 |
commit | d0b328814ac21096df5aaf7501536f8f72033070 (patch) | |
tree | fb79350ea9c5415c839927261448f2c9b4fd9520 /P4 | |
parent | Start P4 (diff) | |
download | csci4061-d0b328814ac21096df5aaf7501536f8f72033070.tar csci4061-d0b328814ac21096df5aaf7501536f8f72033070.tar.gz csci4061-d0b328814ac21096df5aaf7501536f8f72033070.tar.bz2 csci4061-d0b328814ac21096df5aaf7501536f8f72033070.tar.lz csci4061-d0b328814ac21096df5aaf7501536f8f72033070.tar.xz csci4061-d0b328814ac21096df5aaf7501536f8f72033070.tar.zst csci4061-d0b328814ac21096df5aaf7501536f8f72033070.zip |
P4 working
Diffstat (limited to 'P4')
-rw-r--r-- | P4/util.c | 12 |
1 files changed, 9 insertions, 3 deletions
@@ -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); } |