Mr X
Mr X

Reputation: 354

Setting destination port with TCP socket

In the case where the source port and destination port of a non-blocking TCP socket are not in agreement(for a p2p application), how does one specify the destination port that the socket will Connect() to?

Upvotes: 0

Views: 426

Answers (2)

Kristian Evensen
Kristian Evensen

Reputation: 1345

Specifying the destination port of a connection is done in the sockadder-struct that you pass to the connect()-call. Since you don't specify which OS you are working on, in it is difficult to provide a concrete example. However, for both Linux and Windows, you would typically load the sockaddr-struct using getaddrinfo(). A nice Linux-example is provided here, while MS has an example in their official documentation.

Upvotes: 1

Pradheep
Pradheep

Reputation: 3819

In case of P2P the clients first connect to torrent to get a list of peers(seeders) and the port number which is open at the peer. The system which wants to download will then connect to the peer using the address and port provided by the tracker.

The peers are not restrictive(generally) and accept connections from any source address or ip

Upvotes: 1

Related Questions