Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: prosebas en 12 Enero 2021, 06:42 am



Título: Socket c++
Publicado por: prosebas en 12 Enero 2021, 06:42 am
Hola que tal , estoy desarrollando un código como pasatiempo en el que creo un servidor  local y cliente.El servidor unicaménte lo tengo codificado para correr en linux mientras el cliente lo tengo tanto para win y linux, sin embargo, cuando ejecuto el cliente dentro de  un SO linux si es posible conectarse caso contrario al de Win en el que me dice que no es posible conectarse. ¿Alguno sabe si es un problema de compatiblidad o simplemente un problema en mi código ?

//SERVIDOR
Código
  1. #include <iostream>
  2. #include <sys/socket.h>
  3. #include <arpa/inet.h> //inet_addr
  4. #include <netdb.h>     //Define hostent struct
  5. #include <unistd.h>    //close socket
  6. #include <string.h>
  7. #define BUFFER 1024
  8. using namespace std;
  9. int main(int argc, char **argv)
  10. {
  11.    //Create socket
  12.    int listening;
  13.    listening = socket(AF_INET, SOCK_STREAM, 0);
  14.    if (listening == -1)
  15.    {
  16.        cout << "Can't create socket" << endl;
  17.        return 0;
  18.    }
  19.    //Set the server
  20.    struct sockaddr_in server;
  21.    server.sin_family = AF_INET;
  22.    server.sin_port = htons(atoi(argv[1]));
  23.    server.sin_addr.s_addr = INADDR_ANY;
  24.    //Assign to server a unique telephone number
  25.    bind(listening, (struct sockaddr *)&server, sizeof(server));
  26.    //Listening ...
  27.    cout << "Waiting for connections ... " << endl;
  28.    listen(listening, SOMAXCONN);
  29.    //Wait for connections
  30.    struct sockaddr_in client;
  31.    int sizeClient = sizeof(client);
  32.    //Accept client
  33.    int clientSocket = accept(listening, (struct sockaddr *)&client, (socklen_t *)&sizeClient);
  34.    if (clientSocket == -1)
  35.    {
  36.        cout << "Can't connect with the client" << endl;
  37.        return 0;
  38.    }
  39.    char welcome[BUFFER];
  40.    memset(welcome, 0, BUFFER);
  41.    strcpy(welcome, "Welcome");
  42.    send(clientSocket, welcome, BUFFER, 0);
  43.    cout << "Connected!" << endl;
  44.    bool bandera = true;
  45.    while (bandera)
  46.    {
  47.        cout << "(*)";
  48.        cin.getline(welcome, BUFFER);
  49.        if (strcmp(welcome, "SHUTDOWN") == 0)
  50.        {
  51.            send(clientSocket, welcome, BUFFER, 0);
  52.            bandera = false;
  53.        }
  54.        else
  55.        {
  56.            send(clientSocket, welcome, BUFFER, 0);
  57.        }
  58.    }
  59.    close(listening);
  60. }
  61.  
  62.  



//CLIENTE
Código
  1. #if defined _WIN32
  2. #include <iostream>
  3. using namespace std;
  4. #include <WS2tcpip.h>
  5. #pragma comment(lib, "ws2_32.lib")
  6. int inet_pton(int af, const char *src, void *dst);
  7. #else
  8. #include <iostream>
  9. #include <sys/socket.h>
  10. #include <arpa/inet.h> //inet_addr
  11. #include <string.h>
  12. #include <unistd.h> //close socket
  13. #endif
  14. #define BUFFER 2048
  15. using namespace std;
  16. int main()
  17. {
  18. #if defined(_WIN32)
  19.    {
  20.        WSADATA winsock;
  21.        WORD word = MAKEWORD(2, 2);
  22.        int winStatus = WSAStartup(word, &winsock);
  23.        if (winStatus != 0)
  24.        {
  25.            cout << "Can't intialize Winsock on windows" << endl;
  26.            return 0;
  27.        }
  28.    }
  29. #endif
  30.    int socket_ = socket(AF_INET, SOCK_STREAM, 0);
  31.    if (socket_ == -1)
  32.    {
  33.        cout << "Can't create the socket" << endl;
  34.        return 0;
  35.    }
  36.    //Set socket
  37.    sockaddr_in client;
  38.    client.sin_port = htons(8080);
  39.    client.sin_family = AF_INET;
  40. #if (_WIN32)
  41.    string ipAdress="127.0.0.1";
  42.    inet_pton(AF_INET, ipAdress.c_str(), &client.sin_addr);
  43. #else
  44.    client.sin_addr.s_addr = inet_addr("127.0.0.1");
  45. #endif
  46.    //Connect
  47.    int connecting = connect(socket_, (struct sockaddr *)&client, sizeof(client));
  48.    if (connecting == -1)
  49.    {
  50.        cout << "You can't connect" << endl;
  51.        return 0;
  52.    }
  53.    char rcvd[BUFFER];
  54.    memset(rcvd, 0, BUFFER);
  55.    recv(socket_, rcvd, BUFFER, 0);
  56.    cout << rcvd << endl;
  57.    bool bandera = true;
  58.    while (bandera)
  59.    {
  60.        memset(rcvd, 0, BUFFER);
  61.        recv(socket_, rcvd, BUFFER, 0);
  62.        if (strcmp(rcvd, "SHUTDOWN") == 0)
  63.        {
  64.  
  65. #if defined(_WIN32)
  66.            WSACleanup();
  67. #endif
  68.            //close(socket_);
  69.            bandera = false;
  70.            cout << "The connection was closed" << endl;
  71.        }
  72.        else
  73.            cout << "*) " << rcvd << endl;
  74.    }
  75. }
  76. int inet_pton(int af, const char *src, void *dst)
  77. {
  78. #if (_WIN32)
  79.    struct sockaddr_storage ss;
  80.    int size = sizeof(ss);
  81.    char src_copy[INET6_ADDRSTRLEN + 1];
  82.  
  83.    ZeroMemory(&ss, sizeof(ss));
  84.    /* stupid non-const API */
  85.    strncpy(src_copy, src, INET6_ADDRSTRLEN + 1);
  86.    src_copy[INET6_ADDRSTRLEN] = 0;
  87.  
  88.    if (WSAStringToAddress(src_copy, af, NULL, (struct sockaddr *)&ss, &size) == 0)
  89.    {
  90.        switch (af)
  91.        {
  92.        case AF_INET:
  93.            *(struct in_addr *)dst = ((struct sockaddr_in *)&ss)->sin_addr;
  94.            return 1;
  95.        case AF_INET6:
  96.            *(struct in6_addr *)dst = ((struct sockaddr_in6 *)&ss)->sin6_addr;
  97.            return 1;
  98.        }
  99.    }
  100. #endif
  101.    return 0;
  102. }
  103.  


MOD: Modificadas las etiquetas de Código GeSHi para el lenguaje C++


Título: Re: Socket c++
Publicado por: @XSStringManolo en 12 Enero 2021, 22:15 pm
Al servidor le da igual desde donde se le mande la petición. Asique tiene que ser problema del código de tu cliente.

Busca algún ejemplo de cliente por la web que te funcione. Yo suelo usar ncat para debuggear este tipo de aplicaciones.