1
votes

I write socket client-server connection. Server listens client name and if it's not avaible, server closes connection. With correct names all works. Client:

clientSocket = new Socket("192.168.1.102", 15780);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
sendRequest(uName);
currentInt = 0;
updateUI();

Then I check, whether it is possible to open connection.

private void updateUI()
    {  
        if(currentInt <= 100)
        {
            if(clientSocket.isConnected())  
            {
                outServ.setText("Complete!");
                Intent i = new Intent(this, RoomClass.class);
                startActivity(i);
                mRedrawHandler.removeMessages(0);
            }
        }else{
            currentInt++;  
            mRedrawHandler.sleep(50);  
        }
    } 

And it always is connected! But server-side closed cliet port. I heard that thus it is impossible to check up, whether connection is closed. How to make it?

1

1 Answers

2
votes

Socket.isConnected() will return 'false' only if the Server closes properly (calling the .close() method). If you don't have control over the server socket, then uses PrintWrite.checkError()

See this post for examples:https://stackoverflow.com/a/8268497/1012381