Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <stdlib.h>
- #define MAX_CLIENT 5
- #define MAX_BUFFER 512
- int main(void)
- {
- struct addrinfo hints;
- struct addrinfo* result;
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_PASSIVE;
- getaddrinfo(NULL, "1111", &hints, &result);
- int listener_socket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
- int yes = 1;
- if (setsockopt(listener_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == 1) {
- perror("setsockopt");
- exit(1);
- }
- bind(listener_socket, result->ai_addr, result->ai_addrlen);
- listen(listener_socket, MAX_CLIENT);
- fd_set master;
- fd_set readFds;
- FD_ZERO(&master);
- FD_SET(listener_socket, &master);
- int fdmax = listener_socket;
- int received_bytes = 0;
- struct addrinfo new_client_address;
- socklen_t new_client_address_length = sizeof(new_client_address);
- char address[MAX_BUFFER];
- char buffer[MAX_BUFFER];
- struct addrinfo client_hints;
- memset(&client_hints, 0, sizeof client_hints);
- client_hints.ai_family = AF_UNSPEC;
- client_hints.ai_socktype = SOCK_STREAM;
- struct addrinfo *client_res;
- int i,j;
- int new_client;
- while (1) {
- memset(&buffer, 0, MAX_BUFFER);
- readFds = master;
- select(fdmax+1, &readFds, NULL, NULL, NULL);
- for (i = 0; i <= fdmax; i++) {
- if (FD_ISSET(i, &readFds)) {
- if (i == listener_socket) {
- new_client = accept(listener_socket, &new_client_address, &new_client_address_length);
- FD_SET(new_client, &master);
- if (new_client > fdmax) {
- fdmax = new_client;
- }
- int dsa = getaddrinfo(new_client_address.ai_addr, NULL, &client_hints, &client_res);
- printf("asd: %d\n", dsa);
- if (new_client_address.ai_family == AF_INET) {
- struct sockaddr_in* ipv4 = (struct sockaddr_in*)new_client_address.ai_addr;
- inet_ntop(AF_INET,&(ipv4->sin_addr), &address, sizeof address );
- } else {
- struct sockaddr_in6* ipv6 = (struct sockaddr_in6*) new_client_address.ai_addr;
- inet_ntop(AF_INET6,&(ipv6->sin6_addr), &address, sizeof address );
- }
- puts(address);
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement