Java socket timing out: Broken pipe -



Java socket timing out: Broken pipe -

i'm writing simple server in java, , i'm able retrieve incoming info client on server side, not on client side due 2000ms timeout. know why times out?

this server's code:

private static void listen() throws ioexception { while(true) { socket clientsocket = serversocket.accept(); stringbuilder bufferedstringinput = new stringbuilder(); charbuffer cbuf = charbuffer.allocate(4096); seek { inputstream = clientsocket.getinputstream(); bufferedreader br = new bufferedreader(new inputstreamreader(is, "utf8")); int nocharsleft = 0; while ((nocharsleft = br.read(cbuf)) != -1) { char[] arr = new char[nocharsleft]; cbuf.rewind(); cbuf.get(arr); bufferedstringinput.append(arr); cbuf.clear(); } system.out.println(bufferedstringinput.tostring()); } grab (ioexception e) { system.out.println("error received client data: " + e.getmessage()); } string message = "hello client"; seek { printwriter out = new printwriter(clientsocket.getoutputstream()); out.print(message); } grab (ioexception e) { system.out.println("error getting output stream client: " + e.getmessage()); } clientsocket.close(); } }

you're reading input until end of stream, happens when peer closes connection, you're trying write it, of course of study broken pipe. doesn't create sense. should read input until have 1 entire request, whatever means in protocol.

there other problems lurking here:

if client code uses readline(), you're not sending line terminator: utilize println(), not print(), , close printwriter, not client socket.

cbuf.rewind()/get()/clear() should cbuf.flip()/get()/compact().

but create more sense read straight char[] cbuf = new char[8192]; array, bufferedstringinput.append(cbuf, 0, nocharsleft), , forget charbuffer altogether. much info copying @ present.

nocharsleft poor name variable. read count.

java sockets

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -