Results 1 to 11 of 11
  1. #1
    john1984 is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default P2P network in Java

    Hello to everyone.

    I am trying to create a napster-like p2p network. It consists of an index server with known IP and port holding meta-data about every shared file. A peers connects to this server and searches for a file. The server gives to the peer information on which peer has the file (providing his IP and port) and then the asking peer connects with the 'target' peer and copies/downloads the file to his shared folder. The question is the following: how can I achieve peer-to-peer communication ie. not via the Server but directly from a peer to another peer. Can someone give me a clue (some lines of code) on how a process can make connections and also recieve connection calls?

    Thank you for your time!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    For one machine to connect to another it needs the IP address of the other machine. One side needs to act as server(opens socket to receive a connection) and one act as client (opens socket to connect to server).
    Look at the ServerSocket and Socket classes for making connections.

  3. #3
    john1984 is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Thank you for your reply Norm. Actually I knew the way to make conections. Let me express again my question: I want to use two different programs: one fo peers and one for server. The server program is threaded: it waits until a connection from a peer is made and then a child process is spawned and serves peer's request. At some point during peer-server communication, the peer will have to communicate with another peer to recieve the file specified by the server. This is not done via the server but rather in direct peer-to-peer communication. This means that the other peer must wait for a connection attempt (just like the server) and do nothing until another peer connects with it. This is not what I want since a peer cannot concurrently search for files and wait for requests from another peers. It is either the one or the other since method '.accept()' used by Server sockets "freezes" a process which cannot go further until a conection is made. I was wondering if peer process can be threaded but I'm having difficulties. Can someone provide a simple code solution?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    since a peer cannot concurrently search for files and wait for requests from another peers
    It could if the two tasks were on separate threads.
    If you have a server that is using threads, can you write another program that uses threads. One thread to wait on an accept and the other to do the search.

  5. #5
    john1984 is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Yes, this is excactly what I'm trying to find: a correct syntax for such a program. Can you provide some help with the code (just the thread excecution not the rest)? Write if you can a small example (not necessarily working).
    Thank you

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    There have been several examples posted on this forum.
    Use Search for Thread.
    Java Code:
    Thread t = new Thread(new Runnable() {
      public void run() {
        // do it here
      }
    });
    t.start();  // start the thread

  7. #7
    john1984 is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    I really thank you Norm for your help. I managed to find a way out.

  8. #8
    satu is offline Member
    Join Date
    Aug 2008
    Posts
    12
    Rep Power
    0

    Default

    i think JXTA is a best option to start P2P network.
    you have full control over all the standered network protocols if go through this.

  9. #9
    jason wang is offline Member
    Join Date
    Sep 2008
    Posts
    16
    Rep Power
    0

    Default

    Java has special API for p2p and you should use these API to design the program.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Can you post the link to the doc for that API? I don't remember it.
    Thanks.

  11. #11
    georgemc is offline Senior Member
    Join Date
    Sep 2008
    Posts
    135
    Rep Power
    0

    Default

    Download the source for Azureus and have a look at the torrent engine they use. If you're asking this question, you're not up to the job of writing a reliable protocol yourself. Even if you were up to it, why bother? You'll have plenty other things to worry about in your app, let someone else do the heavy lifting of socket management

Similar Threads

  1. Java Based Application to Map Network Drive
    By Wraithier in forum New To Java
    Replies: 15
    Last Post: 05-24-2012, 11:09 AM
  2. [SOLVED] executing java over network
    By jmHoekst in forum New To Java
    Replies: 6
    Last Post: 06-30-2008, 05:58 PM
  3. database+network
    By hidar in forum JDBC
    Replies: 4
    Last Post: 06-30-2008, 09:04 AM
  4. best Java Network API to use?
    By San_Andreas in forum Networking
    Replies: 1
    Last Post: 04-30-2008, 08:42 PM
  5. Getting network card address in Java 6
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-02-2008, 07:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •