[xSocket-develop] basic question about read data from readByteBufferByDelimiter
Status: Inactive
Brought to you by:
grro
|
From: beijing w. <wel...@gm...> - 2008-10-29 13:41:00
|
hi, xSocket develop folks,
This might be a very basic question, I want to read a file by stream and
send to server, then do something, however, ByteBuffer[] in
readByteBufferByDelimiter is empty in server side.
paste the code below.
Thank you in advance!
=============== MServer.java ==============
package mserver;
import java.io.Closeable;
import java.io.IOException;
import java.net.InetAddress;
import java.util.HashSet;
import java.util.Set;
import org.xsocket.connection.IConnectHandler;
import org.xsocket.connection.IHandler;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IServer;
import org.xsocket.connection.Server;
import org.xsocket.connection.ConnectionUtils;
import org.xsocket.connection.IConnection.FlushMode;
import mserver.MServerHandler;
public class MServer implements Closeable {
private IServer server = null;
public MServer(int listenPort) throws Exception {
IHandler hdl = new MServerHandler();
server = new Server(listenPort, hdl);
server.setFlushMode(FlushMode.ASYNC); // performance improvement
ConnectionUtils.start(server);
ConnectionUtils.registerMBean(server);
}
public static void main(String... args) throws Exception {
int port = 8888;
new MServer(port);
}
public InetAddress getLocalAddress() {
return server.getLocalAddress();
}
public void close() throws IOException {
if (server != null) {
server.close();
}
}
}
==============================================
=================== MServerHandler.java ===========================
package mserver;
import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import org.xsocket.Execution;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IConnectHandler;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IConnection.FlushMode;
@Execution(Execution.NONTHREADED)
public class MServerHandler implements IConnectHandler, IDataHandler {
public static final String DELIMITER = "\r\n";
static Charset charset = Charset.forName("gb2312");
static CharsetDecoder decoder = charset.newDecoder();
static CharBuffer charBuffer = null ;
public boolean onConnect(INonBlockingConnection connection) throws
IOException, BufferUnderflowException, MaxReadSizeExceededException {
connection.setFlushmode(FlushMode.ASYNC);
connection.setAutoflush(false);
// connection.write("Welcome to MServer!");
System.out.println(connection.getRemoteAddress() + " connected!");
return true;
}
public boolean onData(INonBlockingConnection connection) throws
IOException, BufferUnderflowException {
ByteBuffer[] buffer =
connection.readByteBufferByDelimiter(DELIMITER, Integer.MAX_VALUE);
// connection.write("Server: ");
connection.write(buffer);
connection.write(DELIMITER);
for (int j = 0; j < buffer.length; ++j) {
buffer[j].flip();
charBuffer = decoder.decode(buffer[j]);
// System.out.println( " charBuffer= " + charBuffer);
// this line is empty!
System.out.println(charBuffer.toString());
System.out.println(j);
}
connection.flush();
System.out.println("====");
return true;
}
}
=========================================
================= MServerClient.java ========================
package mserver;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.Socket;
public class MServerClient {
public void startClient() throws Exception {
int port = 8888;
Socket client = new Socket("localhost", port);
// next step is send a file here.
client.getOutputStream().write("test\r\n".getBytes());
LineNumberReader lnr = new LineNumberReader(
new InputStreamReader(client.getInputStream()));
String response = lnr.readLine();
System.out.println(response);
lnr.close();
client.close();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MServerClient client = new MServerClient();
try {
client.startClient();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|