S
S
snegirev_news2018-05-23 10:45:09
Python
snegirev_news, 2018-05-23 10:45:09

Connection between two computers via socket?

Attempts to implement a p2p network.
There are 2 computers connected to the network through a router, for example. And there is a server with a white ip.
Computers open one connection for eavesdropping (port 5005 and 5006, for example), and connect to the server with the second connection.
The server receives Hello messages from computers and remembers the sender's address and port, let's say this is (ip-addr port:39392 and ip-addr port:21213). Then he sends this information to all the peers known to him.
And when from the first computer, I try to connect to the second computer using the received ip and port, then there is no reaction.
What are the solutions?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
res2001, 2018-05-23
@res2001

Apparently NAT interferes. In such a case, NAT interferes with everyone.
You need to either configure NAT on the gateway (port forwarding), or exchange traffic through the server, and not directly.
If the application is "for internal use" or for a narrow circle of people, then it is usually not a problem to set up NAT, but if mass distribution is expected, then this is not an option and workarounds must be sought.
For the UDP protocol, there is an option to use an external STUN server. For TCP, in my opinion, this option does not work.

V
Vladimir, 2018-05-23
@vintello

nevertheless, I think that it would be nice to read about p2p again,
wikipedia helps a lot

There are a number of machines on the network, each of which can communicate with any of the others. Each of these machines can send requests to other machines to provide some resources within this network and thus act as a client. As a server, each machine must be able to process requests from other machines on the network, send out what was requested. Each machine must also perform some auxiliary and administrative functions (for example, keep a list of other known "neighbors" machines and keep it up to date).

Y
Yaroslav, 2018-05-23
@yaror

Let's do it again.
On which port do you start listening for incoming connections?
Everything, using TCP as an example, should work like this:
1. The client establishes a connection to the public server:
192.168.1.100:12345 -> 1.2.3.4:80
Port 12345 from the client side is allocated, as a rule, automatically by the client operating system.
2. NAT allocates a public pair Ip-address:port for this connection, and remembers it:
(client) 192.168.1.100:12345 -> (NAT)(5.6.7.8:11111) -> (server)1.2.3.4:80
Respectively , from the point of view of the server, they connected to it from a pair of address:port 5.6.7.8:11111
3. Now, attention!
The client, waiting for incoming connections, starts listening on port 12345, since it is for this port that a hole has been punched in the NAT.
This technology is called: Hole Punching.
About other ports open on this client, NAT does not know anything.
4. Someone outside establishes a TCP connection with the address:port pair 5.6.7.8:11111.
NAT knows that behind the public pair 5.6.7.8:11111, in fact, the "gray" pair 192.168.1.100:12345 is hidden, it replaces the destination address and destination port from 5.6.7.8:11111 to 192.168.1.100 in the TCP SYN packet: 12345 and sends this ip packet to the client.
The client, having received a connection request on port 12345, confirms the connection.
Accordingly, there are three points worth checking:
1. After establishing a connection with an external server, the client must raise a Listening Socket on the same source TCP port with which a connection was established with an external server
2. Hole Punching functionality must be enabled on the NAT
3. To successfully establish a connection between the nodes behind the NAT at their public addresses, the Hairpinning functionality responsible for this should be enabled on the NAT.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question