Extract UDP IP address from UDP message in ofxUDPmessage

I need to find the ip of where a UDP message comes from.

I try:

client.ip = udpConnection.GetRemoteAddr(udpMessage);

(whereas client.ip is a string) and the result is null.

if I do:

cout << udpMessage << endl; 

I get the ip address in output.

How do I assign the ip address to a string/char?

Thanks,
L.C.

the function GetRemoteAddr has the following signature:

bool ofxUDPManager::GetRemoteAddr(char* address)

That means that when you pass udpMessage to the function, the function will “fill” the memory pointed to by that udpMessage pointer with a string representing the IP. It will then return “true” if it is successful and “false” if it is unsuccessful. It doesn’t return the string, but a boolean representing success or failure getting the remote address. Thus your client.ip variable is likely invalid.

If you want to save the address to a std::string you might do something like:

std::string ipAddress = "";

char buffer[1024];

bool success = udpConnection.GetRemoteAddr(buffer);

if (success == true)
{
    ipAddress = std::string(buffer);
}
else
{
   // failure
}

std::cout << "Ip Address: " << ipAddress << std::endl;

Also, check out this tutorial, particularly the section about pointers as function parameters.

Cheers!

Thanks a ton! This is exactly what I needed.

Cheers!

1 Like