Advertisement
tomasaccini

Untitled

Jul 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <chrono>
  2. #include <iostream>
  3.  
  4. int aceptar_cliente(const char* ip, const char* port) {
  5.     struct addrinfo* socketinfo;
  6.     getaddrinfo(ip, port, NULL, socketinfo);
  7.     // Asumo que la primera conexión es válida
  8.     int skt = socket(socketinfo->ai_family, socketinfo->ai_socktype, socketinfo->ai_protocol);
  9.     // Asumo que sale todo bien
  10.     bind(skt, socketinfo->ai_addr, socketinfo->ai_addrlen);
  11.     freeaddrinfo(socketinfo);
  12.     listen(skt, 10);
  13.     return accept(skt);
  14. }
  15.  
  16. int main(int argc, char* argv[]) {
  17.     int skt = aceptar_cliente(argv[1], argv[2]); // ip y puerto
  18.     char buf[256];
  19.     while (true) {
  20.         auto t_i = std::chrono::system_clock::now();
  21.         int recibidos = recv(skt, &buf[0], 256);
  22.         auto t_f = std::chrono::system_clock::now();
  23.         std::chrono::duration<int> diff = end-start;
  24.         if (diff > 3000) {
  25.             break;
  26.         }
  27.         if (recibidos <= 0) {
  28.             break
  29.         } else {
  30.             for(int i = 0; i < recibidos; i++) {
  31.                 std::cout << buf[i];
  32.             }
  33.         }
  34.     }
  35.     shutdown(skt, SHUT_RDWT);
  36.     close(skt);
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement