package protocol_sockets.example; import java.io.*; import protocol_sockets.*; /** *

* This client makes a connection to {@link ExampleServer}, sends a short * string of information, and waits to disconnect. *

* * @author Stephen G. Ware */ public class ExampleClient extends Thread implements Receiver { /** URL of the server */ private final String url; /** Port on which to connect */ private final int port; /** The socket connected to the server */ private ProtocolSocket socket; /** *

* Main constructor. *

* * @param url URL of the server * @param port port on which to connect */ public ExampleClient(String url, int port){ this.url = url; this.port = port; } /** *

* Connect to the server and send a string of XML data. *

*/ public void run() { try{ System.out.println("CLIENT IS ATTEMPTING TO CONNECT"); // Create a socket and connect socket = new ProtocolSocket(url, port); // Register to receive input from the socket socket.addReceiver(this); // Wait for the server to tell us we're connected socket.waitForResponse(""); // Send a username and password to the server socket.send("Bobsecret"); } catch(IOException ex){ ex.printStackTrace(); } } /** *

* This method is called by a separate thread every time the socket reads * in a line of data. If the server sends a "goodbye" message, the client * closes the socket. *

*/ public void receive(String message){ if(message.equals("")){ System.out.println("CLIENT HAS BEEN DISCONNECTED"); try{ socket.close(); } catch(IOException ex){} } } }