开发者

two network interface at client side does not connect socket

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_SOC开发者_C百科KET)
            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);
}


Traffic network route in first interface,you should answer this question, why network traffic route in first (192.168.3.1) interface.

When you disable first interface traffic routing to the third interface.You must check subnet mask in both interface.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