sockets - Java TCP Client-send blocked? -
i writing java tcp client talks c server. have alternate sends , receives between two. here code.
- the server sends length of binary msg(len) client(java)
- client sends "ok" string
- server sends binary , client allocates byte array of 'len' bytes recieve it.
- it again sends "ok".
step 1. works. "len" value. client gets "send blocked" , server waits receive data.
can take look.
in try block have defined:
socket echosocket = new socket("192.168.178.20",2400); outputstream os = echosocket.getoutputstream(); inputstream ins = echosocket.getinputstream(); bufferedreader br = new bufferedreader(new inputstreamreader(ins)); string frompu = null; if( (frompu = br.readline()) != null){ system.out.println("pu returns as="+frompu); len = integer.parseint(frompu.trim()); system.out.println("value of len pu="+len); byte[] str = "ok\n".getbytes(); os.write(str, 0, str.length); os.flush(); byte[] buffer = new byte[len]; int bytes; stringbuilder curmsg = new stringbuilder(); bytes =ins.read(buffer); system.out.println("bytes="+bytes); curmsg.append(new string(buffer, 0, bytes)); system.out.println("ciphertext="+curmsg); os.write(str, 0, str.length); os.flush(); }
updated:
here code. @ moment, there no recv or send blocking on either sides. however, both buffered reader , datainput stream reader, unable send ok msg. @ server end, large number of bytes instead of 2 bytes ok.
socket echosocket = new socket("192.168.178.20",2400); outputstream os = echosocket.getoutputstream(); inputstream ins = echosocket.getinputstream(); bufferedreader br = new bufferedreader(new inputstreamreader(ins)); datainputstream dis = new datainputstream(ins); dataoutputstream dos = new dataoutputstream(os); if( (frompu = dis.readline()) != null){ //if( (frompu = br.readline()) != null){ system.out.println("pu server returns length as="+frompu); len = integer.parseint(frompu.trim()); byte[] str = "ok".getbytes(); system.out.println("str.length="+str.length); dos.writeint(str.length); if (str.length > 0) { dos.write(str, 0, str.length); system.out.println("sent ok"); } byte[] buffer = new byte[len]; int bytes; stringbuilder curmsg = new stringbuilder(); bytes =ins.read(buffer); system.out.println("bytes="+bytes); curmsg.append(new string(buffer, 0, bytes)); system.out.println("binarytext="+curmsg); dos.writeint(str.length); if (str.length > 0) { dos.write(str, 0, str.length); system.out.println("sent ok"); }
using bufferedreader
around stream , trying read binary data stream bad idea. wouldn't surprised if server has actually sent data in 1 go, , bufferedreader
has read binary data line it's returned.
are in control of protocol? if so, suggest change send length of data binary (e.g. fixed 4 bytes) don't need work out how switch between text , binary (which pain).
if can't that, you'll need read byte @ time start until see byte representing \n
, convert you've read text, parse it, , read rest chunk. that's inefficient (reading byte @ time instead of reading buffer @ time) i'd imagine amount of data being read @ point pretty small.
Comments
Post a Comment