Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

Java
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.

Java
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();
    }
Posted
Updated 13-Sep-15 2:43am
v2
Comments
Richard MacCutchan 13-Sep-15 8:26am    
Why are you creating a server object in your client code?
mayooran99 13-Sep-15 10:01am    
I have edited the question. It was my mistake. Could you please help with the answer?
Richard MacCutchan 13-Sep-15 12:08pm    
Where does this WebSocketClient come from? It is not part of the standard Android libraries as far as I can see.
mayooran99 13-Sep-15 18:43pm    
It is being used in the MainActivity class. When I launch the program the client gets initiated. I am using TootallNate Java WebSocket Library.
Richard MacCutchan 14-Sep-15 3:49am    
Then you need to check the documentation for that library or ask whoever wrote it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900