23/03/22 17:44
Gemini
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
Network.cpp
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!
Network.h
#include <winsock2.h> #include <iostream> #pragma comment(lib, "ws2_32.lib") class Networks { public: bool ServerTCP(); bool ClientTCP(); bool Send(const char *buffer); bool Recv(); //* funzione da fare -> bool wsaSend(char *buffer); bool isConnected = false; private: //Variabili Socket Private WORD DllVersion = MAKEWORD(2,2); WSADATA wsaData; sockaddr_in si; sockaddr_in a_si; SOCKET m_socket; SOCKET a_socket; bool is_Server = false; bool is_Client = false; //Metodi Socket Privati bool initWSASocket(); bool createWSASocket(); bool closeWSASocket(); //Metodi Client Socket bool connectWSASocket(); //Metodi Server Socket bool bindSocket(); bool acceptWSASocket();
Network.cpp
#include "Networks.h" //===============================================// //= ServerTCP =// //===============================================// bool Networks::ServerTCP() { is_Server = true; if(!initWSASocket()) return false; if(!createWSASocket()) return false; if(!bindSocket()) return false; //Qui forse ci vuole un while true if(!acceptWSASocket()) return false; isConnected = true; return true; } //===============================================// //= ClientTCP =// //===============================================// bool Networks::ClientTCP() { is_Client = true; if(!initWSASocket()) return false; if(!createWSASocket()) return false; if(!connectWSASocket()) return false; isConnected = true; return true; } //===============================================// //= initWSASocket =// //===============================================// bool Networks::initWSASocket() { int WsaResult = WSAStartup(DllVersion, &wsaData); if(WsaResult != 0) { std::cout << "Fallito WSAStartup()." << std::endl; return false; } return true; } //===============================================// //= createWSASocket =// //===============================================// bool Networks::createWSASocket() { m_socket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, (unsigned int)NULL, (unsigned int)NULL); if(m_socket == INVALID_SOCKET) return false; memset(&si, 0, sizeof(si)); if(is_Server == true) { si.sin_family = AF_INET; si.sin_addr.s_addr = INADDR_ANY; si.sin_port = htons(4445); } else if(is_Client == true) { si.sin_family = AF_INET; si.sin_port = htons(4445); si.sin_addr.s_addr = inet_addr("127.0.0.1"); } else { std::cout << "Il socket non e' ne di tipo server ne di tipo client" << std::endl; return false; } return true; } //===============================================// //= closeWSASocket =// //===============================================// bool Networks::closeWSASocket() { if(is_Server == true) { if(!closesocket(a_socket)) return false; } if(!closesocket(m_socket)) return false; if(!WSACleanup()) return false; return true; } //===============================================// //= connectWSASocket =// //===============================================// bool Networks::connectWSASocket() { int ConnResult = WSAConnect(m_socket, (struct sockaddr*)&si, sizeof(si), NULL, NULL, NULL, NULL); if(ConnResult == SOCKET_ERROR) { std::cout << "errore %d"<< WSAGetLastError() << std::endl; closeWSASocket(); return false; } return true; } //===============================================// //= bindSocket =// //===============================================// bool Networks::bindSocket() { if (bind(m_socket, (struct sockaddr*)&si, sizeof(si)) == SOCKET_ERROR) { std::cout << "Fallito bindSocket(). Errore: " << WSAGetLastError() << std::endl; closeWSASocket(); return false; } //Listen if(listen(m_socket, 3) == SOCKET_ERROR) { std::cout << "Fallito listen(). Errore: " << WSAGetLastError() << std::endl; closeWSASocket(); return false; } return true; } //===============================================// //= acceptWSASocket =// //===============================================// bool Networks::acceptWSASocket() { int len = sizeof(a_si); if(a_socket = WSAAccept(m_socket, (struct sockaddr*)&a_si, &len,NULL,0) == SOCKET_ERROR) { std::cout << "Fallito WSAAccept(). Errore: " << WSAGetLastError() << std::endl; closeWSASocket(); return false; } return true; } //===============================================// //= Send =// //===============================================// bool Networks::Send(const char *sendbuffer) { int ResultSend; if(is_Server == true) ResultSend = send(a_socket, sendbuffer, (int) strlen(sendbuffer), 0); else if(is_Client == true) ResultSend = send(m_socket, sendbuffer, (int) strlen(sendbuffer), 0); else { std::cout << "Nessun socket specificato!!!" << std::endl; return false; } if(ResultSend == SOCKET_ERROR) { std::cout << "Errore Send(): " << WSAGetLastError() << std::endl; closeWSASocket(); return false; } std::cout << "Messaggio inviato!" << std::endl; return true; } //===============================================// //= Recv =// //===============================================// bool Networks::Recv() { int recvbufflen = 1024; char recvbuffer[recvbufflen]; int ResultRecv; do { if(is_Server == true) { ResultRecv = recv(a_socket,recvbuffer, recvbufflen, 0); } else if(is_Client == true) { ResultRecv = recv(m_socket,recvbuffer, recvbufflen, 0); } else { std::cout << "Nessun socket specificato!!!" << std::endl; return false; } if(ResultRecv > 0) std::cout << "byte ricevuti" << ResultRecv << " " << recvbuffer << std::endl; else if(ResultRecv == 0) std::cout << "Connesione chiusa!" << std::endl; else std::cout << "Error Recv(): " << WSAGetLastError() << std::endl; } while (ResultRecv > 0); return true; }
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!