Doubt on using Socket in java on android

1

I want to implement data transmission via Socket, I am trying to implement Socket.io but I do not understand the operation. I want to make communication between two android devices. I know it has the server side and the client side. But I can not understand if there is a need for a web server (device > server > device) or they communicate only with each other (device > device).

    
asked by anonymous 25.06.2017 / 17:52

1 answer

1

You can put devices to communicate point to point, without problems. Just keep in mind that: there must be a device that will be treated as a server, that is, who will be listening to a direct connection. And the second device is the client, that is, who is going to connect to the server.

Note: build the Server socket and client socket in threads to not lock the wires of the devices.

Rudimentary code of what the socket could look like:

Side Customer

try {
            String SERVER_SOCKET = "127.0.0.1";
            int SERVER_PORT = 8989;
            Socket socket = new Socket(SERVER_SOCKET, SERVER_PORT);

            String inputLine;
            BufferedReader in = new BufferedReader( InputStreamReader(socket.getInputStream()) );

            while( (inputLine = in.readLine()) != null ){
                Log.d("socketcst", "Recebendo dados do servidor" );
                Log.d("socketcst", inputLine );
            }

            Log.d("socketcst", "Connectou ao servidor");
        } catch (UnknownHostException e) {
            Log.d("socketcst", "UnknownHostException: " + e.toString());
        } catch (IOException e) {
            Log.d("socketcst", "IOException: " + e.toString());
        } finally {
            if (socket != null) {
                try {
                    socket.close();
                    Log.d("socketcst", "Fechou a conexão com o servidor");
                } catch (IOException e) {
                    Log.d("socketcst", "IOException - Close: " + e.toString());
                }
            }
        }

Server Side

private String host = "192.168.0.31";
    private int port = 8989;
    private int conexoes =  50;

    Socket socket = new Socket(this.port, this.conexoes);
    InetAddress inet = socket.getInetAddress();     
    System.out.println("Servidor iniciado na porta " + this.port );

    while (!socket.isClosed()) {

        socket.accept();

        try {

            InputStreamReader entrada = new InputStreamReader(socket.getInputStream());
            OutputStreamWriter saida = new OutputStreamWriter(socket.getOutputStream());
            PrintWriter os = new PrintWriter(saida);

            String inputLine;   
            BufferedReader in = new BufferedReader( entrada );
            JSONObject dadosEntrada = null;

            while ((inputLine = in.readLine()) != null) {
                    Log.d("socketcst", inputLine );
             }

        } catch (IOException e) {
            System.out.println( e.getMessage() );
        }  
    }

    socket.close();

All code must be run as a service or threads not to block the application.

    
25.06.2017 / 22:38