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");
}
Leave a comment: