0

I try to connect with my server by using socket on android. I want it to work like this; when i send a request to the server, if there is a response, call my response function. If the timeout of 500 ms happens (will check the exception and the boolean i set), i want to execute another function. But with this code im using, when i send something to the server and wait for response, it executes the onServerResponse function if there is a response and doesnt do anything(hangs) when there is not a response. How can i edit this code so it will throw a timeout exception when there is no response?

            boolean control = false;
            try{
                Socket socket = new Socket();
                socket.setSoTimeout(500);
                socket.connect(new InetSocketAddress(InetAddress.getByName(SERVER_IP),SERVER_PORT));
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF-8"));
                bw.write("asdasd\n");
                bw.flush();
                control = true;
                int readInt = -1;
                String read = null;
                StringBuilder sb = new StringBuilder("");
                while((readInt = socket.getInputStream().read()) != -1){
                    sb.append((char)readInt);
                }
                read = sb.toString();
                if(read != null && read.trim().length() > 0){ onServerResponse(read); }
            }catch(Exception e){ Log.v("Main", "GOT AN ERROR: "+e+control); }

Problem solved, see the comments for answer.

4
  • There is nothing wrong with the code you have posted and if the call to read() blocks for more than 500ms it will throw an exception. It's likely the connection attempt is what is blocking. Commented Aug 3, 2013 at 17:30
  • Hey Brian, when i set my server to send a response, i recieve the response via onServerResponse otherwise it just hangs at while((readInt = socket.getInputStream().read()) != -1){ and doesnt throws any exception. Commented Aug 3, 2013 at 17:37
  • Well i found what was causing the problems after i set breakpoints. Apparently system doesnt hangs, it just goes without throwing any exception or something. Think i need to modify my code to work with it. Thanks anyways. Commented Aug 3, 2013 at 17:44
  • 500ms is a ridiculously short read timeout in the first place. It should be several seconds at least. Commented Aug 4, 2013 at 1:04

2 Answers 2

2

I'm not sure if this answers your question, but setSoTimeout only applies to reads on the socket after it is connected, not to establishing the connection itself. For that, use the socket.connect(SocketAddress address, int timout) overload.) overload. You probably want to use both.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the heads up, that was one of the flaws of my code but didnt solve my problem. I even moved setSoTimeout before the reading line incase of the connect method overrides the read timeout or something. But it didnt solve my problem. I still dont get anything if server doesnt sends any response, system stays hanged.
0

Something like this would work:

Socket socket = new Socket();
InetAddress addr = InetAddress.getByName(SERVER_IP);
SocketAddress sockaddr = new InetSocketAddress(addr, SERVER_PORT);
// 500ms is too short, up to you.
// normal time would be 5 to 20 seconds, depends on network (intranet/internet)
socket.connect(sockaddr, 500);
// 500ms to timeout reading from the socket
socket.setSoTimeout(500);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.