Forum - C/C++
- Quando passo il SOCKET alla funzione mi da errore sul bind
Gemini (Normal User)
Rookie
Messaggi: 33
Iscritto: 04/10/2021
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++
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
class Networks
{
public :
//Normal Socket
bool ServerTCP( SOCKET m_socket, SOCKET a_socket, const char * ip, int port) ;
bool ClientTCP( SOCKET m_socket, const char * ip, int port) ;
//WSASocket
bool WSAServerTCP( SOCKET m_socket, SOCKET a_socket, const char * ip, int port) ;
bool WSAClientTCP( SOCKET m_socket, const char * ip, int port) ;
bool Send( SOCKET s_socket, const char * buffer) ;
std:: string Recv( SOCKET r_socket) ;
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 ;
bool is_WSASocket = false ;
//Metodi Socket Privati
bool InitSocket( ) ;
bool CreateSocket( SOCKET m_socket, const char * ip, int port) ;
bool CloseSocket( SOCKET m_socket, SOCKET a_socket) ;
//Metodi Client Socket
bool ConnectSocket( SOCKET m_socket) ;
//Metodi Server Socket
bool BindSocket( SOCKET m_socket) ;
bool AcceptSocket( SOCKET m_socket, SOCKET a_socket) ;
} ;
CLASSE NETWORK:CPP
Codice sorgente - presumibilmente C++
#include "Networks.h"
//===============================================//
//= ServerTCP =//
//===============================================//
bool Networks:: ServerTCP ( SOCKET m_socket, SOCKET a_socket, const char * ip, int port)
{
is_Server = true ;
if ( ! InitSocket( ) )
return false ;
if ( ! CreateSocket( m_socket, ip, port) )
return false ;
if ( ! BindSocket( m_socket) )
return false ;
//Qui forse ci vuole un while true
if ( ! AcceptSocket( m_socket, a_socket) )
return false ;
isConnected = true ;
return true ;
}
//===============================================//
//= ClientTCP =//
//===============================================//
bool Networks:: ClientTCP ( SOCKET m_socket, const char * ip, int port)
{
is_Client = true ;
if ( ! InitSocket( ) )
return false ;
if ( ! CreateSocket( m_socket, ip, port) )
return false ;
//Qui forse ci vuole un while
if ( ! ConnectSocket( m_socket) )
return false ;
isConnected = true ;
return true ;
}
//===============================================//
//= WSAServerTCP =//
//===============================================//
bool Networks:: WSAServerTCP ( SOCKET m_socket, SOCKET a_socket, const char * ip, int port)
{
is_WSASocket = true ;
is_Server = true ;
if ( ! InitSocket( ) )
return false ;
if ( ! CreateSocket( m_socket, ip, port) )
return false ;
if ( ! BindSocket( m_socket) )
return false ;
//Qui forse ci vuole un while true
if ( ! AcceptSocket( m_socket, a_socket) )
return false ;
isConnected = true ;
return true ;
}
//===============================================//
//= WSAClientTCP =//
//===============================================//
bool Networks:: WSAClientTCP ( SOCKET m_socket, const char * ip, int port)
{
is_WSASocket = true ;
is_Client = true ;
if ( ! InitSocket( ) )
return false ;
if ( ! CreateSocket( m_socket, ip, port) )
return false ;
//Qui forse ci vuole un while
if ( ! ConnectSocket( m_socket) )
return false ;
isConnected = true ;
return true ;
}
//===============================================//
//= initWSASocket =//
//===============================================//
bool Networks:: InitSocket ( )
{
int WsaResult = WSAStartup( DllVersion, & wsaData) ;
if ( WsaResult ! = 0)
{
std:: cout << "Fallito WSAStartup()." << std:: endl ;
return false ;
}
return true ;
}
//===============================================//
//= createWSASocket =//
//===============================================//
bool Networks:: CreateSocket ( SOCKET m_socket, const char * ip, int port)
{
//Normal Socket
if ( is_WSASocket == false )
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP) ;
else if ( is_WSASocket == true ) //WSA SOCKET
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;
if ( ip == NULL )
si.sin_addr .s_addr = INADDR_ANY;
else
si.sin_addr .s_addr = inet_addr( ip) ;
si.sin_port = htons( port) ;
}
else if ( is_Client == true )
{
si.sin_family = AF_INET;
si.sin_addr .s_addr = inet_addr( ip) ;
si.sin_port = htons( port) ;
}
else
{
std:: cout << "Il socket non e' ne di tipo server ne di tipo client" << std:: endl ;
return false ;
}
return true ;
}
//===============================================//
//= closeWSASocket =//
//===============================================//
bool Networks:: CloseSocket ( SOCKET m_socket, SOCKET a_socket)
{
if ( is_Server == true )
{
if ( ! closesocket( a_socket) )
return false ;
}
if ( ! closesocket( m_socket) )
return false ;
if ( ! WSACleanup( ) )
return false ;
return true ;
}
//===============================================//
//= ConnectSocket =//
//===============================================//
bool Networks:: ConnectSocket ( SOCKET m_socket)
{
int ConnResult;
//Normal Socket
if ( is_WSASocket == false )
ConnResult = connect( m_socket, ( struct sockaddr* ) & si, sizeof ( si) ) ;
else if ( is_WSASocket == true ) //WSA SOCKET
ConnResult = WSAConnect( m_socket, ( struct sockaddr* ) & si, sizeof ( si) , NULL , NULL , NULL , NULL ) ;
if ( ConnResult == SOCKET_ERROR)
{
std:: cout << "errore %d" << WSAGetLastError( ) << std:: endl ;
CloseSocket( m_socket, 0) ;
return false ;
}
return true ;
}
//===============================================//
//= bindSocket =//
//===============================================//
bool Networks:: BindSocket ( SOCKET m_socket)
{
if ( bind( m_socket, ( struct sockaddr* ) & si, sizeof ( si) ) == SOCKET_ERROR)
{
std:: cout << "Fallito bindSocket(). Errore: " << WSAGetLastError( ) << std:: endl ;
CloseSocket( m_socket, 0) ;
return false ;
}
//Listen
if ( listen( m_socket, 3) == SOCKET_ERROR)
{
std:: cout << "Fallito listen(). Errore: " << WSAGetLastError( ) << std:: endl ;
CloseSocket( m_socket, 0) ;
return false ;
}
return true ;
}
//===============================================//
//= acceptWSASocket =//
//===============================================//
bool Networks:: AcceptSocket ( SOCKET m_socket, SOCKET a_socket)
{
int len = sizeof ( a_si) ;
int AcceptResult;
//Normal Socket
if ( is_WSASocket == false )
a_socket = accept( m_socket, ( struct sockaddr* ) & a_si, & len) ;
else if ( is_WSASocket == true ) //WSA SOCKET
a_socket = WSAAccept( m_socket, ( struct sockaddr* ) & a_si, & len,NULL ,0) ;
/*if((a_socket = WSAAccept(m_socket, (struct sockaddr*)&a_si, &len,NULL,0)) == INVALID_SOCKET)
{
std::cout << "Fallito WSAAccept(). Errore: " << WSAGetLastError() << std::endl;
CloseSocket();
return false;
}*/
if ( a_socket == INVALID_SOCKET)
{
std:: cout << "Fallito WSAAccept(). Errore: " << WSAGetLastError( ) << std:: endl ;
CloseSocket( m_socket, a_socket) ;
return false ;
}
return true ;
}
//===============================================//
//= Send =//
//===============================================//
bool Networks:: Send ( SOCKET s_socket, const char * sendbuffer)
{
int ResultSend;
if ( is_Server == true )
{
ResultSend = send( s_socket, sendbuffer, ( int ) strlen ( sendbuffer) , 0) ;
}
else if ( is_Client == true )
{
ResultSend = send( s_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 ;
CloseSocket( s_socket, 0) ;
return false ;
}
std:: cout << "Messaggio inviato!" << std:: endl ;
return true ;
}
//===============================================//
//= Recv =//
//===============================================//
std:: string Networks:: Recv ( SOCKET r_socket)
{
const int recvbufflen = 1024 ;
char recvbuffer[ recvbufflen] ;
int ResultRecv;
do
{
if ( is_Server == true )
{
ResultRecv = recv( r_socket,recvbuffer, recvbufflen, 0) ;
}
else if ( is_Client == true )
{
ResultRecv = recv( r_socket,recvbuffer, recvbufflen, 0) ;
}
else
{
std:: cout << "Nessun socket specificato!!!" << std:: endl ;
return "false" ;
}
if ( ResultRecv > 0)
{
recvbuffer[ ResultRecv] = 0 ;
//std::cout << "byte ricevuti" << ResultRecv << " " << recvbuffer << std::endl;
return recvbuffer;
}
else if ( ResultRecv == 0)
std:: cout << "Connesione chiusa!" << std:: endl ;
else
std:: cout << "Error Recv(): " << WSAGetLastError( ) << std:: endl ;
} while ( ResultRecv > 0) ;
return "true" ;
}
//===================== CLIENT/SERVER =========================//
SERVER:CPP
Codice sorgente - presumibilmente C/C++
#include "Networks.h"
int main()
{
Networks ntw;
SOCKET m_socket, a_socket;
if(!ntw.ServerTCP(m_socket, a_socket, "127.0.0.1", 1234))
{
std::cout << "Errore" << std::endl;
}
if(ntw.isConnected)
{
std::cout << "Connesso" << std::endl;
system("PAUSE");
}
return 0;
}
CLIENT.CPP
Codice sorgente - presumibilmente C/C++
#include "Networks.h"
int main()
{
Networks ntw;
SOCKET m_socket;
if(!ntw.ClientTCP(m_socket, "127.0.0.1", 4444))
{
std::cout << "Errore" << std::endl;
}
if(ntw.isConnected)
{
std::cout << "Connesso" << std::endl;
system("PAUSE");
}
return 0;
}
se uso la classe senza passargli un socket e implementandola nella classe funziona...invece se la passo io no...
non so xke.... :\
nessuno (Normal User)
Guru^2
Messaggi: 6318
Iscritto: 03/01/2010
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à.
Gemini (Normal User)
Rookie
Messaggi: 33
Iscritto: 04/10/2021
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 ....
nessuno (Normal User)
Guru^2
Messaggi: 6318
Iscritto: 03/01/2010
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à.
Gemini (Normal User)
Rookie
Messaggi: 33
Iscritto: 04/10/2021
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++
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
class Networks
{
public :
//Normal Socket
bool ServerTCP( SOCKET& m_socket, SOCKET a_socket, const char * ip, int port) ;
bool ClientTCP( SOCKET m_socket, const char * ip, int port) ;
//WSASocket
bool WSAServerTCP( SOCKET m_socket, SOCKET a_socket, const char * ip, int port) ;
bool WSAClientTCP( SOCKET m_socket, const char * ip, int port) ;
bool Send( SOCKET s_socket, const char * buffer) ;
std:: string Recv( SOCKET r_socket) ;
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 ;
bool is_WSASocket = false ;
//Metodi Socket Privati
bool InitSocket( ) ;
bool CreateSocket( SOCKET m_socket, const char * ip, int port) ;
bool CloseSocket( SOCKET m_socket, SOCKET a_socket) ;
//Metodi Client Socket
bool ConnectSocket( SOCKET m_socket) ;
//Metodi Server Socket
bool BindSocket( SOCKET m_socket) ;
bool AcceptSocket( SOCKET m_socket, SOCKET a_socket) ;
} ;
Codice sorgente - presumibilmente C++
#include "Networks.h"
//===============================================//
//= ServerTCP =//
//===============================================//
bool Networks:: ServerTCP ( SOCKET& m_socket, SOCKET a_socket, const char * ip, int port)
{
is_Server = true ;
if ( ! InitSocket( ) )
return false ;
if ( ! CreateSocket( m_socket, ip, port) )
return false ;
if ( ! BindSocket( m_socket) )
return false ;
//Qui forse ci vuole un while true
if ( ! AcceptSocket( m_socket, a_socket) )
return false ;
isConnected = true ;
return true ;
}
//===============================================//
//= ClientTCP =//
//===============================================//
bool Networks:: ClientTCP ( SOCKET m_socket, const char * ip, int port)
{
is_Client = true ;
if ( ! InitSocket( ) )
return false ;
if ( ! CreateSocket( m_socket, ip, port) )
return false ;
//Qui forse ci vuole un while
if ( ! ConnectSocket( m_socket) )
return false ;
isConnected = true ;
return true ;
}
//===============================================//
//= WSAServerTCP =//
//===============================================//
bool Networks:: WSAServerTCP ( SOCKET m_socket, SOCKET a_socket, const char * ip, int port)
{
is_WSASocket = true ;
is_Server = true ;
if ( ! InitSocket( ) )
return false ;
if ( ! CreateSocket( m_socket, ip, port) )
return false ;
if ( ! BindSocket( m_socket) )
return false ;
//Qui forse ci vuole un while true
if ( ! AcceptSocket( m_socket, a_socket) )
return false ;
isConnected = true ;
return true ;
}
//===============================================//
//= WSAClientTCP =//
//===============================================//
bool Networks:: WSAClientTCP ( SOCKET m_socket, const char * ip, int port)
{
is_WSASocket = true ;
is_Client = true ;
if ( ! InitSocket( ) )
return false ;
if ( ! CreateSocket( m_socket, ip, port) )
return false ;
//Qui forse ci vuole un while
if ( ! ConnectSocket( m_socket) )
return false ;
isConnected = true ;
return true ;
}
//===============================================//
//= initWSASocket =//
//===============================================//
bool Networks:: InitSocket ( )
{
int WsaResult = WSAStartup( DllVersion, & wsaData) ;
if ( WsaResult ! = 0)
{
std:: cout << "Fallito WSAStartup()." << std:: endl ;
return false ;
}
return true ;
}
//===============================================//
//= createWSASocket =//
//===============================================//
bool Networks:: CreateSocket ( SOCKET m_socket, const char * ip, int port)
{
//Normal Socket
if ( is_WSASocket == false )
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP) ;
else if ( is_WSASocket == true ) //WSA SOCKET
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;
if ( ip == NULL )
si.sin_addr .s_addr = INADDR_ANY;
else
si.sin_addr .s_addr = inet_addr( ip) ;
si.sin_port = htons( port) ;
}
else if ( is_Client == true )
{
si.sin_family = AF_INET;
si.sin_addr .s_addr = inet_addr( ip) ;
si.sin_port = htons( port) ;
}
else
{
std:: cout << "Il socket non e' ne di tipo server ne di tipo client" << std:: endl ;
return false ;
}
return true ;
}
//===============================================//
//= closeWSASocket =//
//===============================================//
bool Networks:: CloseSocket ( SOCKET m_socket, SOCKET a_socket)
{
if ( is_Server == true )
{
if ( ! closesocket( a_socket) )
return false ;
}
if ( ! closesocket( m_socket) )
return false ;
if ( ! WSACleanup( ) )
return false ;
return true ;
}
//===============================================//
//= ConnectSocket =//
//===============================================//
bool Networks:: ConnectSocket ( SOCKET m_socket)
{
int ConnResult;
//Normal Socket
if ( is_WSASocket == false )
ConnResult = connect( m_socket, ( struct sockaddr* ) & si, sizeof ( si) ) ;
else if ( is_WSASocket == true ) //WSA SOCKET
ConnResult = WSAConnect( m_socket, ( struct sockaddr* ) & si, sizeof ( si) , NULL , NULL , NULL , NULL ) ;
if ( ConnResult == SOCKET_ERROR)
{
std:: cout << "errore %d" << WSAGetLastError( ) << std:: endl ;
CloseSocket( m_socket, 0) ;
return false ;
}
return true ;
}
//===============================================//
//= bindSocket =//
//===============================================//
bool Networks:: BindSocket ( SOCKET m_socket)
{
if ( bind( m_socket, ( struct sockaddr* ) & si, sizeof ( si) ) == SOCKET_ERROR)
{
std:: cout << "Fallito bindSocket(). Errore: " << WSAGetLastError( ) << std:: endl ;
CloseSocket( m_socket, 0) ;
return false ;
}
//Listen
if ( listen( m_socket, 3) == SOCKET_ERROR)
{
std:: cout << "Fallito listen(). Errore: " << WSAGetLastError( ) << std:: endl ;
CloseSocket( m_socket, 0) ;
return false ;
}
return true ;
}
//===============================================//
//= acceptWSASocket =//
//===============================================//
bool Networks:: AcceptSocket ( SOCKET m_socket, SOCKET a_socket)
{
int len = sizeof ( a_si) ;
int AcceptResult;
//Normal Socket
if ( is_WSASocket == false )
a_socket = accept( m_socket, ( struct sockaddr* ) & a_si, & len) ;
else if ( is_WSASocket == true ) //WSA SOCKET
a_socket = WSAAccept( m_socket, ( struct sockaddr* ) & a_si, & len,NULL ,0) ;
/*if((a_socket = WSAAccept(m_socket, (struct sockaddr*)&a_si, &len,NULL,0)) == INVALID_SOCKET)
{
std::cout << "Fallito WSAAccept(). Errore: " << WSAGetLastError() << std::endl;
CloseSocket();
return false;
}*/
if ( a_socket == INVALID_SOCKET)
{
std:: cout << "Fallito WSAAccept(). Errore: " << WSAGetLastError( ) << std:: endl ;
CloseSocket( m_socket, a_socket) ;
return false ;
}
return true ;
}
//===============================================//
//= Send =//
//===============================================//
bool Networks:: Send ( SOCKET s_socket, const char * sendbuffer)
{
int ResultSend;
if ( is_Server == true )
{
ResultSend = send( s_socket, sendbuffer, ( int ) strlen ( sendbuffer) , 0) ;
}
else if ( is_Client == true )
{
ResultSend = send( s_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 ;
CloseSocket( s_socket, 0) ;
return false ;
}
std:: cout << "Messaggio inviato!" << std:: endl ;
return true ;
}
//===============================================//
//= Recv =//
//===============================================//
std:: string Networks:: Recv ( SOCKET r_socket)
{
const int recvbufflen = 1024 ;
char recvbuffer[ recvbufflen] ;
int ResultRecv;
do
{
if ( is_Server == true )
{
ResultRecv = recv( r_socket,recvbuffer, recvbufflen, 0) ;
}
else if ( is_Client == true )
{
ResultRecv = recv( r_socket,recvbuffer, recvbufflen, 0) ;
}
else
{
std:: cout << "Nessun socket specificato!!!" << std:: endl ;
return "false" ;
}
if ( ResultRecv > 0)
{
recvbuffer[ ResultRecv] = 0 ;
//std::cout << "byte ricevuti" << ResultRecv << " " << recvbuffer << std::endl;
return recvbuffer;
}
else if ( ResultRecv == 0)
std:: cout << "Connesione chiusa!" << std:: endl ;
else
std:: cout << "Error Recv(): " << WSAGetLastError( ) << std:: endl ;
} while ( ResultRecv > 0) ;
return "true" ;
}
Codice sorgente - presumibilmente C/C++
#include "Networks.h"
int main()
{
Networks ntw;
SOCKET m_socket, a_socket;
if(!ntw.ServerTCP(m_socket, a_socket, "127.0.0.1", 1234))
{
std::cout << "Errore" << std::endl;
}
if(ntw.isConnected)
{
std::cout << "Connesso" << std::endl;
system("PAUSE");
}
return 0;
}
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++
#include <winsock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32")
bool CreateSocket( SOCKET& socketidentifier)
{
std:: cout << "ScketIdentifier: " << socketidentifier << std:: endl ;
return true ;
}
int main( )
{
SOCKET socketId;
std:: cout << "SocketID: " << socketId << std:: endl ;
CreateSocket( socketId) ;
return 0 ;
}
Ultima modifica effettuata da Gemini il 22/04/2022 alle 16:32