QuickServer Development Guide (3) - Build an echoserver

xiaoxiao2021-03-06  43

A good way to learn how to use the Quick Server library is to learn what it provides. There are many typical examples in the Examples folder under the QuickServer installation path.

In the following sections, we imitate one of the examples EchoServer to build a server. EchoServer is a simple TCP server that returns the character string sent by the user to the prefix "Echo:". Although this example is not available, it is a good demonstration for all characteristics of QuickServer. We started from building a basic server and add new features to it later.

Code

First implement the most basic function of EchoServer: returns the character string sent by the user to "Echo:".

Create a folder in the locally, such as building the EchoServer folder in C: / Projects /, then create a class Echoserver.java:

01 Package EchoServer;

02

03 Import Org.quickServer.net. *;

04 Import Org.quickServer.net.server. *;

05

06 Import Java.io. *; 07

08 Public Class EchoServer {

09 public static void main (string s []) {

10 Quick Server MyServer =

11 New QuickServer ("echoServer.echocommandhandler);

12 MyServer.Setport (4123);

13 MyServer.setName ("EchoServer V 1.0");

14 try {

15 myserver.startserver ();

16} catches (APPEXCEPTION E) {

17 System.err.Println ("Error In Server:" E);

18}

19}

20}

A QuickServer object MyServer is defined in Chain 10 and 11, which declares that the class to be loaded is declared through a String object "EchoServer.echocommandhandler", which is a command processor for all clients to implement org.quickServer.net. Server.ClientCommandhandler interface, we are about to create.

Chapter 12 sets a server port to do monitor, then set the name of the entire application (line 13). Last start service (line 15).

Next, create a class echocommandrandler.java that implements the org.quickserver.net.server.clickserver.net.server.clientCommandhandler interface to handle the command sent by the server.

01 // echocommandhandler.java

02 Package EchoServer;

03

04 Import java.net. *;

05 import java.io. *;

06 Import Org.quick Server.Net.server.clientCommandHandler;

07 import org.quickServer.net.server.clienthandler;

08

09 Public Class Echocommandhandler Implements ClientCommandhandler {

10

11 public void gotconnected (ClientHandler Handler)

12 throws sockettimeoutexception, ioException {13 Handler.sendClientmsg ( ");

14 Handler.sendClientmsg ("| Welcome to EchoServer V 1.3 |");

15 Handler.sendClientmsg ("| send 'quit' to exit |");

16 Handler.sendClientmsg ( );

17}

18 Public Void LostConnection (ClientHandler Handler)

19 thrtows oException {

20 Handler.sendsystemmsg ("Connection Lost:"

21 Handler.getsocket (). Getinetaddress ());

twenty two }

23 Public Void ClosingConnection (ClientHandler Handler) 24 THROWS IOEXCEPTION {

25 handler.sendsystemmsg ("Closing Connection:"

26 Handler.getsocket (). Getinetaddress ());

27}

Twist

29 Public Void HandleCommand (ClientHandler Handler, String Command)

30 throws sockettimeoutException, ioException {

31 IF (Command.equals ("Quit")) {

32 Handler.sendClientmsg ("bye ;-)");

33 Handler.CloseConnection ();

34} else {

35 Handler.sendClientmsg ("Echo:" Command;

36}

37}

38}

According to QuickServer requirements, this class must implement the ClientCommandrandler interface. When the client establishes a connection (11 line), the GotConnected () method is called. In this method, we send a welcome text (13-16 rows) to the client, which is sent to the client with the SendClientMSG () method through ClientHandler. We will also use the ClientHandler's SendSystemMessage () method to display the InetAddress (20-21, 25-26 rows) of the client. The handlercommand () method is the core method of the ClientCommandrandler interface because the method is called when the server receives any commands sent by the client. In the implementation of this method, we will check if the command is "quit" (31 line), if so, we will send some prompt text indicate that the server will close the connection and then close the connection (33 line). Otherwise, return the command to the prefix "Echo:" to the user. 2. Run and test o Run the command prompt program (cmd.exe) o Enter the root directory where the code is located, such as C: / Projectso compile code javac echoserver / *. Javao If there is no compilation error, run the server: set classpath =% ClassPath%; D: /quickServer/dist/quickServer.jar; ./ (Class Folder) Java EchoServer.EchoServero You will see the following information: o Tests if our server can work properly. Run a CMD program, enter the directory where the sockettest.jar is located, type the java -jar sockettest.jar command, pop up a window. Enter "127.0.0.1" in IP Address, enter "4123" in the Port, click the "Connect" button, will see the information as shown below. If you use a Telnet, type command: Open localhost 4123 Enter some strings in Message, click the "Send" button, the browser will return a string that adds prefix "Echo:". Send "Quit", the server is disconnected.

转载请注明原文地址:https://www.9cbs.com/read-54016.html

New Post(0)