Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Quando passo il SOCKET alla funzione mi da errore sul bind
Forum - C/C++ - Quando passo il SOCKET alla funzione mi da errore sul bind

Avatar
Gemini (Normal User)
Rookie


Messaggi: 33
Iscritto: 04/10/2021

Segnala al moderatore
Postato alle 14:15
Sabato, 16/04/2022
Salve a tutti e auguri posticipati per pasqua ^_^....
ho un problema con le socket di windows.... non capisco perchè se voglio passare un socket a una funzione dentro una classe mi da errore 10038... leggendo su google mi dice che io non passo il socket al bind.... cmq per farvi capire meglio ecco i source

CLASSE NETWORK.H
Codice sorgente - presumibilmente C++

  1. #include <winsock2.h>
  2. #include <iostream>
  3.  
  4. #pragma comment(lib, "ws2_32.lib")
  5.  
  6. class Networks
  7. {
  8.  
  9.     public:
  10.        
  11.         //Normal Socket
  12.         bool ServerTCP(SOCKET m_socket, SOCKET a_socket, const char *ip, int port);
  13.         bool ClientTCP(SOCKET m_socket, const char *ip, int port);
  14.         //WSASocket
  15.         bool WSAServerTCP(SOCKET m_socket, SOCKET a_socket, const char *ip, int port);
  16.         bool WSAClientTCP(SOCKET m_socket, const char *ip, int port);
  17.  
  18.         bool Send(SOCKET s_socket, const char *buffer);
  19.         std::string Recv(SOCKET r_socket);
  20.    
  21.         bool isConnected = false;
  22.  
  23.    
  24.     private:
  25.  
  26.         //Variabili Socket Private
  27.         WORD DllVersion = MAKEWORD(2,2);
  28.         WSADATA wsaData;
  29.         sockaddr_in si;
  30.         sockaddr_in a_si;
  31.  
  32.         //SOCKET m_socket;
  33.         //SOCKET a_socket;
  34.  
  35.         bool is_Server = false;
  36.         bool is_Client = false;
  37.         bool is_WSASocket = false;
  38.  
  39.         //Metodi Socket Privati
  40.         bool InitSocket();
  41.         bool CreateSocket(SOCKET m_socket, const char *ip, int port);
  42.         bool CloseSocket(SOCKET m_socket, SOCKET a_socket);
  43.  
  44.         //Metodi Client Socket
  45.         bool ConnectSocket(SOCKET m_socket);
  46.  
  47.         //Metodi Server Socket
  48.         bool BindSocket(SOCKET m_socket);
  49.         bool AcceptSocket(SOCKET m_socket, SOCKET a_socket);
  50.  
  51.    
  52.  
  53.  
  54. };



