elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Socket c++
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Socket c++  (Leído 2,287 veces)
prosebas

Desconectado Desconectado

Mensajes: 17


Ver Perfil
Socket c++
« 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++


« Última modificación: 12 Enero 2021, 20:58 pm por K-YreX » En línea

@XSStringManolo
Hacker/Programador
Colaborador
***
Desconectado Desconectado

Mensajes: 2.397


Turn off the red ligth


Ver Perfil WWW
Re: Socket c++
« Respuesta #1 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.


En línea

Mi perfil de patrocinadores de GitHub está activo! Puedes patrocinarme para apoyar mi trabajo de código abierto 💖

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Raw socket
Programación Visual Basic
yeikos 3 2,443 Último mensaje 28 Agosto 2007, 15:53 pm
por elmaro
socket en C
Programación C/C++
mapers 4 6,055 Último mensaje 28 Marzo 2011, 18:22 pm
por Garfield07
socket ??
Programación C/C++
<<<-Basura->>> 3 2,451 Último mensaje 6 Agosto 2011, 07:40 am
por <<<-Basura->>>
Multithread Socket (Thread per Socket)not Complete « 1 2 »
Programación Visual Basic
ntaryl 12 8,898 Último mensaje 10 Febrero 2012, 18:42 pm
por ntaryl
Python 3.7 socket socket.gaierror errno7 cliente Android.
Scripting
@XSStringManolo 2 2,750 Último mensaje 28 Diciembre 2019, 00:09 am
por @XSStringManolo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines