TCP ports in Erlang

I use Erlang to encode the bittorrent client. I’ve been stuck for 4 days on the same issue.

I send a handshake to all my peers, each of whom has his own ip: port.

I am using gen_tcp: connect to connect.

The problem is that I cannot get an answer. I did a lot of research and used some programs to monitor incoming / outgoing connections.

Peers receive data and send a response back to the port that I used to send them a handshake.

The wise: get_tcp: connect code selects a port and uses this port to send data to the peer. The response friend responds to this port. However, as you know, in Erlang you need to use gen_tcp: listen to get a response connection, and you need to specify a port. In my case, the port I should listen on is the port that gen_tcp: connect is returning.

I can get this port number using inet: port, but the error is always the same: the port used. I understand why I am getting the error, simply because the port I'm trying to listen to is already using gen_tcp: connect. I tried closing Socket to free the port, but nothing.

So my question is: is it possible in Erlang to somehow connect to a peer and send data to it in a port, and then listen to the same port for an answer. If not, I must somehow tell the partner to send me the data back to the port that I choose.

Any ideas from all the gurus of Erlang are welcome. Thank,

//Franc.

+3
source share
2 answers

Something here sounds wrong, let me summarize what you are trying to do:

  • You are using TCP connections, not UDP

  • The TCP connection is as follows:

    Erlang side: IP1:Port1   <---->  Peer side IP2:Port2
    
  • IP2: Port2 - , gen_tcp: connect, IP1 - IP- , Port1, , , TCP .

  • ,

    {tcp, Socket, Data}
    

    (, , , ). , : gen_tcp: recv og .

  • gen_tcp: . , , . FTP, ( , , ). , . FTP . , .

+6

Bittorrent :

  • , gen_tcp:connect. , - , .

  • . gen_tcp:listen, gen_tcp:accept. , , .

, , , "" . , TCP-.

, . , . , . , , . info_hash , , - , .

+1

Source: https://habr.com/ru/post/1774317/


All Articles