java - UDP received datapackets but prints out funny results -
i doing simple udp file.txt transfer sender receiver. arrived prompt.. but, when print content came out correctly, mashed previous words received on receiver end.
what mean: text file has content in 1 sentence as:
day after day, week after week passed away on return geneva;
eg on receiver side:
day after day,r weekr after weekr passed awayed onayed myayed return toturn geneva
code is:
byte[] bytearray = new byte[9999]; datagramsocket listensocket = new datagramsocket(5001, inetaddress.getbyname("127.0.0.1")); while(true) { datagrampacket segment = new datagrampacket(bytearray, bytearray.length); listensocket.receive(segment); bytearrayinputstream bytein = new bytearrayinputstream(segment.getdata()); int data = bytein.read(); if(data == -1) { //break; } else { system.out.println("got: " + new string(segment.getdata())); }
yes, tcp have been more better option need on why happens in case. hope can give me insight problem.
you forgot blank buffer after first using - if consecutive receive gets less data, previous, re-read of old data.
Comments
Post a Comment