Re: [xSocket-develop] basic question about read data from readByteBufferByDelimiter
Status: Inactive
Brought to you by:
grro
|
From: Gregor R. <gre...@go...> - 2008-10-31 06:48:15
|
Sorry I added the wrong links in my last response. Meanwhile I wrote some examples. Please see http://xsocket.svn.sourceforge.net/viewvc/xsocket/xsocket/core/trunk/src/test/java/org/xsocket/connection/DataHandlerExampleTest.java?revision=3177&view=markup Gregor 2008/10/30 beijing welcome <wel...@gm...> > > 2008/10/30 Gregor Roth <gre...@go...> > >> take a look into xSocket's Junit test: >> - >> http://xlightweb.svn.sourceforge.net/viewvc/xlightweb/xlightweb/core/trunk/src/test/java/org/xlightweb/CommandStreamingTest.java?view=markup >> - >> http://xlightweb.svn.sourceforge.net/viewvc/xlightweb/xlightweb/core/trunk/src/test/java/org/xlightweb/ChannelStreamingTest.java?view=markup >> >> > >> >> May be it helps you. > > > I changed the code to transfer the file, have further problem when write > file, see inline. > > > >> >> >> Gregor >> >> >> 2008/10/30 beijing welcome <wel...@gm...> >> >> >>> >>> 2008/10/30 Gregor Roth <gre...@go...> >>> >>>> … 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. >>>> >>> thanks for your quick response. >>> oh, i just want to let server receive the stream and then send another >>> stream back to client, the solution above may hurt something, what's the >>> most effective method to do that? >>> onData() seems not the correct place to implement this logical, could you >>> please point me which interface should be the right one? >>> >>> >>>> >>>> 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; >>>>> } >>>>> } >>>>> >>>> > I changed the code followed: > > public boolean onData(INonBlockingConnection connection) throws > IOException, BufferUnderflowException { > if (connection.available() < 0) return false; > File file = File.createTempFile("test", null); > System.out.println("avaliable1: " + connection.available()); > // "hello" can be received successfully. > String s = connection.readStringByDelimiter(DELIMITER); > System.out.println(s); > file.createNewFile(); > //file.deleteOnExit(); > String fullname = file.getAbsolutePath(); > System.out.println("fullname: " + fullname); > System.out.println("avaliable2: " + connection.available()); > FileChannel fc = new FileOutputStream(file).getChannel(); > long length = fc.transferTo(0, connection.available(), connection); > * // !!! This line cannot be run???* > System.out.println(length); > fc.close(); > connection.write(fc.size()); > connection.flush(); > > System.out.println("===="); > return true; > } > > > the server side output is: > /127.0.0.1 connected! > avaliable1: 179 > hello > fullname: C:\DOCUME~1\bati\LOCALS~1\Temp\test25638.tmp > avaliable2: 172 > avaliable1: -1 > > however, C:\DOCUME~1\bati\LOCALS~1\Temp\test25638.tmp is empty! I may miss > something. > > >>>>> >>>>> >>>>> ========================================= >>>>> ================= MServerClient.java ======================== >>>>> package mserver; >>>>> >>>>> import java.io.InputStreamReader; >>>>> import java.io.LineNumberReader; >>>>> import java.net.Socket; >>>>> >>>>> public class MServerClient { >>>>> >>>> static String filename = "c:\\111.txt"; > public void startClient() throws Exception { > int port = 8888; > String host = "localhost"; > INonBlockingConnection connection = null; > try { > connection = new NonBlockingConnection(host, port); > connection.write("hello\r\n"); > FileChannel fc = new FileInputStream(filename).getChannel(); > connection.transferFrom(fc); > fc.close(); > System.out.println("done!"); > } catch (Exception e) { > e.printStackTrace(); > } > finally { > if (connection != null) { > connection.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 >>>>> >>>>> >>>> >>> >> > |