AndroidAsync websites not working

I use this koush AndroidSync library to create a websocket (server / client) and transfer data between two Android devices. Both devices connect via Wi-Fi (Wifi AP connects to them, and another device is connected to it). I get a TimeoutException on the client device after 4-5 seconds of sending the request. This is what I have done so far.

ServerActivity.java

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_server); mSockets = new ArrayList<WebSocket>(); mAsyncHttpServer = new AsyncHttpServer(); mWebSocketCallback = new AsyncHttpServer.WebSocketRequestCallback() { @Override public void onConnected(final WebSocket webSocket, RequestHeaders headers) { mSockets.add(webSocket); webSocket.send("Welcome Client"); webSocket.setClosedCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { try { if (ex != null) Log.e("WebSocket", "Error"); } finally { mSockets.remove(webSocket); } } }); webSocket.setStringCallback(new WebSocket.StringCallback() { @Override public void onStringAvailable(String s) { Log.d("SERVERTAG",s); Toast.makeText(getApplicationContext(),s,Toast.LENGTH_SHORT).show(); } }); } }; mAsyncHttpServer.websocket("/",mWebSocketCallback); mAsyncHttpServer.listen(Utils.PORT_NUMBER); Button sendButton = (Button) findViewById(R.id.sendButtonS); sendButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { for(WebSocket socket : mSockets) { socket.send("Server sent a string"); } } }); } 

ClientActivity.java

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_client); mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); //Resolve IP address int ipAddress = mWifiManager.getConnectionInfo().getIpAddress(); String hostAddress = Formatter.formatIpAddress(ipAddress); hostAddress = "http://" + hostAddress + ":" +Utils.PORT_NUMBER; Log.d("CLIENTTAG", "address is " + hostAddress); mWebSocketConnectCallback = new AsyncHttpClient.WebSocketConnectCallback() { @Override public void onCompleted(Exception ex, WebSocket webSocket) { if (ex != null) { ex.printStackTrace(); return; } webSocket.send("Hello Server"); webSocket.setStringCallback(new WebSocket.StringCallback() { @Override public void onStringAvailable(String s) { Log.d("CLIENTTAG",s); Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show(); } }); } }; mAsyncHttpClient = AsyncHttpClient.getDefaultInstance(); mAsyncHttpClient.websocket(hostAddress, null, mWebSocketConnectCallback); } 

This is what I get in logcat on the client device.

 10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ java.util.concurrent.TimeoutException 10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.http.AsyncHttpClient$2.run(AsyncHttpClient.java:240) 10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.lockAndRunQueue(AsyncServer.java:683) 10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:700) 10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:608) 10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer.access$700(AsyncServer.java:37) 10-21 19:50:49.289 742-945/com.haloappstudio.musichub W/System.err﹕ at com.koushikdutta.async.AsyncServer$13.run(AsyncServer.java:557) 

I have not done socket programming before. Can anyone help me here?

Any help is appreciated.

+6
source share
2 answers

I found the problem, as @jrandaz said the problem was with the server IP address.

Turns off

WifiManager.getConnectionInfo().getIpAddress()

returns the device’s own IP address, not the device address of the Wi-Fi hotspot to which it is connected. I used 192.168.43.1 , which is the default wifi hotspot IP address in android, and it worked.

+1
source

On the client side, have you tried passing http, where do you have zero?

mAsyncHttpClient.websocket(hostAddress, null, mWebSocketConnectCallback);

Maybe try

mAsyncHttpClient.websocket(hostAddress, http, mWebSocketConnectCallback);

In addition, when you refer to both devices connected to Wi-Fi, what you mean by the second device is "Connected to it." (This will be clarified through a comment, but does not have enough points). You may not receive the correct IP address when connecting to the demo server.

0
source

Source: https://habr.com/ru/post/977032/


All Articles