CLASSE NETWORK:CPP
Codice sorgente - presumibilmente C++

  1. #include "Networks.h"
  2.  
  3.  
  4. //===============================================//
  5. //=                ServerTCP                    =//
  6. //===============================================//
  7. bool Networks::ServerTCP(SOCKET m_socket, SOCKET a_socket, const char *ip, int port)
  8. {
  9.     is_Server = true;
  10.  
  11.     if(!InitSocket())
  12.         return false;
  13.  
  14.     if(!CreateSocket(m_socket, ip, port))
  15.         return false;
  16.        
  17.     if(!BindSocket(m_socket))
  18.         return false;
  19.  
  20.     //Qui forse ci vuole un while true
  21.     if(!AcceptSocket(m_socket, a_socket))
  22.         return false;
  23.  
  24.    
  25.     isConnected = true;
  26.     return true;
  27. }
  28.  
  29. //===============================================//
  30. //=                ClientTCP                    =//
  31. //===============================================//
  32. bool Networks::ClientTCP(SOCKET m_socket, const char *ip, int port)
  33. {
  34.     is_Client = true;
  35.  
  36.     if(!InitSocket())
  37.         return false;
  38.  
  39.     if(!CreateSocket(m_socket, ip, port))
  40.         return false;
  41.  
  42.      //Qui forse ci vuole un while  
  43.     if(!ConnectSocket(m_socket))
  44.         return false;
  45.  
  46.    
  47.     isConnected = true;
  48.     return true;
  49. }
  50.  
  51. //===============================================//
  52. //=                WSAServerTCP                 =//
  53. //===============================================//
  54. bool Networks::WSAServerTCP(SOCKET m_socket, SOCKET a_socket, const char *ip, int port)
  55. {
  56.     is_WSASocket = true;
  57.     is_Server = true;
  58.    
  59.     if(!InitSocket())
  60.         return false;
  61.  
  62.     if(!CreateSocket(m_socket, ip, port))
  63.         return false;
  64.        
  65.     if(!BindSocket(m_socket))
  66.         return false;
  67.  
  68.     //Qui forse ci vuole un while true
  69.     if(!AcceptSocket(m_socket, a_socket))
  70.         return false;
  71.  
  72.    
  73.     isConnected = true;
  74.     return true;
  75. }
  76.  
  77. //===============================================//
  78. //=                WSAClientTCP                 =//
  79. //===============================================//
  80. bool Networks::WSAClientTCP(SOCKET m_socket, const char *ip, int port)
  81. {
  82.     is_WSASocket = true;
  83.     is_Client = true;
  84.  
  85.     if(!InitSocket())
  86.         return false;
  87.  
  88.     if(!CreateSocket(m_socket, ip, port))
  89.         return false;
  90.  
  91.      //Qui forse ci vuole un while  
  92.     if(!ConnectSocket(m_socket))
  93.         return false;
  94.  
  95.    
  96.     isConnected = true;
  97.     return true;
  98. }
  99.  
  100.  
  101.  
  102.  
  103. //===============================================//
  104. //=                initWSASocket                =//
  105. //===============================================//
  106. bool Networks::InitSocket()
  107. {
  108.     int WsaResult = WSAStartup(DllVersion, &wsaData);
  109.     if(WsaResult != 0)
  110.     {
  111.         std::cout << "Fallito WSAStartup()." << std::endl;
  112.                 return false;
  113.     }
  114.        
  115.     return true;
  116. }
  117.  
  118.  
  119.  
  120. //===============================================//
  121. //=                createWSASocket              =//
  122. //===============================================//
  123. bool Networks::CreateSocket(SOCKET m_socket, const char *ip, int port)
  124. {
  125.     //Normal Socket
  126.     if(is_WSASocket == false)
  127.         m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  128.     else if(is_WSASocket == true) //WSA SOCKET
  129.         m_socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, (unsigned int)NULL, (unsigned int)NULL);
  130.  
  131.     if(m_socket == INVALID_SOCKET)
  132.         return false;
  133.  
  134.     memset(&si, 0, sizeof(si));
  135.  
  136.     if(is_Server == true)
  137.     {
  138.         si.sin_family = AF_INET;
  139.         if(ip == NULL)
  140.                     si.sin_addr.s_addr = INADDR_ANY;
  141.         else
  142.             si.sin_addr.s_addr = inet_addr(ip);
  143.                 si.sin_port = htons(port);
  144.     }
  145.     else if(is_Client == true)
  146.     {
  147.         si.sin_family = AF_INET;
  148.         si.sin_addr.s_addr = inet_addr(ip);
  149.         si.sin_port = htons(port);
  150.     }
  151.     else
  152.     {
  153.         std::cout << "Il socket non e' ne di tipo server ne di tipo client" << std::endl;
  154.         return false;
  155.     }
  156.    
  157.     return true;
  158. }
  159.  
  160. //===============================================//
  161. //=                closeWSASocket               =//
  162. //===============================================//
  163. bool Networks::CloseSocket(SOCKET m_socket, SOCKET a_socket)
  164. {  
  165.     if(is_Server == true)
  166.     {
  167.         if(!closesocket(a_socket))
  168.             return false;
  169.     }
  170.  
  171.     if(!closesocket(m_socket))
  172.         return false;
  173.    
  174.     if(!WSACleanup())
  175.         return false;
  176.  
  177.     return true;
  178. }
  179.  
  180. //===============================================//
  181. //=             ConnectSocket                   =//
  182. //===============================================//
  183. bool Networks::ConnectSocket(SOCKET m_socket)
  184. {
  185.     int ConnResult;
  186.      //Normal Socket
  187.     if(is_WSASocket == false)
  188.         ConnResult = connect(m_socket, (struct sockaddr*)&si, sizeof(si));
  189.     else if(is_WSASocket == true) //WSA SOCKET  
  190.         ConnResult = WSAConnect(m_socket, (struct sockaddr*)&si, sizeof(si), NULL, NULL, NULL, NULL);
  191.    
  192.     if(ConnResult == SOCKET_ERROR)
  193.     {
  194.         std::cout << "errore %d"<< WSAGetLastError() << std::endl;
  195.         CloseSocket(m_socket, 0);
  196.         return false;
  197.     }
  198.  
  199.     return true;
  200. }
  201.  
  202.  
  203. //===============================================//
  204. //=             bindSocket                      =//
  205. //===============================================//
  206. bool Networks::BindSocket(SOCKET m_socket)
  207. {
  208.     if (bind(m_socket, (struct sockaddr*)&si, sizeof(si)) == SOCKET_ERROR)
  209.         {
  210.         std::cout << "Fallito bindSocket(). Errore: " << WSAGetLastError() << std::endl;
  211.         CloseSocket(m_socket, 0);
  212.                 return false;
  213.         }
  214.  
  215.         //Listen
  216.         if(listen(m_socket, 3) == SOCKET_ERROR)
  217.     {
  218.         std::cout << "Fallito listen(). Errore: " << WSAGetLastError() << std::endl;
  219.         CloseSocket(m_socket, 0);
  220.                 return false;
  221.     }
  222.         return true;
  223. }
  224.  
  225. //===============================================//
  226. //=             acceptWSASocket                 =//
  227. //===============================================//
  228. bool Networks::AcceptSocket(SOCKET m_socket, SOCKET a_socket)
  229. {
  230.     int len = sizeof(a_si);
  231.     int AcceptResult;
  232.  
  233.     //Normal Socket
  234.     if(is_WSASocket == false)
  235.         a_socket = accept(m_socket, (struct sockaddr*)&a_si, &len);
  236.     else if(is_WSASocket == true) //WSA SOCKET  
  237.         a_socket = WSAAccept(m_socket, (struct sockaddr*)&a_si, &len,NULL,0);
  238.    
  239.     /*if((a_socket = WSAAccept(m_socket, (struct sockaddr*)&a_si, &len,NULL,0)) == INVALID_SOCKET)
  240.     {
  241.         std::cout << "Fallito WSAAccept(). Errore: " << WSAGetLastError() << std::endl;
  242.         CloseSocket();
  243.                 return false;
  244.     }*/
  245.     if(a_socket == INVALID_SOCKET)
  246.     {
  247.         std::cout << "Fallito WSAAccept(). Errore: " << WSAGetLastError() << std::endl;
  248.         CloseSocket(m_socket, a_socket);
  249.                 return false;
  250.     }
  251.  
  252.     return true;
  253. }
  254.  
  255.  
  256.  
  257. //===============================================//
  258. //=                Send                         =//
  259. //===============================================//
  260. bool Networks::Send(SOCKET s_socket, const char *sendbuffer)
  261. {
  262.     int ResultSend;
  263.  
  264.     if(is_Server == true)
  265.     {
  266.         ResultSend = send(s_socket, sendbuffer, (int) strlen(sendbuffer), 0);
  267.     }
  268.        
  269.     else if(is_Client == true)
  270.     {
  271.         ResultSend = send(s_socket, sendbuffer, (int) strlen(sendbuffer), 0);
  272.     }
  273.        
  274.     else
  275.     {
  276.         std::cout << "Nessun socket specificato!!!" << std::endl;
  277.         return false;
  278.     }
  279.  
  280.     if(ResultSend == SOCKET_ERROR)
  281.     {
  282.         std::cout << "Errore Send(): " << WSAGetLastError() << std::endl;
  283.         CloseSocket(s_socket, 0);
  284.         return false;
  285.     }
  286.  
  287.     std::cout << "Messaggio inviato!" << std::endl;
  288.  
  289.     return true;
  290. }
  291.  
  292.  
  293. //===============================================//
  294. //=                Recv                         =//
  295. //===============================================//
  296. std::string Networks::Recv(SOCKET r_socket)
  297. {
  298.     const int recvbufflen = 1024;
  299.     char recvbuffer[recvbufflen];
  300.     int ResultRecv;
  301.  
  302.     do
  303.     {
  304.         if(is_Server == true)
  305.         {
  306.             ResultRecv = recv(r_socket,recvbuffer, recvbufflen, 0);
  307.         }
  308.            
  309.         else if(is_Client == true)
  310.         {
  311.             ResultRecv = recv(r_socket,recvbuffer, recvbufflen, 0);
  312.         }
  313.            
  314.         else
  315.         {
  316.             std::cout << "Nessun socket specificato!!!" << std::endl;
  317.             return "false";
  318.         }
  319.        
  320.         if(ResultRecv > 0)
  321.         {
  322.             recvbuffer[ResultRecv] = 0;
  323.             //std::cout << "byte ricevuti" << ResultRecv << " " << recvbuffer << std::endl;
  324.             return recvbuffer;
  325.         }
  326.         else if(ResultRecv == 0)
  327.             std::cout << "Connesione chiusa!" << std::endl;
  328.         else
  329.             std::cout << "Error Recv(): " << WSAGetLastError() << std::endl;
  330.  
  331.     } while (ResultRecv > 0);
  332.    
  333.     return "true";
  334. }





