简体   繁体   English

我无法使用Socket.IO-client Java连接到服务器

[英]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 连接到我的Java服务器

     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. 如果我使用Java客户端,则可以使用服务器,但我想连接到android客户端。 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 确保已在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. 与主线Linux内核不同,Android的内核会限制创建套接字的请求,除非该应用程序已经具有INTERNET权限。

Find more technical information about Android's Paranoid networking option in this link . 在此链接中找到有关Android的偏执狂网络选项的更多技术信息。

UPDATE #1 更新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. 如果要将Android客户端连接到PC本地主机上的服务器,则不应使用127.0.0.1作为服务器IP地址。 This is Android its own localhost. 这是Android自己的本地主机。 You should instead use 10.0.2.2 as server IP address. 您应该改为使用10.0.2.2作为服务器IP地址。 More Info 更多信息

@Pavel i've implemented SocketIO client for one of my app @Pavel我已经为我的一个应用实现了SocketIO客户端

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
            }
        });
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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