1

I am used the below code to connect to a php websocket.

serverAddr = InetAddress.getByName(ip_str);
                socket = new Socket(serverAddr, port_str);
                mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));

Trying to open a socket and want to listen to what data is coming from the server.

the ip_str is something like this : wss://xyz.com:8181/game where xyz is the actual host name, and 8181 is a dummy port number(have given dummy values as i cannot give the actual values here - but it is working fine on web as we are trying to connect same socket via web).

On this line :

socket = new Socket(serverAddr, port_str);

I am getting the following error message :

java.net.ConnectException: failed to connect to ip6-localhost/::1 (port 8181): connect failed: ECONNREFUSED (Connection refused)

Anyone has any idea why i am facing this issue?

Parth Anjaria
  • 3,961
  • 3
  • 30
  • 62

2 Answers2

1

If ip_str is a full URL like "wss://xyz.com:8181/game" then the following line is completely wrong:

serverAddr = InetAddress.getByName(ip_str);

You can't pass a URL to InetAddress.getByName(). It only accepts a dotted IP address or a hostname as input, eg:

serverAddr = InetAddress.getByName("xyz.com");

So, use the URI or URL class to parse the URL into its constituent components, and then you can resolve the hostname component to an IP address, eg:

URI WebSocketUri = new URI("wss://xyz.com:8181/game"); // or URL
serverAddr = InetAddress.getByName(WebSocketUri.getHost());
...

Or, you can let Socket resolve the host for you:

URI WebSocketUri = new URI("wss://xyz.com:8181/game"); // or URL
if (WebSocket.getScheme() == "wss")
    socket = new SSLSocket(WebSocket.getHost(), WebSocket.getPort());
else
    socket = new Socket(WebSocket.getHost(), WebSocket.getPort());
// send an HTTP request for WebSocket.getRawPath() and negotiate WebSocket handshake as needed...

Or better, use URLConnection instead:

WebSocketUri = new URL("wss://xyz.com:8181/game");
URLConnection conn = WebSocketUri.openConnection();
...

Or, use an actual 3rd party WebSocket library (of which there are many available for Android).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • this is now working fine, thank you. but then i am facing an issue that the server does not get my request. it shows an error on server – Parth Anjaria Sep 07 '17 at 06:40
  • @ParthAnjaria that is a different issue than the one you asked here. Please post a new question, and be sure to show your latest code. – Remy Lebeau Sep 07 '17 at 06:58
-1

It is because you are trying to connect via secured/encrypted connection, wss://. You need extra step before your app can connect to the target address. It is usually by setting the certificate first.

You may check this solution: Unable to connect websocket with wss in android. More information here: how to create Socket connection in Android?

user1506104
  • 6,554
  • 4
  • 71
  • 89
  • 1
    The connection (the TCP connection) would still succeed in that case, it would need additional steps to achieve a full-fledged websocket connection. In other words, the error message would not be ECONNREFUSED – President James K. Polk Sep 06 '17 at 15:11