//===================== CLIENT/SERVER =========================//

SERVER:CPP
Codice sorgente - presumibilmente C/C++

  1. #include "Networks.h"
  2.  
  3. int main()
  4. {
  5.  
  6.     Networks ntw;
  7.     SOCKET m_socket, a_socket;
  8.  
  9.     if(!ntw.ServerTCP(m_socket, a_socket, "127.0.0.1", 1234))
  10.     {
  11.         std::cout << "Errore" << std::endl;
  12.     }
  13.     if(ntw.isConnected)
  14.     {
  15.         std::cout << "Connesso" << std::endl;
  16.         system("PAUSE");
  17.     }
  18.     return 0;
  19. }




CLIENT.CPP
Codice sorgente - presumibilmente C/C++

  1. #include "Networks.h"
  2.  
  3. int main()
  4. {
  5.  
  6.     Networks ntw;
  7.     SOCKET m_socket;
  8.  
  9.     if(!ntw.ClientTCP(m_socket, "127.0.0.1", 4444))
  10.     {
  11.         std::cout << "Errore" << std::endl;
  12.     }
  13.     if(ntw.isConnected)
  14.     {
  15.         std::cout << "Connesso" << std::endl;
  16.         system("PAUSE");
  17.     }
  18.     return 0;
  19. }



se uso la classe senza passargli un socket e implementandola nella classe funziona...invece se la passo io no...
non so xke.... :\

PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 20:30
Sabato, 16/04/2022
Se passi il socket sempre per valore e non per puntatore è ovvio che non funzionerà mai


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
PM Quote
Avatar
Gemini (Normal User)
Rookie


Messaggi: 33
Iscritto: 04/10/2021

Segnala al moderatore
Postato alle 8:55
Domenica, 17/04/2022
scusami nessuno scusa per la stupidità ma in che senso devo passarglielo come puntare ? potresti farmi un esempio plz.... ho provato a fare sia così
SOCKET *m_socket e a passargliela però mi danno errore le funzioni standard di winsock perchè non accettano questo tipo di parametro.... e ho provato a fare anche così
Networks *ntw;
ntw->CreaSocket(blablabla), e nemmeno funziona..... scusami per la domanda stupida :D....

PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 13:02
Domenica, 17/04/2022
Non è una domanda stupida, semplicemente una domanda fatta da chi non conosce le basi del linguaggio e vuole fare il passo più lungo della gamba.

Prima di scrivere codice con i socket dovresti conoscere le basi del C e del C++ altrimenti ti fermerai ad ogni passo e dovrai copiare codice scritto da altri.


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
PM Quote
Avatar
Gemini (Normal User)
Rookie


