ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Introduction to the Peer-to-Peer Sockets Project
Pages: 1, 2, 3, 4

Using a P2P Socket to Contact a P2P Server Socket

Creating and using a P2P socket to connect to the P2P server socket you created is just as easy.



First, sign into your peer-to-peer network:

// sign into the peer-to-peer network,
// using the username "clientpeer", the password "clientpeerpassword",
// and find the peer-to-peer network named "TestNetwork"
java.net.P2PNetwork.signin("clientpeer", "clientpeerpassword", "TestNetwork");

Next, create your P2P socket with the host name and port to which you want to connect:

// create a socket to connect to the domain
// "www.nike.laborpolicy" on port 100
java.net.Socket socket = new java.net.P2PSocket("www.nike.laborpolicy", 100);

Now you can easily communicate with the server:

// now communicate with this server
java.io.DataInputStream in = new DataInputStream(socket.getInputStream());
java.io.DataOutputStream out = new DataOutputStream(socket.getOutputStream());
String results = in.readUTF();
System.out.println(results);
out.writeUTF("Hello server world!");

Here is the full source code for the client:

import java.io.InputStream;
import java.io.OutputStream;
import java.io.DataOutputStream;
import java.io.DataInputStream;
import java.net.Socket;
import java.net.P2PSocket;
import java.net.P2PNetwork;

public class ExampleClientSocket {
   public static void main(String args[]) {
      try {
         // sign into the peer-to-peer network,
         // using the username "clientpeer", the password "clientpeerpassword",
         // and find a network named "TestNetwork"
         System.out.println("Signing into the P2P network..");
         P2PNetwork.signin("clientpeer", "clientpeerpassword", "TestNetwork");
		
         // create a socket to connect to the
         // domain "www.nike.laborpolicy" on port 100
         System.out.println("Connecting to server socket! at " + "www.nike.laborpolicy:100...");
         Socket socket = new P2PSocket("www.nike.laborpolicy", 100);
		 System.out.println("Connected.");
		 
         // now communicate with this server
         DataInputStream in = new DataInputStream(socket.getInputStream());
         DataOutputStream out = new DataOutputStream(socket.getOutputStream());
         String results = in.readUTF();
         System.out.println("Message from server: " + results);
         out.writeUTF("Hello server world!");
		 
         // shut everything down
         socket.close();
      }
	  
      catch (Exception e) {
         e.printStackTrace();
         System.exit(1);
      }
   }
}

Running the P2P Server Socket and Socket Examples

Open two separate shell windows. In both windows, type the following to set some system variables needed by the Ant makefiles (modify the variables below to point to where you have installed P2P Sockets and your JDK, respectively):

set p2psockets_home=c:\p2psockets
set JAVA_HOME=c:\j2sdk1.4.1

In both windows, change to the p2psockets directory where you installed the P2P Sockets package. The example source files are in p2psockets/src/examples and are named ExampleClientSocket.java and ExampleServerSocket.java, respectively. Everything has already been compiled into the p2psockets/dist/examples directory, but if you want to compile them again, enter the following in one of the two shells:

ant clobber build jar

To run the client and server, in one window enter the following to start the example P2P server socket:

ant example-serversocket-run

You should see the following printed in the server peer window:

Buildfile: build.xml
example-serversocket-run:
Signing into the P2P network...
Using the application peer group name TestNetwork
Waiting for RendezVous Connection.........
Finished connecting to RendezVous.
Creating server socket for
www.nike.laborpolicy:100...
Waiting for client...

In the other window, enter the following to start the example P2P client socket:

ant example-clientsocket-run

You should see the following printed in the client peer window:

Buildfile: build.xml
example-clientsocket-run:
Signing into the P2P network..
Using the application peer group name TestNetwork
Waiting for RendezVous Connection......
Finished connecting to RendezVous.
Connecting to server socket at
www.nike.laborpolicy:100...
Connected.
Message from server: Hello client world!

In the server window, you should now see the following:

Buildfile: build.xml
example-serversocket-run:
Signing into the P2P network...
Using the application peer group name TestNetwork
Waiting for RendezVous Connection.........
Finished connecting to RendezVous.
Creating server socket for
www.nike.laborpolicy:100...
Waiting for client...
Client Accepted.
Message from client: Hello server world!

Congratulations! You've just created a simple peer-to-peer network with very little work or extra knowledge needed above understanding basic Java sockets and server sockets.

If you have trouble, ensure that you are connected to the Internet, that you are running Java 1.4+, and that you did not install the P2P Sockets package into a directory with spaces in its name (such as in Program Files/p2psockets). You might also have to delve into the JXTA Configurator if your machine is in an unusual network configuration. If you are attempting to run the client and server P2P socket examples across separate machines that are blocked by NAT devices, you might run into an open bug that P2P Sockets currently has.

Finding Out if a Host Name, IP Address, or Port is Taken

By default, the P2PInetAddress and P2PServerSocket classes don't check to make sure a given host name, IP address, or port is taken. The reason for this is so that application developers can use P2P server sockets in unique ways that are not possible with normal sockets. For example, different peers can start up P2P server sockets for the same host name and port in order to provide failover and scalability. If we were to throw an exception when a server socket was started because the address was already bound, then this possibility would be precluded.

However, methods are provided to check if a given host name, IP address, or port is already taken. These are static methods on the class java.net.P2PNameService. Examples are shown below:

boolean nameAvailable = P2PNameService.isHostNameTaken("www.nike.laborpolicy");
boolean portAvailable = P2PNameService.isPortTaken("www.nike.laborpolicy", 80);
boolean addressAvailable = P2PNameService.isIPAddressTaken("33.44.74.12");

Use these before creating your P2P server sockets if you want your server to be a unique instance (read the Limitations and Security Concerns section, though, on P2P domain-name spoofing).

You have now learned the bulk of working with P2P sockets and server sockets. The following sections detail technical information on how the P2P Sockets package provides full compatibility with standard TCP/IP sockets and server sockets. You don't necessarily need to know this information, but should know it if you will be working with the InetAddress class or the Loopback (127.0.0.1) or Any (0.0.0.0) addresses. If you skip these sections, please make sure to read the section Limitations and Security Concerns at the end of this article.

Pages: 1, 2, 3, 4

Next Pagearrow