User Guide
Socket connections 389
{
socket = new ServerSocket(port);
incoming = socket.accept();
readerIn = new BufferedReader(new
InputStreamReader(incoming.getInputStream()));
printOut = new PrintStream(incoming.getOutputStream());
printOut.println("Enter EXIT to exit.\r");
out("Enter EXIT to exit.\r");
boolean done = false;
while (!done)
{
String str = readerIn.readLine();
if (str == null)
{
done = true;
}
else
{
out("Echo: " + str + "\r");
if(str.trim().equals("EXIT"))
{
done = true;
}
}
incoming.close();
}
}
catch (Exception e)
{
System.out.println(e);
}
}
private void out(String str)
{
printOut.println(str);
System.out.println(str);
}
}
Save the document to your hard disk as SimpleServer.java and compile it using a Java
compiler, which creates a Java class file named SimpleServer.class.
You can start the XMLSocket server by opening a command prompt and typing
java
SimpleServer
. The SimpleServer.class file can be located anywhere on your local computer
or network; it doesn’t need to be placed in the root directory of your web server.
TIP
If you’re unable to start the server because the files are not located within the Java
classpath, try starting the server with
java -classpath . SimpleServer.