Как установить многопортовое соединение от сервера к клиенту в Android Studio - PullRequest
0 голосов
/ 10 июля 2020

Я реализовал клиента в Android Studio и могу sh установить связь с сервером:

    public void run() {
    mRun = true;
    try {
        String SERVER_IP = MainActivity.mconfigipaddress;
        InetAddress serverAddr = InetAddress.getByName( SERVER_IP );      //here you must put your computer's IP address.
        String SERVER_PORTX = MainActivity.mconfigport;
        int SERVER_PORT = Integer.valueOf( SERVER_PORTX );
        Socket socket = new Socket(serverAddr, SERVER_PORT );            //create a socket to make the connection with the server
        try {
            //sends the message to the server
            mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
            //receives the message which the server sends back
            getDeviceName();
            mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            sendMessage(Constants.LOGIN_NAME+": "+ Modelox);                   // send login name

            while (mRun) {                                              //in this while the client listens for the messages sent by the server
                mServerMessage = mBufferIn.readLine();
                if (mServerMessage != null && mMessageListener != null) {
                    mMessageListener.messageReceived(mServerMessage);   //call the method messageReceived from MyActivity class
                }
            }
        } catch (Exception e) {
            Log.e("TCP", "S: Error", e);
        } finally {
            socket.close();                                             //the socket must be closed. It is not possible to reconnect to this socket
        }

    } catch (Exception e) {
        Log.e("TCP", "C: Error", e);
    }
}

, но теперь мне нужно подключить три (03) серверных порта одновременно в один Клиент, подскажите, возможно ли это и как это сделать?

IPAdress: 192.168.4.1
Used Ports:
192.168.4.1:8880 
192.168.4.1:8881 
192.168.4.1:8882 
...