Iowa15
Iowa15

Reputation: 3079

Winsock C++ App not working on some networks

This code works fine on my network but on my friend's network. The client can't connect to the host on my friend's. The default gateway on my network is 10.0.1.1, subnet mask is 255.255.255.0. On my friend's, the default gateway is 192.168.1.1, and the subnet mask is the same. By the way, these are the IPs for the Wireless LAN adapter on the Wireless Network Connection. On my friend's network, both the client socket and the server socket initialize fine, but they don't connect to each other. If you run the host and client on the same computer, they will connect, but not on different computers. One my network, both computers are running windows 7, on his, one is running win7 on one is running win Vista

//Socket.h
#pragma once
#include <iostream>
#include "WinSock2.h"

 using namespace std;

 const int STRLEN = 256;

 class Socket
 {
     protected:
         WSADATA wsaData;
         SOCKET mySocket;
         SOCKET myBackup;
         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 );
 };






//Socket.cpp
#include "Socket.h"

    Socket::Socket()
{
    if( WSAStartup( MAKEWORD(2, 2), &wsaData ) != NO_ERROR )
    {
        cerr<<"Socket Initialization: Error with WSAStartup\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);
    }

    myBackup = 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()
{
    //cout<<"CLOSE CONNECTION"<<endl;
    closesocket( mySocket );
    mySocket = myBackup;
}

void Socket::GetAndSendMessage()
{
    char message[STRLEN];
    cin.ignore();
    cout<<"Send > ";
    cin.get( message, STRLEN );
    SendData( message );
}

void ServerSocket::StartHosting( int port )
{
     Bind( port );
        Listen();
}

void ServerSocket::Listen()
{
    //cout<<"LISTEN FOR CLIENT..."<<endl;

    if ( listen ( mySocket, 1 ) == SOCKET_ERROR )
    {
        cerr<<"ServerSocket: Error listening on socket\n";
        system("pause");
        WSACleanup();
        exit(15);
    }

    //cout<<"ACCEPT CONNECTION..."<<endl;

    acceptSocket = accept( myBackup, NULL, NULL );
    while ( acceptSocket == SOCKET_ERROR )
    {
        acceptSocket = accept( myBackup, 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 );

    //cout<<"BIND TO PORT "<<port<<endl;

    if ( bind ( mySocket, (SOCKADDR*) &myAddress, sizeof( myAddress) ) == SOCKET_ERROR )
    {
        cerr<<"ServerSocket: Failed to connect\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<<"ClientSocket: Failed to connect\n";
        system("pause");
        WSACleanup();
        exit(13);
    } 
}

Upvotes: 0

Views: 572

Answers (1)

Hakan Serce
Hakan Serce

Reputation: 11256

Most modern wireless access points provide a "wireless isolation" functionality, which prevents wireless hosts to see another host on the network (apart from the hotspot itself). This is a security feature mostly enabled by default. As far as I can see this is the most probable reason for your problem. Just check your wireless AP settings and disable isolation.

Upvotes: 2

Related Questions