-1

i recently develop an app that can use java socket to connect to java server socket in PC. This code work perfectly when connect to the server it send String immediately after the thread had been called but i dont know why when i close the server and try it then it did not throw exception immediately but after 1 minute the page idle then only the exception page called.

Here the logcat file when server is open and also when it close: https://i.stack.imgur.com/3jeF0.jpg

    public class checksession implements Runnable
    {
    private Activity activity;
    Socket soc;

    public checksession(Activity activity)
    {
        //get activity from class called
        this.activity=activity;
        //this.soc=soc;

    }


    @Override
    public void run()
    {
        try{

            soc=new Socket("192.168.0.113",11000);


            DataOutputStream dout=new DataOutputStream(soc.getOutputStream());


            //request format --> requestkey-field required
            //format for LG request--> LG-username-password
            String sendrequest="SS";

            //send requestcode to server
            dout.writeUTF(sendrequest);
            dout.flush();//refresh to make sure it send to the server

            //wait for input
            DataInputStream dis=new DataInputStream(soc.getInputStream());
            final String  codefromserver=(String)dis.readUTF();
            System.out.println("reply:"+codefromserver);


            String replycode[]= codefromserver.split("-");

            //server reply code format
            // if not used on database RE-CK-NO
            //if used on database RE-CK-YES

            String sessionavailableornot=replycode[2];

            if(sessionavailableornot.equals("YES"))
            {
                activity.runOnUiThread(new Runnable() {
                    public void run() {
                        //Do your UI operations like dialog opening or Toast here
                        //navigate user to main screen
                        Intent in = new Intent(activity, sessiondetected.class);
                        in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        activity.startActivity(in);
                    }
                });

            }

            soc.close();

        }catch(Exception e) {
            //runOnUiThread function is to let the function to be run on main thread
            //bacause UI function cannot be run on worker thread
            activity.runOnUiThread(new Runnable() {
                public void run() {
                    //Do your UI operations like dialog opening or Toast here
                    //navigate user back to connectionerror page so that user know
                    Intent inerr = new Intent(activity, serverconnectionerror.class);
                    inerr.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    activity.startActivity(inerr);
                }
            });
        }

    }
}
Balasubramanian
  • 700
  • 7
  • 26
  • 1
    What if you try setting the `SO_TIMEOUT` to e.g. two seconds with `soc.setSoTimeout(2000);` right after `soc=new Socket("192.168.0.113",11000);`? – Mick Mnemonic Sep 13 '17 at 09:22
  • You haven't set any timeouts here. You are therefore getting the platform's default timeouts, which are about a minute for connect timeouts, and infinity for read timeouts. Unclear what you're asking. – user207421 Sep 13 '17 at 09:43
  • @MickMnemonic It still the same even i set timeout T_T – blackstoom Sep 13 '17 at 16:17
  • @EJP Basicly, this is a thread that will be call at my first app page where this thread will send a String with value "SS" to the server on my computer if the computer check the ip has logged in recently It will send RE-SS-YES back to the client and user will be redirect to logged in page. There is no problem where no delay of 1 minute when the server is online. However, when i close the server and try there 1 minute delay before it throw exception. My thought is when this code "soc=new Socket("192.168.0.113",11000);" is executed if it cant bind to a serversocket it will throw an exception – blackstoom Sep 13 '17 at 16:20
  • isn't that the when new Socket(ipaddr,port) is executed if it cant bind to a serversocket it will throw an exception immediately? If it is it should work perfectly since i catch exception down there to redirect user to serverconnectionerror.xml that will tell user they cant connect to the server – blackstoom Sep 13 '17 at 16:29
  • How about setting the (other) connection timeout value through `soc.connect(...)` as follows: `soc = new Socket(); soc.setSoTimeout(2000); soc.connect(new InetSocketAddress("192.168.0.113", 11000), 2000);` – Mick Mnemonic Sep 13 '17 at 19:52
  • There is no such thing as 'bind to a serversocket'. You bind sockets to ports. You appear to be getting a connect timeout, which behaves as I described above. It remains unclear what you're asking. – user207421 Sep 14 '17 at 02:49
  • @MickMnemonic Thks Mick! you solution worked :D – blackstoom Sep 14 '17 at 08:12
  • If you found the solution helpful, you could approve my answer. – Mick Mnemonic Sep 21 '17 at 07:01

1 Answers1

0

You need to specify timeout values for the socket; otherwise platform defaults will be used.

The following code sets the SO_TIMEOUT (socket read timeout) and the connection timeout values before opening the socket.

final int timeout = 2000; // in millis
soc = new Socket();
soc.setSoTimeout(timeout);
soc.connect(new InetSocketAddress("192.168.0.113", 11000), timeout);
Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30