0

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.

Andrea
  • 11,801
  • 17
  • 65
  • 72
Pavel
  • 251
  • 1
  • 4
  • 18

2 Answers2

1

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

frogatto
  • 28,539
  • 11
  • 83
  • 129
  • of course I have NOT forgotten to add"android.permission.INTERNET" – Pavel Sep 24 '15 at 09:36
  • @Pavel So, do you want to connect from the server to your Android client? – frogatto Sep 24 '15 at 09:37
  • I want connect my android client to my java server and pass String message from android client to java server – Pavel Sep 24 '15 at 09:50
  • I tried it and not helped public static final String CHAT_SERVER_URL = "http://192.168.0.33"; – Pavel Sep 24 '15 at 09:59
  • @Pavel Where is your server running? In your computer's localhost? or somewhere else? – frogatto Sep 24 '15 at 10:02
  • Yes on my computer's localhost. Standart JavaSE application – Pavel Sep 24 '15 at 10:13
  • @Pavel So, First off check out if your server is listening on localhost interface. You could find out using `netstat` command. Or enter `http://localhost/` in your browser on your computer. – frogatto Sep 24 '15 at 11:35
0

@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
            }
        });
    }
};
Angad Tiwari
  • 1,738
  • 1
  • 12
  • 23
  • its simple url public static final String CHAT_SERVER_URL = "http://192.168.0.211:8000" – Angad Tiwari Sep 24 '15 at 10:18
  • u can try the ip of the chat server assign by server like http://192.168.0.10:4444 or http://10.0.2.2:4444 instead of http://127.0.0.1:4444 – Angad Tiwari Sep 24 '15 at 10:21
  • http://127.0.0.1:4444 is not recognized if the code is run on android (if it is an android app) – Angad Tiwari Sep 24 '15 at 10:21
  • I have error if tried this structure FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{smsagent.sedi.ru.sockets/smsagent.sedi.ru.sockets.MainActivity}: java.lang.RuntimeException: java.net.URISyntaxException: Illegal character in scheme at index 0: 192.168.0.33:4444 – Pavel Sep 24 '15 at 10:22
  • at which IDE u r working .. ? if android studio - then add dependency at your gradle file compile 'com.github.nkzawa:socket.io-client:0.5.2' – Angad Tiwari Sep 24 '15 at 10:24
  • "java.lang.RuntimeException: java.net.URISyntaxException: Illegal character in scheme at index 0:" - means add url with http:// – Angad Tiwari Sep 24 '15 at 10:24
  • ok Pavel.. is the chat server is on diff machine or ours? and your IDE? – Angad Tiwari Sep 24 '15 at 10:29
  • Yes server run in IntellijIDEA on my computer – Pavel Sep 24 '15 at 10:33
  • then use url as http://10.0.2.2:4444 for socket connection and dependency - compile 'com.github.nkzawa:socket.io-client:0.5.2' and debug or log for mSocket.connect() – Angad Tiwari Sep 24 '15 at 10:35
  • I do not understand you. How can i use url as 10.0.2.2:4444 for socket connection? – Pavel Sep 24 '15 at 10:52
  • http://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator-in-eclips – Angad Tiwari Sep 24 '15 at 10:55
  • CHAT_SERVER_URL = "http://10.0.2.2:4444"; not work((((( and in debug after mSocket.connect(); line mSocket.connected() - show true – Pavel Sep 24 '15 at 11:04
  • mSocket.connected() == true means the socket connection b/w client & server is est. – Angad Tiwari Sep 24 '15 at 11:14
  • n how can u tell that the CHAT_SERVER_URL = "10.0.2.2:4444" not working... as the socket connection is showing true – Angad Tiwari Sep 24 '15 at 11:18
  • it is in debug but mSocket.connect(); Log.d("mylog", String.valueOf(mSocket.connected())); show false – Pavel Sep 24 '15 at 11:24
  • how this is possible.. it means you not debugging the app properly – Angad Tiwari Sep 24 '15 at 11:43