java - Sending ByteArrays and Bytes from the same TCP connection -
in application i'm create socket communication between client , server. there 2 types of messages passing differ in-terms of length of data. 1 message type has 1 byte, while other 1 of variable length. trying use single tcp connection handle both situations, far failed. please tell me ideal approach this. using 2 connections different port numbers best approach? note impossible use socket.io in project due external constraints.
here's reading code i'm using:
socket socket = new socket( dstaddress, dstport ); inputstream inputstream = socket.getinputstream(); try( bytearrayoutputstream bytearrayoutputstream = new bytearrayoutputstream( 1024 ) ) { byte[] buffer = new byte[1024]; int bytesread; while( ( bytesread = inputstream.read( buffer ) ) != -1 ) { bytearrayoutputstream.write( buffer, 0, bytesread ); } } catch( exception e ) { e.printstacktrace(); }
thanks
you need design own communication protocol, http example, of course simpler. , implement decoder/encoder on both sides
Comments
Post a Comment