0

I have created a websocket server on android using Tootallnate java-websocket. I have hosted it in 10.0.2.2 and port number 15. How can I connect my client who is also running in the same emulator? When I use the below code, it connects even when the server is not running! why is that? what am I doing wrong? I have tested my server. Printed the instance and address and all. Everything works fine in server side. Please advice.

package com.example.android_websocketclient_test;

import java.net.URI;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.handshake.ServerHandshake;

import android.util.Log;

public class EmptyClient extends WebSocketClient{
public EmptyClient(URI serverUri, Draft draft) {
    super(serverUri, draft);
}

public EmptyClient(URI serverURI) {
    super(serverURI);
}

@Override
public void onOpen(ServerHandshake handshakedata) {
    Log.d("test app","new connection opened");
    System.out.println("new connection opened");
}

@Override
public void onClose(int code, String reason, boolean remote) {
    System.out.println("closed with exit code " + code + " additional info: " + reason);
}

@Override
public void onMessage(String message) {
    System.out.println("received message: " + message);
}

@Override
public void onError(Exception ex) {
    System.err.println("an error occurred:" + ex);
}

}

Following is how I create my client.

public static void main(String[] args) throws URISyntaxException {      
    WebSocketClient client = new EmptyClient(new URI("ws://10.0.2.2:15"), new Draft_10());
    client.connect();
}
AnOldSoul
  • 4,017
  • 12
  • 57
  • 118
  • We cannot see what url or ip you use and how. – greenapps Sep 11 '15 at 11:57
  • @greenapps I have edited my code. Please help. – AnOldSoul Sep 13 '15 at 12:05
  • Nou what is the problem? That it connects? Or that it also connects when the server is not running? The latter is impossible of course. Also you you should always use 127.0.0.1 as ipaddress when server and clienr run on the same device or emulator. – greenapps Sep 13 '15 at 15:44

0 Answers0