java - Socket Timeout Issue: android client reading data from pc server -
i'm new android, java , socket programming of course i'm trying merge three!
i'm creating desktop based java server send short string android app(client).
i have android app running fine , sends strings server read no problems.
i have server running , recieves strings no problems.
the app(client) has socket timeout whenever try read strings returned server. i'm using same code can't understand problem.
anyway here's code:
//server// import java.io.*; import java.net.*; import java.util.*; public class gpsserver { serversocket serversocket = null; socket socket = null; public gpsserver() { try { serversocket = new serversocket(8189); } catch (unknownhostexception unhe) { system.out.println("unknownhostexception: " + unhe.getmessage()); } catch (interruptedioexception intioe) { system.out.println("timeout while attempting establish socket connection."); } catch (ioexception ioe) { system.out.println("ioexception: " + ioe.getmessage()); } } public void refreshserver() { try { socket = serversocket.accept(); inputstreamreader inputstreamreader = new inputstreamreader(socket.getinputstream()); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); printwriter printwriter = new printwriter(socket.getoutputstream(),true); system.out.println("socket read successful"); printwriter.println("send bye disconnect."); string lineread = ""; boolean done = false; while (((lineread = bufferedreader.readline()) != null) && (!done)){ system.out.println("received client: " + lineread); printwriter.println("you sent: " + lineread); if (lineread.comparetoignorecase("bye") == 0) done = true; } system.out.println("closing connection"); socket.close(); bufferedreader.close(); inputstreamreader.close(); printwriter.close(); } catch (unknownhostexception unhe) { system.out.println("unknownhostexception: " + unhe.getmessage()); } catch (interruptedioexception intioe) { system.out .println("timeout while attempting establish socket connection."); } catch (ioexception ioe) { system.out.println("ioexception: " + ioe.getmessage()); } } public void nullify() { try { socket.close(); serversocket.close(); } catch (ioexception ioe) { system.out.println("ioexception: " + ioe.getmessage()); } } }
and client...
//client import java.io.*; import java.net.*; import java.util.*; public class gpsclient { public string lastmessage; socket socket = null; string serverurl; int serverport; public gpsclient() { lastmessage = ""; serverport = 8189; serverurl = "192.168.10.4"; try { socket = new socket(serverurl, serverport); socket.setsotimeout(10000); } catch (unknownhostexception unhe) { system.out.println("unknownhostexception: " + unhe.getmessage()); } catch (interruptedioexception intioe) { system.out.println("timeout while attempting establish socket connection."); } catch (ioexception ioe) { system.out.println("ioexception: " + ioe.getmessage()); } } public void retrievenew() { try { socket = new socket(serverurl, serverport); socket.setsotimeout(10000); lastmessage = "connected!"; inputstreamreader inputstreamreader = new inputstreamreader(socket.getinputstream()); bufferedreader bufferedreader = new bufferedreader(inputstreamreader); printwriter printwriter = new printwriter(socket.getoutputstream(),true); printwriter.println("request"); lastmessage = "request sent"; // error when uncomment block, i.e. try read response server // "timeout while attempting establish socket connection." // string lineread = ""; // while ((lineread = bufferedreader.readline()) != null) { // system.out.println("received server: " + lineread); // lastmessage = "received server: " + lineread; // } lastmessage = "closing connection!"; bufferedreader.close(); inputstreamreader.close(); printwriter.close(); socket.close(); } catch (unknownhostexception unhe) { system.out.println("unknownhostexception: " + unhe.getmessage()); } catch (interruptedioexception intioe) { system.out.println("timeout while attempting establish socket connection."); } catch (ioexception ioe) { system.out.println("ioexception: " + ioe.getmessage()); } { try { socket.close(); } catch (ioexception ioe) { system.out.println("ioexception: " + ioe.getmessage()); } } } public void nullify() { try { printwriter printwriter = new printwriter(socket.getoutputstream(),true); printwriter.println("bye"); printwriter.close(); socket.close(); } catch (ioexception ioe) { system.out.println("ioexception: " + ioe.getmessage()); } } }
thanks in advance!
josh
apart issues noted in comment, creating 2 sockets in client, , server written process 1 connection. writes first 1 , tries read it, meanwhile client writing second 1 , trying read that.
when fix hit deadlock, both sides trying read each other.
also shouldn't use printwriter
or printstream
on network, swallow exceptions need know about. use bufferedwriter
.
Comments
Post a Comment