简体   繁体   中英

connecting to server but server has not started java

I am creating an android app which connects to a server(java) through sockets.I have created a server application in swing which contains start button which open a port which i have specified.

So my question is how can i handle the situatuion when client try to connect but server has not started means i havent click on start button? And i also want create dialog box for client which suggest to start the server

I have tried this code but its get hang my application:

I have got really confused with this!

Here is code:

 try {
        cs = new Socket(IPADD,PORT);
         if(cs.isConnected())
         {   Toast.makeText(ipInfo.this,"Connected",Toast.LENGTH_SHORT).show();
             Intent inst = new Intent(ipInfo.this,homeActivity.class);
             inst.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
             startActivity(inst);
             finish();
        }else {
           Toast.makeText(ipInfo.this,"Server is disconnected\nStart server in desktop",Toast.LENGTH_SHORT).show();
             }
    }catch (IOException e)
     {Toast.makeText(ipInfo.this,e.getMessage(),Toast.LENGTH_SHORT).show();
    }catch (Exception e)
     {Toast.makeText(ipInfo.this,e.getMessage(),Toast.LENGTH_SHORT).show();
}

I think you have to wait for it to connected - is there no callback? The code says:

 /**
 * Returns the connection state of the socket.
 * <p>
 * Note: Closing a socket doesn't clear its connection state, which means
 * this method will return <code>true</code> for a closed socket
 * (see {@link #isClosed()}) if it was successfuly connected prior
 * to being closed.
 *
 * @return true if the socket was successfuly connected to a server
 * @since 1.4
 */
public boolean isConnected() {
    // Before 1.3 Sockets were always connected during creation
    return connected || oldImpl;
}

so isConnected() just returns a static value, which is unlikely to have actually changed so immediately after you instantiated the socket. There are lots of networking libraries for Android. Unless you specifically need to do this, I recommend using Retorfit or Volley (esp. retrofit), or perhaps just make an OKHTTP client and use this?

Your code isn't hanging permanently, it's just waiting for the timeout, since the server isn't responding, at which point you will get a UnknownHostException exception. Catch this and show your "Start Server" message inside the catch.

try {
    Socket socket = new Socket();
    // 1000 is the timeout
    socket.connect(new InetSocketAddress(IPADD, PORT), 1000);
    Toast.makeText(ipInfo.this,"Connected",Toast.LENGTH_SHORT).show();
    Intent inst = new Intent(ipInfo.this,homeActivity.class);
    inst.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(inst);
    finish();
} catch (IOException e)
    Toast.makeText(ipInfo.this,"Server is disconnected\nStart server in desktop",Toast.LENGTH_SHORT).show();
} catch (Exception e) {
    Toast.makeText(ipInfo.this,e.getMessage(),Toast.LENGTH_SHORT).show();
}

Your socket code can never work. It has to be executed in a thread or AsyncTask. And if you do you cannot display Toast()s then.

So what are you doing?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM