diff options
author | Oskar <[email protected]> | 2024-05-30 17:41:09 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-05-30 17:41:09 +0200 |
commit | b0d494f15524fb8dc037c1fce2cb72c0556bb36e (patch) | |
tree | ed97da4681f4a6d5f30673b649384d105725cf10 | |
parent | 26daaa717074f6c383e05efc070e63f24609fb5b (diff) |
added for loop to loop through the linked list from getaddrinfo not sure why exactly i need to do it if i listen on my own port but the book did it so i did it. I also learned how to see what IP and what port the client connects from
-rw-r--r-- | listen.c | 51 |
1 files changed, 33 insertions, 18 deletions
@@ -12,15 +12,16 @@ #include <netinet/in.h> // int listen(int sockfd, int backlog); - +#define __FAIL EXIT_FAILURE #define BACKLOG 2 int main () { - int sockfd; // socket fd + int sockfd = -1; // socket fd int gai_result; // result from getaddrinfo - int bind_result; // result from bind + int bind_result = -1; // result from bind struct addrinfo *res; // Pointer to linked list sent back by gai + struct addrinfo *p; // for linked list stuff struct addrinfo hints; // Options we pass to gai memset(&hints, 0, sizeof(hints)); // We set this struct to 0 and then give it all the options we want below hints.ai_family = AF_INET; @@ -29,22 +30,29 @@ int main () { gai_result = getaddrinfo(NULL, "62000", &hints, &res); // NULL because we are going to listen if (gai_result != 0) { - fprintf(stderr, "gai_result: %d\n", gai_result); - exit(1); + fprintf(stderr, "gai_result: %d\n", gai_result); + exit(__FAIL); } + + for (p = res ; p != NULL ; p = p->ai_next) { // Loop - sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); // Socket, get the family, socktype and protocol. - if (sockfd == -1) { - fprintf(stderr, "sockfd: %d\n", sockfd); - exit(1); - } + sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol); // Socket, get the family, socktype and protocol. + if (sockfd == -1) { + fprintf(stderr, "sockfd: %d\n", sockfd); + continue; + } - bind_result = bind(sockfd, res->ai_addr, res->ai_addrlen); // Bind to socket fd. pass our own address (ai_addr) and the length - if (bind_result != 0) { - fprintf(stderr, "bind_result: %d\n", bind_result); - exit(1); + bind_result = bind(sockfd, p->ai_addr, p->ai_addrlen); // Bind to socket fd. pass our own address (ai_addr) and the length + if (bind_result != 0) { + fprintf(stderr, "bind_result: %d\n", bind_result); + close(sockfd); + continue; + } } - + if (sockfd == -1 || bind_result == -1) { + exit(__FAIL); + } + freeaddrinfo(res); // free the info because we dont need it anymore fprintf(stdout, "Waiting for a connection...\n"); @@ -58,11 +66,18 @@ int main () { fprintf(stdout, "Connection is being established...\n"); if (newfd == -1) { fprintf(stderr, "newfd: %d\n", newfd); - exit(1); + exit(__FAIL); } - fprintf(stdout, "Connection established!\n"); - char *msg = "Server says hi!\n"; + char host_ip[NI_MAXHOST]; + char port_port[NI_MAXSERV]; + int gni; + gni = getnameinfo((struct sockaddr *)&their_addr, addr_size, host_ip, sizeof(host_ip), port_port, sizeof(port_port), NI_NUMERICSERV | NI_NUMERICHOST); + + if (gni == 0) { + fprintf(stdout, "Client %s %s just connected!\n", host_ip, port_port); + } + char *msg = "Connection established!\n"; int len; int bytes_sent; len = strlen(msg); |