简体   繁体   中英

I can not connect to server with Socket.IO-client Java

I want use library

And I wan realize this functionality:

  1. connect to my java server

     Socket socket = IO.socket("http://127.0.0.1:4444"); socket.io().open(new Manager.OpenCallback() { @Override public void call(Exception err) { if (err != null) { Log.d("mylog", err.getMessage()); return; } Log.d("mylog", "connected"); } }); 
  2. Send messge - I do not understand how.

    It is my server:

     public class Server { public static void main(String[] args) throws IOException { System.out.println("Welcome to Server side"); BufferedReader in = null; PrintWriter out= null; ServerSocket servers = null; Socket fromclient = null; // create server socket try { servers = new ServerSocket(4444); } catch (IOException e) { System.out.println("Couldn't listen to port 4444"); System.exit(-1); } try { System.out.print("Waiting for a client..."); fromclient= servers.accept(); System.out.println("Client connected"); } catch (IOException e) { System.out.println("Can't accept"); System.exit(-1); } in = new BufferedReader(new InputStreamReader(fromclient.getInputStream())); out = new PrintWriter(fromclient.getOutputStream(),true); String input,output; System.out.println("Wait for messages"); while ((input = in.readLine()) != null) { if (input.equalsIgnoreCase("exit")) break; out.println("S ::: "+input); System.out.println(input); } out.close(); in.close(); fromclient.close(); servers.close(); } } 

If I use Java client I can work with server, but I want to connect to android client. And I found only this library, and I not understand how it work. Examples in site not helped for me.

Make sure you have added its permission in your AndroidManifest.xml

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

Android's kernel, unlike mainline Linux kernel, restricts requests for creating sockets unless that application already has INTERNET permission.

Find more technical information about Android's Paranoid networking option in this link .

UPDATE #1

If you want to connect your Android client to the server on your PC localhost you should not use 127.0.0.1 as server IP address. This is Android its own localhost. You should instead use 10.0.2.2 as server IP address. More Info

@Pavel i've implemented SocketIO client for one of my app

here is the code... i think ..this may help u

private Socket mSocket;
/**
 * chat socket connection methods
 */
public void socketConnection()
{
    try
    {
        mSocket = IO.socket(Constant.CHAT_SERVER_URL);
        mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
        mSocket.on("message", onSocketConnectionListener);
        mSocket.connect();
    }
    catch (URISyntaxException e)
    {
        e.printStackTrace();
    }

    new Thread(new Runnable() {
        @Override
        public void run() {
            while(mSocket.connected()==false)
            {
                //do nothing
            }
            sendConnectData();
        }
    }).start();
}

/**
 * Send Data to connect to chat server
 */
public void sendConnectData()
{
    JSONObject msgToSend=new JSONObject();
    try
    {
        msgToSend.put("Type", 1);
        msgToSend.put("userid", userid);

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    mSocket.emit("message", msgToSend);
}

/**
 * Listener for socket connection error.. listener registered at the time of socket connection
 */
private Emitter.Listener onConnectError = new Emitter.Listener() {
    @Override
    public void call(Object... args) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mSocket != null)
                    if (mSocket.connected() == false)
                        socketConnection();
            }
        });
    }
};

/**
 * Listener to handle messages received from chat server of any type... Listener registered at the time of socket connected
 */
private Emitter.Listener onSocketConnectionListener = new Emitter.Listener() {
    @Override
    public void call(final Object... args) {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // handle the response args
            }
        });
    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM