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

CodePudding user response:

The
reference 3 floor smwhotjay response:
client yourself how to get to the port? Getsockname

Excuse me: client calls getsockname after getting the [randomly assigned port number] or the [10080] when connect to the server?

CodePudding user response:

Getsockname is to get connected to the server port number, also is a time when connected [10080] of the port, can't get to the [8679], is there any way to get to 8679?

CodePudding user response:

This is the network IP? If the server program is also make it yourself, then communication back and try other access to return the IP address of the server (such as http://pv.sohu.com/cityjson? Ie=utf-8)


CodePudding user response:

From the client side is unable to get to their random distribution connection port of the server side

CodePudding user response:

The server returns the client port get)

CodePudding user response:

To get himself in the server port, it is estimated that only a custom application layer protocol, let returned by the server.
  • Related