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++ - Send Server errore 100038
Forum - C/C++ - Send Server errore 100038

Avatar
Gemini (Normal User)
Rookie


Messaggi: 33
Iscritto: 04/10/2021

Segnala al moderatore
Postato alle 18:44
Mercoledì, 23/03/2022
Eccomi con un'altro problema che non riesco a capire il perchè... in pratica ho creato una classe dove si crea una connessione WSASocket e ho creato questa classe per semplificare tutti i passaggi nulla di complicato... però non capisco perchè il server sia quando riceve che quando invia una stringa mi da errore 100038..ho visto un pò su internet e mi dice che non ho associato nessuna SOCKET a Send o Recv... vi provo a mostrare un pò il codice della classe magari qualcuno riesce a vedere l'errore dov'è perchè ripeto non riesco a trovarlo per me è tutto ok

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.         bool ServerTCP();
  11.         bool ClientTCP();
  12.  
  13.         bool Send(const char *buffer);
  14.         bool Recv();
  15.        
  16.         //* funzione da fare -> bool wsaSend(char *buffer);
  17.  
  18.        
  19.         bool isConnected = false;
  20.  
  21.    
  22.     private:
  23.  
  24.     //Variabili Socket Private
  25.     WORD DllVersion = MAKEWORD(2,2);
  26.     WSADATA wsaData;
  27.     sockaddr_in si;
  28.     sockaddr_in a_si;
  29.  
  30.     SOCKET m_socket;
  31.     SOCKET a_socket;
  32.  
  33.     bool is_Server = false;
  34.     bool is_Client = false;
  35.  
  36.     //Metodi Socket Privati
  37.     bool initWSASocket();
  38.     bool createWSASocket();
  39.     bool closeWSASocket();
  40.  
  41.     //Metodi Client Socket
  42.     bool connectWSASocket();
  43.  
  44.     //Metodi Server Socket
  45.     bool bindSocket();
  46.     bool acceptWSASocket();





