Determining the TCP port number to which the client is bound

I am creating a TCP socket without worrying that the port number is bound to [socket.sin_port = 0]. How later, if I want to print the client port number, how do I do this? The C client application (on Linux) creates many clients that connect to the server. To debug problems, I capture traffic on the air. I was thinking of printing the port number in the logs while a problem occurs, so filtering on the air becomes easy.

Any help would be appreciated.

-Prabhu

+5
source share
3 answers

Use the getsockname () call to get the socket address and port after a successful connection.

: . !

+10

, getsockname(). , , (, connect()). (, bind()), getpeername() .

, , , netstat sockstat, . , Linux ( ), man sockstat. , , . grep, , Ethereal. !

+2
lsof -p <process id> | grep TCP
0

All Articles