diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..1c5c6ff --- /dev/null +++ b/src/main.c @@ -0,0 +1,56 @@ +#include<stdio.h> +#include<arpa/inet.h> +#include<sys/types.h> +#include<sys/socket.h> +#include<netdb.h> +#include<string.h> +#include<stdlib.h> +#include<errno.h> + +int +main() +{ + int gai_status, b_status; + int socketfd; + struct addrinfo hints, *res; + char ipstr[INET6_ADDRSTRLEN]; + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_PASSIVE; + + gai_status = getaddrinfo(NULL, "9123", &hints, &res); + if (gai_status != 0) + { + fprintf(stderr, "gai error: %s\n", gai_strerror(gai_status)); + exit(1); + } + + socketfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (socketfd < 0) + { + fprintf(stderr, "socket error: %d\n", errno); + exit(1); + } + + b_status = bind(socketfd, res->ai_addr, res->ai_addrlen); + if (b_status < 0) + { + fprintf(stderr, "bind error: %d\n", errno); + exit(1); + } + + inet_ntop(res->ai_family, &(((struct sockaddr_in *) res->ai_addr)->sin_addr), ipstr, sizeof ipstr); + if (ipstr == NULL) + { + fprintf(stderr, "string error: %d\n", errno); + exit(1); + } + + printf("Endereço: %s", ipstr); + + freeaddrinfo(res); + + return 0; +} |
