Saturday, 15 March 2014

Java Socket doesn't wait to receive bytes -



Java Socket doesn't wait to receive bytes -

i have developed socket in java this:

serversocket = new serversocket(port); system.out.println("listening in port " + port + " ..."); while (true) { socket = serversocket.accept(); system.out.println("connection has been created."); handle(socket); }

and handle method is:

private static void handle(final socket socket) throws ioexception { new thread(new runnable() { @override public void run() { seek { inputstream = socket.getinputstream(); myclass lift = new myclass(socket, is); elevator.start(); } grab (ioexception io) { io.printstacktrace(); } { seek { socket.close(); } grab (ioexception e) { e.printstacktrace(); } } } }).start(); }

and myclass this:

class myclass { private socket socket; private inputstream is; private printwriter out; private outputstream ds; public myclass(socket socket, inputstream is) { this.socket = socket; this.is = is; initializeoutputstream(socket); } private void initializeoutputstream(socket socket) { seek { ds = socket.getoutputstream(); } grab (ioexception e) { e.printstacktrace(); } } public void start() { while (true) { seek { int datalength = 10; byte[] databuffer = new byte[datalength]; is.read(databuffer, 0, datalength); // read , print cmd. system.out.println("data:" + datalayer.bytetostring(databuffer)); } grab (ioexception e) { e.printstacktrace(); seek { ds.close(); system.out.println("ds closed."); is.close(); system.out.println("is closed."); socket.close(); system.out.println("socket closed."); break; } grab (ioexception e1) { e1.printstacktrace(); } e.printstacktrace(); } } } }

when client sends info works well, when client isn't sending info prints:

data:0 data:0 data:0 ...

and dont stops.

can tell me how solve problem ?

the situation described happens when connection closed client-side (i.e. socket closed/stream @ end), , not when client not sending (if client idle still connected, server not print anything).

and because method read of inputstream class not throw exception when stream/connection closed, homecoming value -1, implementation, while loop continues run infinitely.

therefore quick prepare of issue can replace line read stream these two:

int endofstream=is.read(databuffer, 0, datalength); if(endofstream==-1) break;

basically in way checking if stream closed: if case, break while loop.

another solution declare , initialize variable int endofstream=0; before while loop, , alter while loop status in way:

int endofstream = 0; //init 0 come in first time in loop while (endofstream != -1) { //new loop status seek { int datalength = 10; byte[] databuffer = new byte[datalength]; endofstream = is.read(databuffer, 0, datalength); //store homecoming of read if (endofstream != -1) { //check if not @ end of stream // read , print cmd. system.out.println("data:" + databuffer[0]); } //... rest of code ...

another issue in code guess bug, i'm not sure because otherwise shouldn't able run code (but can not say, since copied code not complete). bug when phone call start method myclass.start();: since method not static, must phone call on object of class myclass instantiated line before, i.e. phone call method in way: elevator.start();

i hope may help you.

java sockets

No comments:

Post a Comment