Messaggi: 33
Iscritto: 04/10/2021

Segnala al moderatore
Postato alle 16:26
Venerdì, 22/04/2022
hai ragione nessuno ma purtroppo studiando da autodidatta e quindi vedendo video e tutorial su internet non ricordavo questa particolarità dei puntatori ,che con la & si indica l'indirizzo che occupa la variabile nella ram mentre con * si fa riferimento al valore del puntatore....non ho ancora provato ma se provo in questo modo dovrebbe funzionare...almeno credo...

Codice sorgente - presumibilmente C++

  1. #include <winsock2.h>
  2. #include <iostream>
  3.  
  4. #pragma comment(lib, "ws2_32.lib")
  5.  
  6. class Networks
  7. {
  8.  
  9.     public:
  10.        
  11.         //Normal Socket
  12.         bool ServerTCP(SOCKET& m_socket, SOCKET a_socket, const char *ip, int port);
  13.         bool ClientTCP(SOCKET m_socket, const char *ip, int port);
  14.         //WSASocket
  15.         bool WSAServerTCP(SOCKET m_socket, SOCKET a_socket, const char *ip, int port);
  16.         bool WSAClientTCP(SOCKET m_socket, const char *ip, int port);
  17.  
  18.         bool Send(SOCKET s_socket, const char *buffer);
  19.         std::string Recv(SOCKET r_socket);
  20.    
  21.         bool isConnected = false;
  22.  
  23.    
  24.     private:
  25.  
  26.         //Variabili Socket Private
  27.         WORD DllVersion = MAKEWORD(2,2);
  28.         WSADATA wsaData;
  29.         sockaddr_in si;
  30.         sockaddr_in a_si;
  31.  
  32.         //SOCKET m_socket;
  33.         //SOCKET a_socket;
  34.  
  35.         bool is_Server = false;
  36.         bool is_Client = false;
  37.         bool is_WSASocket = false;
  38.  
  39.         //Metodi Socket Privati
  40.         bool InitSocket();
  41.         bool CreateSocket(SOCKET m_socket, const char *ip, int port);
  42.         bool CloseSocket(SOCKET m_socket, SOCKET a_socket);
  43.  
  44.         //Metodi Client Socket
  45.         bool ConnectSocket(SOCKET m_socket);
  46.  
  47.         //Metodi Server Socket
  48.         bool BindSocket(SOCKET m_socket);
  49.         bool AcceptSocket(SOCKET m_socket, SOCKET a_socket);
  50.  
  51.    
  52.  
  53.  
  54. };