Network.cpp
Codice sorgente - presumibilmente C++

  1. #include "Networks.h"
  2.  
  3.  
  4. //===============================================//
  5. //=                ServerTCP                    =//
  6. //===============================================//
  7. bool Networks::ServerTCP()
  8. {
  9.     is_Server = true;
  10.  
  11.     if(!initWSASocket())
  12.         return false;
  13.  
  14.     if(!createWSASocket())
  15.         return false;
  16.        
  17.     if(!bindSocket())
  18.         return false;
  19.  
  20.     //Qui forse ci vuole un while true
  21.     if(!acceptWSASocket())
  22.         return false;
  23.  
  24.    
  25.     isConnected = true;
  26.     return true;
  27. }
  28.  
  29. //===============================================//
  30. //=                ClientTCP                    =//
  31. //===============================================//
  32. bool Networks::ClientTCP()
  33. {
  34.     is_Client = true;
  35.  
  36.     if(!initWSASocket())
  37.         return false;
  38.  
  39.     if(!createWSASocket())
  40.         return false;
  41.  
  42.     if(!connectWSASocket())
  43.         return false;
  44.  
  45.    
  46.     isConnected = true;
  47.     return true;
  48. }
  49.  
  50.  
  51.  
  52.  
  53. //===============================================//
  54. //=                initWSASocket                =//
  55. //===============================================//
  56. bool Networks::initWSASocket()
  57. {
  58.     int WsaResult = WSAStartup(DllVersion, &wsaData);
  59.     if(WsaResult != 0)
  60.     {
  61.         std::cout << "Fallito WSAStartup()." << std::endl;
  62.                 return false;
  63.     }
  64.        
  65.    
  66.     return true;
  67. }
  68.  
  69.  
  70. //===============================================//
  71. //=                createWSASocket              =//
  72. //===============================================//
  73. bool Networks::createWSASocket()
  74. {
  75.     m_socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, (unsigned int)NULL, (unsigned int)NULL);
  76.     if(m_socket == INVALID_SOCKET)
  77.         return false;
  78.  
  79.     memset(&si, 0, sizeof(si));
  80.  
  81.     if(is_Server == true)
  82.     {
  83.         si.sin_family = AF_INET;
  84.                 si.sin_addr.s_addr = INADDR_ANY;
  85.                 si.sin_port = htons(4445);
  86.     }
  87.     else if(is_Client == true)
  88.     {
  89.         si.sin_family = AF_INET;
  90.         si.sin_port = htons(4445);
  91.         si.sin_addr.s_addr = inet_addr("127.0.0.1");
  92.     }
  93.     else
  94.     {
  95.         std::cout << "Il socket non e' ne di tipo server ne di tipo client" << std::endl;
  96.         return false;
  97.     }
  98.    
  99.     return true;
  100. }
  101.  
  102. //===============================================//
  103. //=                closeWSASocket               =//
  104. //===============================================//
  105. bool Networks::closeWSASocket()
  106. {  
  107.     if(is_Server == true)
  108.     {
  109.         if(!closesocket(a_socket))
  110.             return false;
  111.     }
  112.  
  113.     if(!closesocket(m_socket))
  114.         return false;
  115.    
  116.     if(!WSACleanup())
  117.         return false;
  118.  
  119.     return true;
  120. }
  121.  
  122. //===============================================//
  123. //=             connectWSASocket                =//
  124. //===============================================//
  125. bool Networks::connectWSASocket()
  126. {
  127.     int ConnResult = WSAConnect(m_socket, (struct sockaddr*)&si, sizeof(si), NULL, NULL, NULL, NULL);
  128.    
  129.     if(ConnResult == SOCKET_ERROR)
  130.     {
  131.         std::cout << "errore %d"<< WSAGetLastError() << std::endl;
  132.         closeWSASocket();
  133.         return false;
  134.     }
  135.  
  136.     return true;
  137. }
  138.  
  139.  
  140. //===============================================//
  141. //=             bindSocket                      =//
  142. //===============================================//
  143. bool Networks::bindSocket()
  144. {
  145.     if (bind(m_socket, (struct sockaddr*)&si, sizeof(si)) == SOCKET_ERROR)
  146.         {
  147.         std::cout << "Fallito bindSocket(). Errore: " << WSAGetLastError() << std::endl;
  148.         closeWSASocket();
  149.                 return false;
  150.         }
  151.  
  152.         //Listen
  153.         if(listen(m_socket, 3) == SOCKET_ERROR)
  154.     {
  155.         std::cout << "Fallito listen(). Errore: " << WSAGetLastError() << std::endl;
  156.         closeWSASocket();
  157.                 return false;
  158.     }
  159.         return true;
  160. }
  161.  
  162. //===============================================//
  163. //=             acceptWSASocket                 =//
  164. //===============================================//
  165. bool Networks::acceptWSASocket()
  166. {
  167.     int len = sizeof(a_si);
  168.    
  169.     if(a_socket = WSAAccept(m_socket, (struct sockaddr*)&a_si, &len,NULL,0) == SOCKET_ERROR)
  170.     {
  171.         std::cout << "Fallito WSAAccept(). Errore: " << WSAGetLastError() << std::endl;
  172.         closeWSASocket();
  173.                 return false;
  174.     }
  175.  
  176.     return true;
  177. }
  178.  
  179.  
  180.  
  181. //===============================================//
  182. //=                Send                         =//
  183. //===============================================//
  184. bool Networks::Send(const char *sendbuffer)
  185. {
  186.     int ResultSend;
  187.  
  188.     if(is_Server == true)
  189.         ResultSend = send(a_socket, sendbuffer, (int) strlen(sendbuffer), 0);  
  190.     else if(is_Client == true)
  191.         ResultSend = send(m_socket, sendbuffer, (int) strlen(sendbuffer), 0);    
  192.     else
  193.     {
  194.         std::cout << "Nessun socket specificato!!!" << std::endl;
  195.         return false;
  196.     }
  197.  
  198.     if(ResultSend == SOCKET_ERROR)
  199.     {
  200.         std::cout << "Errore Send(): " << WSAGetLastError() << std::endl;
  201.         closeWSASocket();
  202.         return false;
  203.     }
  204.  
  205.     std::cout << "Messaggio inviato!" << std::endl;
  206.  
  207.     return true;
  208. }
  209.  
  210.  
  211. //===============================================//
  212. //=                Recv                         =//
  213. //===============================================//
  214. bool Networks::Recv()
  215. {
  216.     int recvbufflen = 1024;
  217.     char recvbuffer[recvbufflen];
  218.     int ResultRecv;
  219.  
  220.     do
  221.     {
  222.         if(is_Server == true)
  223.         {
  224.             ResultRecv = recv(a_socket,recvbuffer, recvbufflen, 0);
  225.         }
  226.            
  227.         else if(is_Client == true)
  228.         {
  229.             ResultRecv = recv(m_socket,recvbuffer, recvbufflen, 0);
  230.         }
  231.            
  232.         else
  233.         {
  234.             std::cout << "Nessun socket specificato!!!" << std::endl;
  235.             return false;
  236.         }
  237.        
  238.         if(ResultRecv > 0)
  239.             std::cout << "byte ricevuti" << ResultRecv << " " << recvbuffer << std::endl;
  240.         else if(ResultRecv == 0)
  241.             std::cout << "Connesione chiusa!" << std::endl;
  242.         else
  243.             std::cout << "Error Recv(): " << WSAGetLastError() << std::endl;
  244.  
  245.     } while (ResultRecv > 0);
  246.    
  247.     return true;
  248. }



