1

I am using android-websockets library from codebutler in my project. But I ran into problem when I execute disconnect() method. Following is the code:

public void disconnectServer()
{
    if(client != null)
    {
        try {

            if(client.isConnected())
            {
                client.disconnect();

            }

        } catch (Exception e) {

            e.printStackTrace();
        }

    }
}

I get the following exception:

05-11 17:58:19.873: W/System.err(29443): java.net.SocketException: Socket closed

05-11 17:58:19.873: W/System.err(29443): at libcore.io.Posix.recvfromBytes(Native Method)

05-11 17:58:19.873: W/System.err(29443): at libcore.io.Posix.recvfrom(Posix.java:161)

05-11 17:58:19.873: W/System.err(29443): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:250)

05-11 17:58:19.878: W/System.err(29443): at libcore.io.IoBridge.recvfrom(IoBridge.java:553)

05-11 17:58:19.878: W/System.err(29443): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:485)

05-11 17:58:19.878: W/System.err(29443): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:37)

05-11 17:58:19.878: W/System.err(29443): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:237)

05-11 17:58:19.878: W/System.err(29443): at libcore.io.Streams.readSingleByte(Streams.java:41)

05-11 17:58:19.878: W/System.err(29443): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:233)

05-11 17:58:19.878: W/System.err(29443): at java.io.DataInputStream.readByte(DataInputStream.java:75)

05-11 17:58:19.878: W/System.err(29443): at com.codebutler.android_websockets.HybiParser.start(HybiParser.java:120)

05-11 17:58:19.878: W/System.err(29443): at com.codebutler.android_websockets.WebSocketClient$1.run(WebSocketClient.java:145)

05-11 17:58:19.878: W/System.err(29443): at java.lang.Thread.run(Thread.java:818)

Please help!!!!!

Vivek Molkar
  • 3,910
  • 1
  • 34
  • 46
Amey Bhandarkar
  • 511
  • 2
  • 5
  • 14

1 Answers1

2

It is because the implementation of disconnect() method does not synchronize with the thread which is created in the implementation of connect() method. codebutler/android-websockets is not of commercial quality. The disconnect() method does not perform even the closing handshake which is required by RFC 6455.

Use another WebSocket library if you don't want to see the error.

Community
  • 1
  • 1
Takahiko Kawasaki
  • 18,118
  • 9
  • 62
  • 105
  • Thanks for such a quick reply. Can you please suggest any library for commercial use? I have tried using two libraries viz., java_websocket & codebutler/android-websockets but it is giving me problems. Apart from the problem that I have mentioned above, I am facing a serious problem of socket disconnection. I don't know why it gets disconnected if I keep it idle for sometime. I get NotYetConnectedException when I try to call any APU in my project. Can you please suggest me any library which can be used for commercial use? – Amey Bhandarkar May 13 '15 at 05:29
  • Try **[nv-websocket-client](https://github.com/TakahikoKawasaki/nv-websocket-client)** ([Blog](http://darutk-oboegaki.blogspot.jp/2015/05/websocket-client-library-java-se-15.html)). To keep a connection, you need to send [Ping](https://tools.ietf.org/html/rfc6455#section-5.5.2) (or unsolicited [Pong](https://tools.ietf.org/html/rfc6455#section-5.5.3)) periodically. `nv-websocket-client` provides `WebSocket.setPingInterval` method to do it very easily. See [JavaDoc](http://takahikokawasaki.github.io/nv-websocket-client/) for details. – Takahiko Kawasaki May 13 '15 at 05:48