Re: [xSocket-develop] basic question about read data from readByteBufferByDelimiter
Status: Inactive
Brought to you by:
grro
|
From: Gregor R. <gre...@go...> - 2008-10-29 17:48:54
|
… you do two things in our onData() implementation: Echoing the received
data and printing it.
If you are using the async flush mode, the buffer will be written to the
network asynchronously by the internal I/O thread. This means by returning
from the write method the buffer will be accessed by the I/O thread in the
background in a concurrent way. Because you access the buffer after the
write call to print it race conditions occurs. The solution is to duplicate
the byte buffer by calling <ByteBuffer>.duplicate() method or not to set the
flush mode to async.
Converting byte buffers to a string is a little bit tricky. You can use the
convenience method DataConverter.toString(<ByteBuffer[]>, encoding) to
convert the byte buffers to a string.
Gregor
2008/10/29 beijing welcome <wel...@gm...>
> 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();
> }
> }
>
> }
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's
> challenge
> Build the coolest Linux based applications with Moblin SDK & win great
> prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> xSocket-develop mailing list
> xSo...@li...
> https://lists.sourceforge.net/lists/listinfo/xsocket-develop
>
>
|