Websocket.disconnect() method throws java.net.SocketException: Socket closed

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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!!!!!

回答1:

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.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!