Peer

ITP HELP | RecentChanges | Preferences

Peer Listing

A server ususally performs the function of introducing clients to each other. In order for peers to know where to look for each other we are still going to use a server for an initial introduction. I have provided a little [servlet] for telling you who is currently connected to the peer network. You don't have to write anything to use my applet, you just point your java application towards it's url to use it. I provide the source just for your reference. After this introduction the server bows out.

You can communicate with this servlet at the address:

String servletAddress = "http://stage.itp.nyu.edu:8080/dano/servlet/PeersListServlet";

via http which is easily done in java:

//when you fist come on

URL myURL = new URL(servletAddress + "?action=add&address=" + myIPAndPort + "&app=" + app + "&timeout=" + timeout); //now you listen for what comes back, see the code

//remind the servlet that you are still interested in peers

  URL myURL = new URL(servletAddress + "?action=refresh&address=" + myIPAndPort + "&app=" + app);

//take yourself off the list URL myURL = new URL(servletAddress + "?action=remove&address=" + myIPAndPort + "&app=" + app);

As you can see have to pass along a couple of parameters. Of course you have to send your ip and port so the others can find you. Send a timeout parameter so we can clean up after dead applicatons. The most important parameter is "app" Because this servlet is being used by everyone in the class, app allows you to filter out hearing about the peers of other people. In your code you should give yourself a unique app name:

String app = "changeThisMaybeNeID" + getClass().getName();

You debug things directly in a web browser by typing in these lines:

  1. http://stage.itp.nyu.edu:8080/dano/servlet/PeersListServlet?action=listWeb
  2. http://stage.itp.nyu.edu:8080/dano/servlet/PeersListServlet?action=clear&app=dbo3Vid

UDP vs TCP

UDP is much easier because it is by nature hermaphroditic. With TCP I have the each application act as client for all the existing people and as a server for all the people who come later. A little more complicated.

Firewalls and NATs

Peer to peer connections are difficult in network environments with strict firewalls (inside corporations) and environments that use NAT's to share a single ip (home networks). There are [ways] of getting around this. You can play with peer to peer easily in the ITP network environment. If you want to go further you might consider using the JXTA [P2P Sockets] to get around these and other problems. The peer to peer example supplied will not work on ITP's wireless network, so you should work on a wired machine or ask Nancy to work on the "sandbox" wireless system. The best resource for working around NATs and firewalls is Daniel Kantor.

Serialization

   ObjectOutputStream objOut = new ObjectOutputStream(socket.getOutputStream());
   objOut.write(myOb);  //send it down the line

   ObjectInputStream objIn = new ObjectInputStream(socket.getInputStream());
   newObj = objIn.readObject();  //and voila it comes out the other side

Inner Classes


public class OutsideClass {
   bla
   bla
   bla going about my Outside class business

   public class ListeningThread implements Runnable { //this just shows up like a method

     public void run() {
            .....Ya I get to use all the stuff from the outside class
    }//end of the inner class ListeningThread
}//end of outside class

New Threading

 //This is a new way to make threads
        listeningThread = new Thread(new ListeningThread());
        listeningThread.start();

ITP HELP | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited March 14, 2005 12:02 pm (diff)
Search:
To EDIT, You have to enter an ADMINISTRATOR password (guess) in Preferences. And refresh