sockets question about multiple IPs on one computer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • manontheedge
    New Member
    • Oct 2006
    • 175

    sockets question about multiple IPs on one computer

    I've run into a problem with some socket code I have. The problem is that there are computers on my network with multiple IP addresses, and I don't know how to tell the sending socket which IP to send.

    For example, I've got one computer with say 192.168.0.1 and 192.168.1.1, and it sends the 1.1 every time, but I want the 0.1 instead ( basically I want to tell it which one to send ).

    Any ideas how I can get it to explicitly SEND the IP that I want it to send?

    I don't think it matters, but all of the computers are running XP.
  • manontheedge
    New Member
    • Oct 2006
    • 175

    #2
    I actually found / discovered the solution to my question in case anyone in the future wants to know.

    First, the IP can be set in the Create function, the 4th argument is the IP. I wasn't aware of this, since I've never had to use this arg.

    Then, to discover the multiple IPs from the local computer ...
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    #include <winsock2.h>
    #pragma comment(lib, "wsock32.lib")
    
    int main()
    {
        WSADATA WSAData;
    
        // Get local host name
        char szHostName[128] = "";
    
        // Get local IP addresses
        struct sockaddr_in SocketAddress;
        struct hostent     *pHost        = 0;
    
        char aszIPAddresses[10][16]; // maximum of ten IP addresses
    
        for(int iCnt = 0; ((pHost->h_addr_list[iCnt]) && (iCnt < 10)); ++iCnt)
        {
            memcpy(&SocketAddress.sin_addr, pHost->h_addr_list[iCnt], pHost->h_length);
            strcpy(aszIPAddresses[iCnt], inet_ntoa(SocketAddress.sin_addr));
            
            cout << aszIPAddresses[iCnt] << endl;
        }
    
        // Cleanup
        WSACleanup();
    
        system("pause");
    }
    by the way, I ran this on XP using VS.NET 2003
    Last edited by Banfa; Feb 23 '10, 04:46 PM. Reason: Removed link to competing forum

    Comment

    Working...