Advertisement
tomasaccini

Untitled

Jul 22nd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.16 KB | None | 0 0
  1. #include <time.h>
  2. #include <stdlib.h>
  3.  
  4. int conectar_cliente(const char* ip, const char* port) {
  5.     struct addrinfo* socketinfo;
  6.     getaddrinfo(ip, port, NULL, socketinfo);
  7.     // Asumo que la primera conexion es valida
  8.     int skt = Socket(socketinfo->ai_family, socketinfo->ai_socktype, socketinfo->ai_protocol);
  9.     // Asumo que se conecta bien
  10.     connect(skt, socketinfo->ai_addr, socketinfo->ai_addrlen);
  11.     freeaddrinfo(socketinfo);
  12.     return skt;
  13. }
  14.  
  15. char* buffer_aleatorio(size_t tam) {
  16.     char* p = (char*)malloc(sizeof(char) * (tam + 1));
  17.     if (!p) return NULL;
  18.     p[tam] = '\0';
  19.     srand(time(NULL));   // should only be called once
  20.     for (size_t i = 0; i < tam; ++i) {
  21.         p[i] = rand() % 128;
  22.     }
  23.     return p;
  24. }
  25.  
  26. int main(int argc, char* argv[]) {
  27.     int skt = conectar_cliente(argv[1], argv[2]);
  28.     for (size_t i = 0; i < 50; i++) {
  29.         char* buf = buffer_aleatorio(10);
  30.         int enviados = 0;
  31.         while (enviados < 10) {
  32.             // Supongo que no hay errores ni se cierra la conexión del otro lado
  33.             enviador += send(skt, buf, 10, NULL);
  34.         }
  35.         free(buf);
  36.     }
  37.     return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement