Retrieving Port on Client When Created with 0
Hey, I was hoping someone might be able to help me out with this simple question. When I create the client port I set the port to 0 and save off the value in a sockaddr_in struct as part of my network class. Later when I create messages I want to be able to retrieve this value from the member and tack it onto my packet to help the server with address translating routers, but when I go to retrieve it, the sin_port member still returns 0. Does anyone know of a way that I can get at the actual port on the client side and save it off...or am I going to have to define a port to be able to accomplish this? Thanks, Permafried-
getsockname()
Quote:
The getsockname function retrieves the current name for the specified socket descriptor in name. It is used on the bound or connected socket specified by the s parameter. The local association is returned. This call is especially useful when a connect call has been made without doing a bind first; the getsockname function provides the only way to determine the local association that has been set by the system.
On call, the namelen parameter contains the size of the name buffer, in bytes. On return, the namelen parameter contains the actual size in bytes of the name parameter.
The getsockname function does not always return information about the host address when the socket has been bound to an unspecified address, unless the socket has been connected with connect or accept (for example, using ADDR_ANY). A Windows Sockets application must not assume that the address will be specified unless the socket is connected. The address that will be used for the socket is unknown unless the socket is connected when used in a multihomed host. If the socket is using a connectionless protocol, the address may not be available until I/O occurs on the socket.
Jon,
Thanks for the help that did the trick. I was hoping I could pass the internal port as part of the message in case of translating firewalls, but it seems when I try to send a message back to the client using the extracted port it never gets there, but when I use the raw sockaddr_in that I pick off from recvfrom it works fine....strange problem but I should be on the right track.
Thanks again,
Permafried-
Thanks for the help that did the trick. I was hoping I could pass the internal port as part of the message in case of translating firewalls, but it seems when I try to send a message back to the client using the extracted port it never gets there, but when I use the raw sockaddr_in that I pick off from recvfrom it works fine....strange problem but I should be on the right track.
Thanks again,
Permafried-
You absolutely should use the value you get from recvfrom(). Data that the client itself sees is not necessarily valid on the other end of the connection.
Here's why.
Here's why.
enum Bool { True, False, FileNotFound };
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement