views:

26

answers:

0

i have two network interface installed on client system interface one has ip 192.168.3.1 and interface 2 has ip 192.168.5.1 ,i want to connect to remote system which has ip 192.168.5.7 but connection not establish.when i disable network interface 192.168.3.1 it will work fine. i am using the following code

#include "windows.h"
#include "stdio.h"
#include "conio.h"
#include "winsock2.h"

bool Connect(const char *addr_name, int port)
{
    int ErrorCode=0;
    SOCKET Socket;

    ::WSAData wsa_data;
     ErrorCode = ::WSAStartup(MAKEWORD(1, 1), &wsa_data);
    if( ErrorCode != 0)
        return ErrorCode;

    // Get binary address to connect to
    u_long addr = inet_addr(addr_name); 

    if (addr == INADDR_NONE)
        ErrorCode=1;
    else
    {
        // Allocate socket
        Socket = ::socket(AF_INET, SOCK_STREAM, 0);
        if (Socket == INVALID_SOCKET)
            ErrorCode = ::WSAGetLastError();
        else
        {
            // Set up sockaddr_in for connect
            sockaddr_in sin;
            memset(&sin, 0, sizeof(sin));
            sin.sin_family = AF_INET;
            sin.sin_port = ::htons((u_short)port);
            sin.sin_addr.s_addr = addr;

            // Connect the socket to the address
            if (::connect(Socket, (sockaddr*) &sin, sizeof(sin)) == SOCKET_ERROR)
            {
                ErrorCode = ::WSAGetLastError();
                ::closesocket(Socket);
                Socket = INVALID_SOCKET;
            }
        }
    }

    return ErrorCode == 0;
}

void main(int argc,char** argv)
{
    Connect("192.168.5.7",1258);
}