Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bind socket to NIC.

Status
Not open for further replies.

HyperEngineer

Programmer
May 8, 2002
190
US

I've got a question. Can I bind a socket to a specific NIC so that when I send and receive it communicates to devices connected to that NIC only?

Any help would be appreciated, even if to say it can't be done.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
Have a look at bind (Windows Sockets) in the VC++ help. If you know the NIC address, you can put it into the socket address in the call to bind.
 
Thanks for the reply xwb.

I did look at the bind function in MSDN. And I did assign the values to the sockaddr_in structure. But when I try to bind I get the WSAEADDRNOTAVAIL error. Here is the code:

Code:
CSocketDx::CSocketDx(void* arglist)
{
    m_TelnetPort = (CTelnetGeneric*) arglist;
    
    char* msg;
    unsigned long ip;
    unsigned long tcp;

    char* cIP = g_TelnetPipe->m_cIP;
    char* cTCP = g_TelnetPipe->m_cTCPIP;
    int nPort = g_TelnetPipe->m_nPort;
    int nPortNIC = nPort;

    msg = (char*) malloc(128);

    // NIC card parameters
    if((ip = inet_addr(cTCP)) == INADDR_NONE)
    {
        g_TelnetPipe->ErrorMessage("Invalid host TCP given");
        WriteToLogFile("TCP not OK\n");
        return;
    }
    else
    {
        g_TelnetPipe->ErrorMessage("Valid host TCP given");
        WriteToLogFile("TCP OK\n");
    }
    m_sockaddr_bind.sin_family = AF_INET;
    m_sockaddr_bind.sin_port = htons(nPortNIC);
    m_sockaddr_bind.sin_addr = *(in_addr*)&tcp;

    // device parameters
    if((ip = inet_addr(cIP)) == INADDR_NONE)
    {
        g_TelnetPipe->ErrorMessage("Invalid host IP given");
        WriteToLogFile("IP not OK\n");
        return;
    }
    else
    {
        g_TelnetPipe->ErrorMessage("Valid host IP given");
        WriteToLogFile("IP OK\n");
    }
    m_sockaddr_in.sin_family = AF_INET;
    m_sockaddr_in.sin_port = htons(nPort);
    m_sockaddr_in.sin_addr = *(in_addr*)&ip;

    m_hSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (m_hSocket == INVALID_SOCKET)
    {
        g_TelnetPipe->m_cLastStatus = "Failed to Create socket";
        WriteToLogFile("Failed to create socket\n");
        free(msg);
        return;
    }

    nRet = bind(m_hSocket, (sockaddr*)&m_sockaddr_bind, sizeof (sockaddr));
    if (nRet == 0)
    {
        WriteToLogFile("Bind success\n");
        g_TelnetPipe->m_cLastStatus = "Bind success";
        free(msg);
    }
    else
    {
        // we have an error
        int nWSAError = WSAGetLastError();
        sprintf(msg, "Error = %d\n", nWSAError);
        WriteToLogFile(msg);

        switch (nWSAError)
        {
            case WSANOTINITIALISED:
                msg = "Not initialized\n";
                break;
            case WSAENETDOWN:
                msg = "Network down\n";
                break;
            case WSAEADDRINUSE:
                msg = "Address in use\n";
                break;
            case WSAEADDRNOTAVAIL:
                msg = "Invalid address for computer\n";
                break;
            case WSAEFAULT:
                msg = "Name or NameLen problem\n";
                break;
            case WSAEINPROGRESS:
                msg = "Blocking or in process\n";
                break;
            case WSAEINVAL:
                msg = "Already bound\n";
                break;
            case WSAENOBUFS:
                msg = "Not enough buffer space\n";
                break;
            case WSAENOTSOCK:
                msg = "Not a socket\n";
                break;
        }
        WriteToLogFile(msg);
        g_TelnetPipe->m_cLastStatus = "Bind socket error";
        free(msg);
        return;
    }

    nRet = connect(m_hSocket, (sockaddr*)&m_sockaddr_in, sizeof(sockaddr));
    if (nRet == 0)
    {
        sprintf(msg, "Connect success - %d\n", nRet);
        WriteToLogFile(msg);
        g_TelnetPipe->m_cLastStatus = "Connect success\n";
        free(msg);
    }
    else
    {
        sprintf(msg, "Connect failure - %d\n", nRet);
        WriteToLogFile(msg);
        g_TelnetPipe->m_cLastStatus = "Connect failure\n";
    }
}

thanks for the help,

HyperEngineer
If it ain't broke, it probably needs improvement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top