This tutorial shows the use of the Client / Server implementation inside eEngine.
Client / Server tutorial makes no sense as WebStart
package de.esw.eengine.test.t8_net;
import java.io.IOException;
import java.nio.channels.SelectionKey;
import de.esw.eengine.EEngineException;
import de.esw.eengine.net.TCPServer;
/**
* @author markusw
* @version $Id: Server.java 142 2005-06-29 11:31:02Z markusw $
* @since 1.0
*/
public class Server extends TCPServer {
/**
* @throws EEngineException
*/
public Server() throws EEngineException {
super(100, "US-ASCII", "localhost", 5000);
}
/**
* @see de.esw.eengine.net.AbstractServer#processMessage(java.nio.channels.SelectionKey)
*/
@Override
protected void processMessage(SelectionKey key) throws IOException {
String command = this.readString();
System.out.println("Read from channel: " + command);
if ("shutdown".equals(command)) {
this.isProcessing = false;
} else {
// We are interested in answering
this.setWriteRequest(key, true);
}
}
/**
* @see de.esw.eengine.net.AbstractServer#composeMessage(java.nio.channels.SelectionKey)
*/
@Override
protected void composeMessage(SelectionKey key) throws IOException {
this.writeString("World");
// We are done writing
this.setWriteRequest(key, false);
}
/**
* @param args
*/
public static void main(String[] args) {
try {
Server server = new Server();
server.run();
server.dispose();
} catch (EEngineException e) {
e.printStackTrace();
}
}
}
package de.esw.eengine.test.t8_net;
import de.esw.eengine.net.TCPChannel;
/**
* @author markusw
* @version $Id: Client.java 142 2005-06-29 11:31:02Z markusw $
* @since 1.0
*/
public class Client {
/**
* @param args
*/
public static void main(String[] args) {
try {
TCPChannel client = new TCPChannel(100, "US-ASCII", "localhost",
5000, true);
System.out.println("Question: Hello");
client.beginMessage();
client.writeString("Hello");
client.sendMessage();
client.recieveMessage();
System.out.println("Answer: " + client.readString());
client.beginMessage();
client.writeString("shutdown");
client.sendMessage();
// Shutdown client
client.dispose();
} catch (Exception e) {
e.printStackTrace();
}
}
}