Jump to content

Local port Socket


photo

Recommended Posts

Hello, 

I have a problem for my paradigm with sockets. I need to open two sockets. One Listen in X port destination, this is normal and well implemented, and the other one opened and listening to the local port, of the first socket, opened by the SO . 

I tried to look for a function that returns this port but there is no one. 

I saw there was a opened ticket that expose this problem in 2018 and there isn't solution yet.

I'm working with Unigine 2.18.

So, will have unigine solved this problem soon?

Thanks.

Link to comment

Hello! 
Could you elaborate more on what "and the other one opened and listening to the local port, of the first socket, opened by the SO" means? Perhaps you could draw a schema or provide pseudocode for what you want to do?

What ticket from 2018 are you referring to?

Link to comment

Unfortunately, the ability to obtain the sender's port is still not available through the UnigineAPI.
However, you can still achieve this as described in the thread to which you refer. it looks something like this

Unigine::SocketPtr serverSocket = Unigine::Socket::create(Unigine::Socket::SOCKET_TYPE_DGRAM);

//... init socket


char buffer[1024];
sockaddr_in clientAddr;
socklen_t clientAddrLen = sizeof(clientAddr);
ssize_t bytesReceived = recvfrom(serverSocket->getFD(), buffer, sizeof(buffer), 0, (sockaddr*)&clientAddr, &clientAddrLen);
if (bytesReceived == -1) {
    Unigine::Log::error("Error receiving data\n");
    return;
}

// Get the client's port
int clientPort = ntohs(clientAddr.sin_port);
Log::message("Received data from client port: %d\n", clientPort);


Alternatively, you can use any third-party networking library with broader capabilities.

Link to comment
×
×
  • Create New...