Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <unistd.h>
  6. #include <cstring>
  7. #include <netinet/in.h>
  8. #include <arpa/inet.h>
  9.  
  10. using namespace std;
  11.  
  12. int main() {
  13.     int socketId = socket(AF_INET, SOCK_STREAM, 0);
  14.     sockaddr_in sa = {AF_INET, htons(8888), INADDR_ANY};
  15.     const int reuseProp = 1;
  16.     setsockopt(socketId, SOL_SOCKET, SO_REUSEADDR, &reuseProp, sizeof(reuseProp));
  17.  
  18.     if (bind(socketId, (sockaddr*) &sa, sizeof(sa)) != 0) {
  19.         perror("An error occured");
  20.     }
  21.     int client;
  22.     listen(socketId, 10);
  23.     while (true) {
  24.         sockaddr_in connection;
  25.         socklen_t conSize = sizeof(connection);
  26.         client = accept(socketId, (sockaddr*) &connection, &conSize);
  27.         printf("%s:%d\n", inet_ntoa(connection.sin_addr), htons(connection.sin_port));
  28.     }
  29.  
  30. //    char buffer[256] = "Sieci Komputerowe II";
  31. //    write(connection, buffer, strlen(buffer));
  32.     shutdown(client, SHUT_RDWR);
  33.     close(socketId);
  34.     return 0;
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement