PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label p2p. Show all posts
Showing posts with label p2p. Show all posts

Saturday, October 22, 2022

[FIXED] How can I initiate 2 Java Sockets that listen on the same port?

 October 22, 2022     bittorrent, java, p2p, serversocket, sockets     No comments   

Issue

I am currently writing a BitTorrent client and from my understanding I can use a single local port to connect to different peers and communicate with them independently.

If I were to write a server, i.e. I would have to accept connections then I know I could use a Java ServerSocket to listen on the same port and handle connections independently otherwise. However, what I want to do however is to initiate connections rather than waiting for them to be initiated (as there is no server), and I want to use the same local port for these (so I can connect to say hundreds of peers sharing the same port). How can I correctly do this?


Solution

With a socket connections, you need to have an "initiator" (usually referred to as Client) and "acceptor" (referred to as Server), even in the case of peer-to-peer communications. That is, you can still talk about Client and Server as the roles of the different peers for this particular connection.

You can indeed reuse the local port when you act as the client (use this constructor: http://docs.oracle.com/javase/6/docs/api/java/net/Socket.html#Socket(java.net.InetAddress,%20int,%20java.net.InetAddress,%20int) ). A socket will be created (identified by Remote Address + Remote Port + Local Address + Local Port) as long as there is no other socket with the same 4 attributes (that is, you will not be able to establish a second connection to the same peer/server, but you can establish connections to other peers with the same local port).

I cannot think of a practical benefit of doing this though (as opposed to letting the system assign a random local port for you). Then again, there may be something I am not thinking of :)



Answered By - xpa1492
Answer Checked By - Mary Flores (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, April 21, 2022

[FIXED] How to build a p2p connection between 2 devices like torrent

 April 21, 2022     connection, file-sharing, p2p     No comments   

Issue

I want to build a system with 2 or more devices. Devices in the system can send files to each other using p2p, not through the server (files to be sent are not uploaded to the server)

To clear, suppose my system has 2 PCs placed in 2 different places (not LAN network and no static ip), they can share files with each other via the internet without a server. I do not want shared files uploaded to the server (no time and memory required).

Of course my system will still have a server and a database that will store data and transfer some important information between the two devices via sockets.

I searched a lot on the internet, and all I found was around the torrent network. I don't know how to build a system like torrent.

I have not decided which language or framework I will use (Java, C#, Python, NodeJS). Any suggestions on tutorial, languages, libraries, frameworks are welcome.

Thanks all


Solution

Since you have a server, it's much easier.

  1. Using your server to build a ICE structure to do NAT traversal

  2. You may need some machines to release the pressure of server when finding other nodes, so LSD and DHT are both recommanded.

  3. Using peer wire protocol to send files is really good.

There is an open source project libtorrent which may be useful for you. I think you can just use some part of it to realise your design. As for language, libtorrent is written by C++, but it's not really important.



Answered By - tyChen
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing