Home > Software engineering > In TCP, after the success of the client connection how to obtain their own port?
In TCP, after the success of the client connection how to obtain their own port?
Time:09-20
Client connection to the server port 10080, see the client on the server IP connection is [25.77.148.91:8679],
This IP is the client's IP, and the port number 8679 is randomly assigned to the client, the port number the server can get, but the client how to get to the port? What kind of API to obtain?
CodePudding user response:
Getpeername is to accept return of the socket for the other side of the TCP/IP port
Struct sockaddr_in sa;
Int len=sizeof (sa);
Getpeername (AcceptSocket, (struct sockaddr *) & amp; Sa, & amp; Len); Printf (" the other IP % s: % d ", inet_ntoa (sa) sin_addr), ntohs (sa) sin_port));
CodePudding user response:
getsockname
CodePudding user response:
The client himself how to get to the port? getsockname