仅在1分钟后抛出异常抛出异常

时间:2017-09-13 09:14:49

标签: java android multithreading sockets socketexception

我最近开发了一个可以使用java socket连接到PC中的java服务器套接字的应用程序。这个代码完美地连接到服务器它发送线程后立即调用线程,但我不知道为什么当我关闭服务器并尝试它然后它没有立即抛出异常,但1分钟后页面空闲然后只有异常页面叫。

这里是服务器打开时以及关闭时的logcat文件: https://imgur.com/a/lOqdq

    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);
                }
            });
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您需要为套接字指定超时值;否则将使用平台默认值。

以下代码在打开套接字之前设置SO_TIMEOUT(套接字读取超时)和连接超时值。

final int timeout = 2000; // in millis
soc = new Socket();
soc.setSoTimeout(timeout);
soc.connect(new InetSocketAddress("192.168.0.113", 11000), timeout);