QuickStart - Building HelloWorld Server - QuickServer v1.4.6
This Quick Start Guide shows you how to create a multi client "HelloWorld" server application using the QuickServer v1.4.6.
[View v1.4.5 version of QuickStart Guide]
So lets say our server will interact with the client some thing like this..
C: <open connection>
S: Hello Word!
C: hi
S: You Sent: Hi
C: quit
S: bye
S: <close connection>
So now lets start by writing a command handler that implements this
This class implements the org.quickserver.net.server.ClientCommandHandler interface as required by QuickServer. Rest of the code should be self explanatory (else refer to "Developers Guide").
Now compile this file and make a jar out of it. Make sure you include QuickServer.jar in your CLASSPATH environment variable before you try to compile.
javac mypackage/*.java
jar cvf helloworld.jar mypackage/*.class
Now we need to write our xml file to tell QuickServer about our server's configuration. Note that writing xml is not required and QuickServer can be started with out one (configurable programmatically), but it just makes it easy to use xml and configure a server.
So below is the HelloWorld.xml
So that's it, we should be ready with our server. Lets start it by giving the following command.
java -jar QuickServer.jar -load HelloWorld.xml
You should see similar output on your screen.
Now you can use any socket communication software like telnet to connect on port 1000 and comunicate with the server that we just built.
Alternatively you may use 'SocketTest' - a java based tool for testing sockets (Download/WebStart from http://sockettest.sourceforge.net/)
Below is the screen shot of a simple test using SocketTest.
Also below is a code for a simple java client for our HelloWorld server.
// HelloWorldClient.java
package mypackage;
import java.net.*;
import java.io.*;
public class HelloWorldClient {
public static void main(String args[]) {
BufferedReader br = null;
PrintWriter out = null;
Socket socket = null;
int port = 1000;
if(args.length<2) {
System.err.println("Usage : "+
"\n HelloWorldClient ");
System.exit(0);
}
try {
port = Integer.parseInt(args[1]);
System.out.print("Connecting.. ");
socket = new Socket(args[0], port);
System.out.println("Connected to "+
args[0]+":"+port+"\n");
br = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(
socket.getOutputStream())), true);
String recData = br.readLine();
System.out.println("S: "+recData);
out.println("Hi");
System.out.println("C: Hi");
recData = br.readLine();
System.out.println("S: "+recData);
out.println("quit");
System.out.println("C: quit");
recData = br.readLine();
System.out.println("S: "+recData);
} catch(Exception e) {
System.err.println("Error " + e);
} finally {
try {
if(socket!=null)
socket.close();
} catch(Exception er) {
System.err.println("Error closing: " + er);
}
}
}
}
Note that in the above QuickStart we receive and send strings that end with newline (\r\n) character. This is known as STRING mode and is the default mode of data exchange. QuickServer supports other modes of data exchange like BYTE, BINARY, OBJECT.
If you like to know about features that come with QuickServer click here.
If you need more detailed explanation on developing server's using QuickServer refer to examples that come with QuickServer and/or Developers Guide and API documentation.