Il server e il client si connettono e l'errore lo trovo solo con il server mentre con il client mi riceve e manda messaggi senza problemi.... non capisco!

PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6403
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 19:53
Mercoledì, 23/03/2022
Ma il main qual è?


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 20:02
Mercoledì, 23/03/2022
non loi ho postati perchè pensavo che non ce ne era bisogno scusami... cmq eccoli

Client.cpp
Codice sorgente - presumibilmente C/C++

  1. #include "Networks.h"
  2.  
  3.  
  4. int main()
  5. {
  6.     Networks ntw;
  7.  
  8.     if(!ntw.ClientTCP())
  9.     {
  10.         std::cout << "ClientTCP() Errore" << std::endl;
  11.        
  12.     }
  13.  
  14.     if(ntw.isConnected == true)
  15.     {
  16.         std::cout << "Connesso!" << std::endl;
  17.         ntw.Recv();
  18.        
  19.     }
  20.    
  21.    
  22.     system("PAUSE");
  23.     return 0;
  24. }



Server.cpp
Codice sorgente - presumibilmente C/C++

  1. #include "Networks.h"
  2.  
  3.  
  4.  
  5. int main()
  6. {
  7.     Networks ntw;
  8.  
  9.     if(!ntw.ServerTCP())
  10.     {
  11.         std::cout << "ServerTCP() Errore" << std::endl;
  12.     }
  13.    
  14.     if(ntw.isConnected == true)
  15.     {
  16.         std::cout << "Connesso!" << std::endl;
  17.         ntw.Send("Ciao");
  18.        
  19.     }
  20.    
  21.    
  22.     system("PAUSE");
  23.     return 0;
  24. }


Ultima modifica effettuata da Gemini il 23/03/2022 alle 20:04
PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6403
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 20:34
Mercoledì, 23/03/2022
Nella acceptWSASocket devi sostituire la linea della accept con questa

if ((a_socket = WSAAccept(m_socket, (struct sockaddr*)&a_si, &len, NULL, 0)) == INVALID_SOCKET)

e nella Recv devi scrivere

if (ResultRecv > 0)
{
     recvbuffer[ResultRecv] = NULL;
     std::cout << "byte ricevuti" << ResultRecv << " " << recvbuffer << std::endl;
}
else .....


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 20:56
Mercoledì, 23/03/2022
oh grazie mille nessuno non me ne ero accorto  che ci volevano altre parentesi tonde....e poi dimentico sempre di azzerare l'array :) grazie ancora nessuno :k:

PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6403
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 21:15
Mercoledì, 23/03/2022
E poi avevi usato SOCKET_ERROR ma ci vuole INVALID_SOCKET


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