套接字客户端 - 服务器(android)

时间:2015-11-18 09:10:31

标签: android sockets chat

抱歉我的英文。我正在尝试创建客户端和服务器。现在,我的逻辑是这样的:

启动应用 - > 启动服务器 - > 服务器收听所有邮件 - > 如果客户端向服务器发送消息 - > 消息转到服务器 - > 服务器收到的消息 - > 服务器需要向其所有客户发送消息

但是服务器没有这样做

**服务器需要向其所有客户发送消息**

或者

客户端没有收听服务器

我的问题:为什么客户端不接受来自服务器的消息?

//Main

//send message
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sendText.getText().toString().equals("")) {
                    return;
                }

                if(chatClientThread==null){
                    return;
                }

                chatClientThread.sendMsg(sendText.getText().toString() + "\n");
            }
        });

         //method create server and client, cliend connect to server
        createServer(port);
    }

    public void createServer(int port) {
        //create server
        ChatServerThread chatServerThread = new ChatServerThread(port);
        chatServerThread.start();

        //subscribe to server 192.168.31.101 - ip server
        createClient("Testov", "192.168.31.101", port);
    }

//method for create client
 public void createClient(String name, String ipAdress, int SocketServerPORT) {
        chatClientThread = new ChatClientThread(
                name, ipAdress, SocketServerPORT);
        chatClientThread.start();
    }


//this is server class
private class ChatServerThread extends Thread {

        int SocketServerPORT;

        public ChatServerThread(int SocketServerPORT) {
            //set port
            this.SocketServerPORT = SocketServerPORT;
        }


        @Override
        public void run() {
            Socket socket = null;
            DataInputStream dataInputStream = null;
            DataOutputStream dataOutputStream = null;

            try {
                //create server socket
                serverSocket = new ServerSocket(SocketServerPORT);
                //waite connect
                socket = serverSocket.accept();

                Log.e("connect", "server side");

                //for input and output
                dataInputStream = new DataInputStream(socket.getInputStream());
                dataOutputStream = new DataOutputStream(socket.getOutputStream());

                //listener
                while(true) {
                    if (dataInputStream.available() > 0) {
                        //read line
                        String newMsg = dataInputStream.readUTF();
                        //send line to client
                        dataOutputStream.writeUTF(newMsg);

                        final String ms = newMsg;
                        MainActivity.this.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), ms, Toast.LENGTH_SHORT).show();
                            }
                        });

                        //end thread
                        dataOutputStream.flush();
                    }
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }

//this is client side

private class ChatClientThread extends Thread {

    Socket socket = null;
    DataOutputStream dataOutputStream = null;
    DataInputStream dataInputStream = null;

    String name;
        String dstAddress;
        int dstPort;

        String msgToSend = "";
        boolean goOut = false;
        String msgLog = "";

        ChatClientThread(String name, String address, int port) {
            this.name = name;
            dstAddress = address;
            dstPort = port;
        }

        @Override
        public void run() {

            try {
                //set ip adress and port
                socket = new Socket(dstAddress, dstPort);

                //for get and post data
                String mesageFromServer = null;
                dataInputStream = new DataInputStream(socket.getInputStream());

                while(!goOut) {
                        //wait message from server
                        mesageFromServer = dataInputStream.readUTF();
                        //output
                         Log.e("client", mesageFromServer);

                        final String ms = mesageFromServer;
                        MainActivity.this.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), ms, Toast.LENGTH_SHORT).show();
                            }
                        });

                         msgToSend = "";
                }


            } catch (UnknownHostException e) {
                e.printStackTrace();
                final String eString = e.toString();
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, eString, Toast.LENGTH_LONG).show();
                        Log.e("errror", eString);
                    }

                });
            } catch (IOException e) {
                e.printStackTrace();
                final String eString = e.toString();
                MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this, eString, Toast.LENGTH_LONG).show();
                        Log.e("errror", eString);
                    }

                });
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if (dataOutputStream != null) {
                    try {
                        dataOutputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                if (dataInputStream != null) {
                    try {
                        dataInputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }

        }

        private void sendMsg(String msg){
            try {
                dataOutputStream = new DataOutputStream(socket.getOutputStream());
                dataOutputStream.writeUTF(msg);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private void disconnect(){
            goOut = true;
        }
    }

0 个答案:

没有答案