Pagina 1 di 4 1 2 3 ... ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 34
  1. #1

    [C++] WinSock connettersi a un' altro computer

    salve,

    è bossibile connettersi a un altro computer non connesso al tuo router attraverso le WinSock??
    se sì, come??

  2. #2
    Non mi è chiarissimo cosa intendi fare..winsock è una libreria utilizzata in casa microsoft per semplificare l'utilizzo dei protocolli dello stack tcp/ip..winsock utilizza naturalmente i socket; i socket altro non sono che dei canali tramite i quali due processi, su due macchine fisiche separate, possono comunicare utilizzando una porta..

    come si fa? http://shoe.bocks.com/net/
    o tanti altri tutorial che si trovano in giro..
    klamis.it Il primo socialnetwork per chi ama le figurine

  3. #3
    intendevo dire che se tu dai il programma client ad un tuo amico e tu sei il sever, lui inserendo il mio ip remoto no riesce a connettersi.

    c'è un modo per connettersi usando l'ip remoto??

  4. #4
    Come t'hanno detto devi procedere con i Socket (WinSock su windows) e creare un server e un client. Poi se il tuo "server" e' raggiungibile tramite IP pubblico su internet, tramite il tuo client ti riuscirai a connettere al "server" usando IP e porta.
    lolide
    Java Programmer

    Informati

  5. #5
    come faccio a rendere possibile da utilizzare l'ip pubblico??

  6. #6
    Parto dall'assunto che tu sia strasicuro che client e server funzionino (in tal caso client e server lanciati nel tuo computer devono funzionare).
    Se la porta che hai usato per il tuo server non è una standard internet (p.e. la 80) è probabile che un firewall qualunque (quello del tuo router, quello del tuo SO, etc.) la stia bloccando.
    ...

  7. #7
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Originariamente inviato da bonino33
    è bossibile connettersi a un altro computer non connesso al tuo router attraverso le WinSock??
    se sì, come??
    come faccio a rendere possibile da utilizzare l'ip pubblico??
    Probabilmente hai le idee confuse ... qual è la domanda?

    Quale difficoltà hai?

    E soprattutto, quale codice hai scritto? Cosa non va?
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  8. #8
    Originariamente inviato da Caiodark
    Parto dall'assunto che tu sia strasicuro che client e server funzionino (in tal caso client e server lanciati nel tuo computer devono funzionare).
    Se la porta che hai usato per il tuo server non è una standard internet (p.e. la 80) è probabile che un firewall qualunque (quello del tuo router, quello del tuo SO, etc.) la stia bloccando.
    come mai quando creo il sockets accendendo il server e usando la porta 80 non me lo crea??'

  9. #9
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Originariamente inviato da bonino33
    come mai quando creo il sockets accendendo il server e usando la porta 80 non me lo crea??'
    Se non fai vedere il codice non avrai molte risposte ...
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  10. #10
    main.cpp:

    codice:
    #include <iostream>
    #include "C:\Users\JeanDenajar\Desktop\socket\Socket.h"
    #pragma comment(lib, "ws2_32.lib")
    using namespace std;
    
    int main()
    {
        int scelta;
        int port = 80;
        //char *ipAddress = "127.0.0.1";
        char ipaddress[456];
    	string ipAddress;
        bool done = false;
        char recMessage[STRLEN];
        char sendMessage[STRLEN];
        cout<<"1) fai host"<<endl;
        cout<<"2) entra nel server"<<endl;
        cout<<"3) esci"<<endl;
        cin>>scelta;
        if ( scelta == 3 )
            exit(0);
        else if ( scelta == 2 )
        {
    		
            //Client
            cout<<"Inserisci l'IP: ";
            cin>>ipaddress;
    		ipAddress = ipaddress;
    		cout << ipaddress;
            ClientSocket sockClient;
            cout<<"Attendi..."<<endl;
            sockClient.ConnectToServer( ipAddress.c_str(), port );
            //Connected
            while ( !done )
            {     
                sockClient.GetAndSendMessage();
                cout<<"\t--aspetta--"<<endl;
                sockClient.RecvData( recMessage, STRLEN );
                cout<<"ricevuto > "<<recMessage<<endl;
                if ( strcmp( recMessage, "end" ) == 0 || strcmp( sendMessage, "end" ) == 0 )
                {
                    done = true;
                }
            }
            sockClient.CloseConnection();
        }
        else if ( scelta == 1 )
        {
            //SERVER
            ServerSocket sockServer;
            cout<<"attendo connessione client..."<<endl;
            sockServer.StartHosting( port );
            //Connected
            while ( !done )
            {
                cout<<"\t--aspetta--"<<endl;
                sockServer.RecvData( recMessage, STRLEN );
                cout<<"Ricevuto > "<<recMessage<<endl;
                sockServer.GetAndSendMessage();
                if ( strcmp( recMessage, "end" ) == 0 ||
                        strcmp( sendMessage, "end" ) == 0 )
                {
                    done = true;
                }
            }
        }
    }
    Sokets.cpp:

    codice:
    #include "Socket.h"
    
    Socket::Socket()
    {
        if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
        {
            cerr<<"impossibile inizializzare\n";
            system("pause");
            WSACleanup();
            exit(10);
        }
    
        //Create a socket
        mySocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
    
        if ( mySocket == INVALID_SOCKET )
        {
            cerr<<"Socket Initialization: Error creating socket"<<endl;
            system("pause");
            WSACleanup();
            exit(11);
        }
    
        Backup = mySocket;
    }
    
    Socket::~Socket()
    {
        WSACleanup();
    }
    
    bool Socket::SendData( char *buffer )
    {
        send( mySocket, buffer, strlen( buffer ), 0 );
        return true;
    }
    
    bool Socket::RecvData( char *buffer, int size )
    {
        int i = recv( mySocket, buffer, size, 0 );
        buffer[i] = '\0';
        return true;
    }
    
    void Socket::CloseConnection()
    {
    
        closesocket( mySocket );
        mySocket = Backup;
    }
    
    void Socket::GetAndSendMessage()
    {
        char message[STRLEN];
        cin.ignore();
        cout<<"Inviato > ";
        cin.get( message, STRLEN );
        SendData( message );
    }
    
    void ServerSocket::StartHosting( int port )
    {
         Bind( port );
         Listen();
    }
    
    void ServerSocket::Listen()
    {
    
        
        if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
        {
            cerr<<"Errore server\n";
            system("pause");
            WSACleanup();
            exit(15);
        }
        
        acceptSocket = accept( Backup, NULL, NULL );
        while ( acceptSocket == SOCKET_ERROR )
        {
            acceptSocket = accept( Backup, NULL, NULL );
        }
        mySocket = acceptSocket;
    }
    
    void ServerSocket::Bind( int port )
    {
        myAddress.sin_family = AF_INET;
        myAddress.sin_addr.s_addr = inet_addr( "0.0.0.0" );
        myAddress.sin_port = htons( port );
        
    
    
        if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
        {
            cerr<<"Errore server\n";
            system("pause");
            WSACleanup();
            exit(14);
        }
    }
    
    void ClientSocket::ConnectToServer( const char *ipAddress, int port )
    {
        myAddress.sin_family = AF_INET;
        myAddress.sin_addr.s_addr = inet_addr( ipAddress );
        myAddress.sin_port = htons( port );
        
        //cout<<"CONNECTED"<<endl;
    
        if ( connect( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress ) ) == SOCKET_ERROR )
        {
            cerr<<"impossibil connttersi\n";
            system("pause");
            WSACleanup();
            exit(13);
        } 
    }
    Sockets.h :

    codice:
    #include "WinSock2.h"
    
    using namespace std;
    
    const int STRLEN = 256;
    
    class Socket
    {
        protected:
            WSADATA wsaData;
            SOCKET mySocket;
            SOCKET Backup;
            SOCKET acceptSocket;
            sockaddr_in myAddress;
        public:
            Socket();
            ~Socket();
            bool SendData( char* );
            bool RecvData( char*, int );
            void CloseConnection();
            void GetAndSendMessage();
    };
    
    class ServerSocket : public Socket
    {
        public:
            void Listen();
            void Bind( int port );
            void StartHosting( int port );
    };
    
    class ClientSocket : public Socket
    {
        public:
            void ConnectToServer( const char *ipAddress, int port );
    };

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.