Codice sorgente - presumibilmente C++

  1. #include "Networks.h"
  2.  
  3.  
  4. //===============================================//
  5. //=                ServerTCP                    =//
  6. //===============================================//
  7. bool Networks::ServerTCP(SOCKET& m_socket, SOCKET a_socket, const char *ip, int port)
  8. {
  9.     is_Server = true;
  10.  
  11.     if(!InitSocket())
  12.         return false;
  13.  
  14.     if(!CreateSocket(m_socket, ip, port))
  15.         return false;
  16.        
  17.     if(!BindSocket(m_socket))
  18.         return false;
  19.  
  20.     //Qui forse ci vuole un while true
  21.     if(!AcceptSocket(m_socket, a_socket))
  22.         return false;
  23.  
  24.    
  25.     isConnected = true;
  26.     return true;
  27. }
  28.  
  29. //===============================================//
  30. //=                ClientTCP                    =//
  31. //===============================================//
  32. bool Networks::ClientTCP(SOCKET m_socket, const char *ip, int port)
  33. {
  34.     is_Client = true;
  35.  
  36.     if(!InitSocket())
  37.         return false;
  38.  
  39.     if(!CreateSocket(m_socket, ip, port))
  40.         return false;
  41.  
  42.      //Qui forse ci vuole un while  
  43.     if(!ConnectSocket(m_socket))
  44.         return false;
  45.  
  46.    
  47.     isConnected = true;
  48.     return true;
  49. }
  50.  
  51. //===============================================//
  52. //=                WSAServerTCP                 =//
  53. //===============================================//
  54. bool Networks::WSAServerTCP(SOCKET m_socket, SOCKET a_socket, const char *ip, int port)
  55. {
  56.     is_WSASocket = true;
  57.     is_Server = true;
  58.    
  59.     if(!InitSocket())
  60.         return false;
  61.  
  62.     if(!CreateSocket(m_socket, ip, port))
  63.         return false;
  64.        
  65.     if(!BindSocket(m_socket))
  66.         return false;
  67.  
  68.     //Qui forse ci vuole un while true
  69.     if(!AcceptSocket(m_socket, a_socket))
  70.         return false;
  71.  
  72.    
  73.     isConnected = true;
  74.     return true;
  75. }
  76.  
  77. //===============================================//
  78. //=                WSAClientTCP                 =//
  79. //===============================================//
  80. bool Networks::WSAClientTCP(SOCKET m_socket, const char *ip, int port)
  81. {
  82.     is_WSASocket = true;
  83.     is_Client = true;
  84.  
  85.     if(!InitSocket())
  86.         return false;
  87.  
  88.     if(!CreateSocket(m_socket, ip, port))
  89.         return false;
  90.  
  91.      //Qui forse ci vuole un while  
  92.     if(!ConnectSocket(m_socket))
  93.         return false;
  94.  
  95.    
  96.     isConnected = true;
  97.     return true;
  98. }
  99.  
  100.  
  101.  
  102.  
  103. //===============================================//
  104. //=                initWSASocket                =//
  105. //===============================================//
  106. bool Networks::InitSocket()
  107. {
  108.     int WsaResult = WSAStartup(DllVersion, &wsaData);
  109.     if(WsaResult != 0)
  110.     {
  111.         std::cout << "Fallito WSAStartup()." << std::endl;
  112.                 return false;
  113.     }
  114.        
  115.     return true;
  116. }
  117.  
  118.  
  119.  
  120. //===============================================//
  121. //=                createWSASocket              =//
  122. //===============================================//
  123. bool Networks::CreateSocket(SOCKET m_socket, const char *ip, int port)
  124. {
  125.     //Normal Socket
  126.     if(is_WSASocket == false)
  127.         m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  128.     else if(is_WSASocket == true) //WSA SOCKET
  129.         m_socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, (unsigned int)NULL, (unsigned int)NULL);
  130.  
  131.     if(m_socket == INVALID_SOCKET)
  132.         return false;
  133.  
  134.     memset(&si, 0, sizeof(si));
  135.  
  136.     if(is_Server == true)
  137.     {
  138.         si.sin_family = AF_INET;
  139.         if(ip == NULL)
  140.                     si.sin_addr.s_addr = INADDR_ANY;
  141.         else
  142.             si.sin_addr.s_addr = inet_addr(ip);
  143.                 si.sin_port = htons(port);
  144.     }
  145.     else if(is_Client == true)
  146.     {
  147.         si.sin_family = AF_INET;
  148.         si.sin_addr.s_addr = inet_addr(ip);
  149.         si.sin_port = htons(port);
  150.     }
  151.     else
  152.     {
  153.         std::cout << "Il socket non e' ne di tipo server ne di tipo client" << std::endl;
  154.         return false;
  155.     }
  156.    
  157.     return true;
  158. }
  159.  
  160. //===============================================//
  161. //=                closeWSASocket               =//
  162. //===============================================//
  163. bool Networks::CloseSocket(SOCKET m_socket, SOCKET a_socket)
  164. {  
  165.     if(is_Server == true)
  166.     {
  167.         if(!closesocket(a_socket))
  168.             return false;
  169.     }
  170.  
  171.     if(!closesocket(m_socket))
  172.         return false;
  173.    
  174.     if(!WSACleanup())
  175.         return false;
  176.  
  177.     return true;
  178. }
  179.  
  180. //===============================================//
  181. //=             ConnectSocket                   =//
  182. //===============================================//
  183. bool Networks::ConnectSocket(SOCKET m_socket)
  184. {
  185.     int ConnResult;
  186.      //Normal Socket
  187.     if(is_WSASocket == false)
  188.         ConnResult = connect(m_socket, (struct sockaddr*)&si, sizeof(si));
  189.     else if(is_WSASocket == true) //WSA SOCKET  
  190.         ConnResult = WSAConnect(m_socket, (struct sockaddr*)&si, sizeof(si), NULL, NULL, NULL, NULL);
  191.    
  192.     if(ConnResult == SOCKET_ERROR)
  193.     {
  194.         std::cout << "errore %d"<< WSAGetLastError() << std::endl;
  195.         CloseSocket(m_socket, 0);
  196.         return false;
  197.     }
  198.  
  199.     return true;
  200. }
  201.  
  202.  
  203. //===============================================//
  204. //=             bindSocket                      =//
  205. //===============================================//
  206. bool Networks::BindSocket(SOCKET m_socket)
  207. {
  208.     if (bind(m_socket, (struct sockaddr*)&si, sizeof(si)) == SOCKET_ERROR)
  209.         {
  210.         std::cout << "Fallito bindSocket(). Errore: " << WSAGetLastError() << std::endl;
  211.         CloseSocket(m_socket, 0);
  212.                 return false;
  213.         }
  214.  
  215.         //Listen
  216.         if(listen(m_socket, 3) == SOCKET_ERROR)
  217.     {
  218.         std::cout << "Fallito listen(). Errore: " << WSAGetLastError() << std::endl;
  219.         CloseSocket(m_socket, 0);
  220.                 return false;
  221.     }
  222.         return true;
  223. }
  224.  
  225. //===============================================//
  226. //=             acceptWSASocket                 =//
  227. //===============================================//
  228. bool Networks::AcceptSocket(SOCKET m_socket, SOCKET a_socket)
  229. {
  230.     int len = sizeof(a_si);
  231.     int AcceptResult;
  232.  
  233.     //Normal Socket
  234.     if(is_WSASocket == false)
  235.         a_socket = accept(m_socket, (struct sockaddr*)&a_si, &len);
  236.     else if(is_WSASocket == true) //WSA SOCKET  
  237.         a_socket = WSAAccept(m_socket, (struct sockaddr*)&a_si, &len,NULL,0);
  238.    
  239.     /*if((a_socket = WSAAccept(m_socket, (struct sockaddr*)&a_si, &len,NULL,0)) == INVALID_SOCKET)
  240.     {
  241.         std::cout << "Fallito WSAAccept(). Errore: " << WSAGetLastError() << std::endl;
  242.         CloseSocket();
  243.                 return false;
  244.     }*/
  245.     if(a_socket == INVALID_SOCKET)
  246.     {
  247.         std::cout << "Fallito WSAAccept(). Errore: " << WSAGetLastError() << std::endl;
  248.         CloseSocket(m_socket, a_socket);
  249.                 return false;
  250.     }
  251.  
  252.     return true;
  253. }
  254.  
  255.  
  256.  
  257. //===============================================//
  258. //=                Send                         =//
  259. //===============================================//
  260. bool Networks::Send(SOCKET s_socket, const char *sendbuffer)
  261. {
  262.     int ResultSend;
  263.  
  264.     if(is_Server == true)
  265.     {
  266.         ResultSend = send(s_socket, sendbuffer, (int) strlen(sendbuffer), 0);
  267.     }
  268.        
  269.     else if(is_Client == true)
  270.     {
  271.         ResultSend = send(s_socket, sendbuffer, (int) strlen(sendbuffer), 0);
  272.     }
  273.        
  274.     else
  275.     {
  276.         std::cout << "Nessun socket specificato!!!" << std::endl;
  277.         return false;
  278.     }
  279.  
  280.     if(ResultSend == SOCKET_ERROR)
  281.     {
  282.         std::cout << "Errore Send(): " << WSAGetLastError() << std::endl;
  283.         CloseSocket(s_socket, 0);
  284.         return false;
  285.     }
  286.  
  287.     std::cout << "Messaggio inviato!" << std::endl;
  288.  
  289.     return true;
  290. }
  291.  
  292.  
  293. //===============================================//
  294. //=                Recv                         =//
  295. //===============================================//
  296. std::string Networks::Recv(SOCKET r_socket)
  297. {
  298.     const int recvbufflen = 1024;
  299.     char recvbuffer[recvbufflen];
  300.     int ResultRecv;
  301.  
  302.     do
  303.     {
  304.         if(is_Server == true)
  305.         {
  306.             ResultRecv = recv(r_socket,recvbuffer, recvbufflen, 0);
  307.         }
  308.            
  309.         else if(is_Client == true)
  310.         {
  311.             ResultRecv = recv(r_socket,recvbuffer, recvbufflen, 0);
  312.         }
  313.            
  314.         else
  315.         {
  316.             std::cout << "Nessun socket specificato!!!" << std::endl;
  317.             return "false";
  318.         }
  319.        
  320.         if(ResultRecv > 0)
  321.         {
  322.             recvbuffer[ResultRecv] = 0;
  323.             //std::cout << "byte ricevuti" << ResultRecv << " " << recvbuffer << std::endl;
  324.             return recvbuffer;
  325.         }
  326.         else if(ResultRecv == 0)
  327.             std::cout << "Connesione chiusa!" << std::endl;
  328.         else
  329.             std::cout << "Error Recv(): " << WSAGetLastError() << std::endl;
  330.  
  331.     } while (ResultRecv > 0);
  332.    
  333.     return "true";
  334. }





