Can I find the port number if I only have SOCKET?

Can I find the port number if I only have SOCKET? I am connecting ws2_32 calls and it seems that SOCKET is trying to send data, but I have no information about this SOCKET. I want to know if there is anyway to get sockaddr or port only from SOCKET?

I had a similar problem with UDP socket, but I fixed it by simply getting the port number from sockaddr when it was about to send data and updated my information about it. But the problem is that I have nothing to hide except SOCKET.

The program makes a SOCKET with port 0 and later assigns it to a new port. By the time this happens, my program information in the juice is already set with port 0, which is not correct. So, I need to find a way to either ... A: Find a port based on SOCKET, or B: When windows update a port, I can connect that port and update the port.

+3


source to share


1 answer


There are two functions that do exactly what you want:



  • getpeername

    - gets information about the other end of the TCP connection.
  • getsockname

    - gets socket information (local end) for TCP and UDP
+3


source







All Articles