weird characters while reading text and binary from a socket's stream in java -


i have tried search answer no luck (both @ google , stackoverflow)

i writing java program in server , client can communicate sending/receiving data , files...

i sending files chunks of 1mb each. let client know number of chunks, sending string line containing blocknb=x x number of chunks, followed file.

however when reading client, receiving instead of line weird characters: ur\u0000\u0002[b¬ó\u0017ø\btà\u0002\u0000\u0000xp\u0000\u0000\bp \b , \uxxxx representant of values (i expecting here blocknb=1)

(written in clearer way: ur [b¬ó ø tà xp p (where spaces escaped characters)

here code.
server side

try (         serversocket welcome = new serversocket(6500);         socket socket = welcome.accept();         objectoutputstream outputstream = new objectoutputstream(socket.getoutputstream());         printwriter printwriter = new printwriter(socket.getoutputstream())     )     {         system.out.println("accepted");         file f = new file("...");  //the file path         try (             fileinputstream fileinputstream = new fileinputstream(f)         )         {             long length = f.length();             byte[] buffer;             //here put code executed             buffer = new byte[(int) length];             printwriter.println("blocknb=1");              fileinputstream.read(buffer);             outputstream.writeobject(buffer);              printwriter.println("}");         }     } 

client side

try (         socket socket = new socket("localhost", 6500);         objectinputstream inputstream = new objectinputstream(socket.getinputstream());         fileoutputstream fileoutputstream = new fileoutputstream("c:/d/test.txt");         scanner scanner = new scanner(socket.getinputstream())     )     {         string msg = scanner.nextline();  //the weird string read @ point         long blocknb = long.parselong(msg.split("blocknb=", 2)[1]);         byte[] file = (byte[]) inputstream.readobject();         fileoutputstream.write(file);     } 

p.s.: when removed last 3 lines (only) server side went normal. i've received blocknb=1 expected. problem appeared when there mix of 2 types of data/two types of outputstreams

english third language forgive me in case of bad grammar or word misuse

edit: main problem here forgot flush streams. when flushed streams started receive eofexception (that avoid in debugging if applied ordering read/write - there kind of synchronization problem) used quicksilver's solution worked in addition flush.

i avoid using 2 different objects write , read same stream. try using objectoutputstream on server side , objectinputstream on client side. handle strings same way handle buffers:

server: outputstream.writeobject("blocknb=1");

client: string blocknbstr = inputstream.readobject();


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -