TooTallNate / Java-WebSocket

A barebones WebSocket client and server implementation written in 100% Java.
http://tootallnate.github.io/Java-WebSocket
MIT License
10.53k stars 2.58k forks source link

WebSocket Server not starting on Android #181

Closed jbelstner closed 11 years ago

jbelstner commented 11 years ago

I am trying to migrate a LocalSocket implementation to use this WebSocket implementation in Android. Just like my LocalSocket implementation, both the Server and the Client app exist on the same device. The Client app is running in the foreground while the Server is instantiated inside a Service running in the background. Inside the Service, I start a new thread and call the WebSocketServer.start() method from inside Thread.run(). When I try to WebSocketClient.send() something from the client, I always get the NotYetConnectedException.

An interesting observation is that if I try to use WebSocketServer.run() instead of WebSocketServer.start(), the ServerSocketChannel.open() method always throws an IOException and exits.

I've run the sample Java code here on my Mac and it works great. Has anyone gotten the server code to run in Android that might be able to point me in the right direction?

Thanks so much!

Davidiusdadi commented 11 years ago

You have to make sure that the client is connected! You can do that by calling send in onOpen or using the connectBlocking() method instad of connect().

Its absolutely ok to call WebSocketServer.run() directly. There are however serveral reasons why a port can not be bound - most likely the port is already in use. You should check the message of the exception - if you like you can post it here.

The last thing i would like to know is the android version you are using?

jbelstner commented 11 years ago

Thanks for your reply. Here is what I found.

On the Server side, the exception thrown from ServerSocketChannel.open() is "java.net.SocketException: socket failed: EACCES (Permission denied)".

I did a quick search and I see others have encountered this as well. I added ...

<uses-permission
    android:name="android.permission.INTERNET" />

to my AndroidManafest.xml and the exception goes away.

Thanks for pointing me in the right direction!!! BTW, I'm running on an Asus Nexus 7, Android version 4.2.2, Kernel version 3.1.10-g05b777c, Build number JDQ39

vikramsingh88 commented 1 year ago

I have created a websocket server in android app public class WebsocketServer extends WebSocketServer { }

And starting it from Application class int port = 1000; InetSocketAddress inetSockAddress = new InetSocketAddress(port); WebsocketServer wsServer = new WebsocketServer(inetSockAddress); wsServer.run();

Also add the permission in Manifest file

But getting permission denied exception as soon as Application class starts the server D Error on websocket connection: Permission denied at org.java_websocket.server.WebSocketServer.doSetupSelectorAndServerThread(WebSocketServer.java:567) at org.java_websocket.server.WebSocketServer.run(WebSocketServer.java:367)

PhilipRoman commented 1 year ago

I see that you are using port 1000. Ports below 1024 are reserved for root users, try with port greater than 1024.

vikramsingh88 commented 1 year ago

Thanks Philip for quick reply, yes I tried using port 8000 and it worked.