Codice sorgente - presumibilmente C/C++

  1. #include "Networks.h"
  2. int main()
  3. {    
  4.     Networks ntw;
  5.     SOCKET m_socket, a_socket;    
  6.     if(!ntw.ServerTCP(m_socket, a_socket, "127.0.0.1", 1234))    
  7.     {        
  8.         std::cout << "Errore" << std::endl;    
  9.     }    
  10.     if(ntw.isConnected)    
  11.     {        
  12.         std::cout << "Connesso" << std::endl;        
  13.         system("PAUSE");    
  14.     }    
  15.     return 0;
  16. }



non ho provato ma volevo sapere solo se ho capito bene o sto ancora sbagliando.... :|


ho provato a fare degli esempi per vedere se mi restituiva il valore del socket che sarebbe 16, e ho provato a fare in questo modo
Codice sorgente - presumibilmente C++

  1. #include <winsock2.h>
  2. #include <iostream>
  3.  
  4. #pragma comment(lib, "ws2_32")
  5.  
  6. bool CreateSocket(SOCKET& socketidentifier)
  7. {
  8.     std::cout << "ScketIdentifier: " << socketidentifier << std::endl;
  9.     return true;
  10. }
  11.  
  12.  
  13. int main()
  14. {
  15.     SOCKET socketId;
  16.  
  17.     std::cout << "SocketID: " << socketId << std::endl;
  18.  
  19.     CreateSocket(socketId);
  20.  
  21.     return 0;
  22. }




Ultima modifica effettuata da Gemini il 22/04/2022 alle 16:32
PM Quote