客户端不会向服务器发送文本

时间:2017-03-16 08:09:53

标签: java android sockets

我目前正在使用两个Android设备之间的Java套接字创建一个简单的聊天应用程序 我的问题是我完全输入IP地址和端口后客户端连接到服务器 然后,当我尝试从客户端向服务器发送任何文本时,客户端突然停止我搜索了很多,但我找不到我的代码有什么问题

服务器端:

<gcse:search></gcse:search>

客户端:

Thread TH=new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    status.setText("hosted on"+ ip);
                    ss=new ServerSocket(8080);
                    while(true) {
                        S = ss.accept();

                        status.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            status.setText("connected");
                                        }
                                    }
                        );
                        CommunicationThread commThread = new CommunicationThread(S);
                        new Thread(commThread).start();

    class CommunicationThread implements Runnable {
        private Socket ClientSocket;
        DataInputStream DIS;

        public CommunicationThread(Socket ClientSocket) throws IOException {
            this.ClientSocket = ClientSocket;

            DIS = new DataInputStream(ClientSocket.getInputStream());
            data = DIS.readUTF();
        }
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {

                data = data.toString();
                tv_receive.post(new Runnable() {

                    public void run() {
                        tv_receive.setText(data);
                    }

我在客户端崩溃后得到的内容::

 btn_connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TH=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            S = new Socket(et_IP_Address.getText().toString(),Integer.parseInt(et_port.getText().toString()));
                            DOS = new DataOutputStream(S.getOutputStream());


                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                });// End of thread
                TH.start();
            }
        }); // end of  buutton

        // Sending
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TH=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //while(true) {

                            DOS.write(Integer.parseInt(et_message.getText().toString()));
                            tv_transmit.post(new Runnable() {
                                @Override
                                public void run() {
                                    tv_transmit.setText(et_message.getText());

                                }
                            });
                            try {
                                DOS.flush();
                            }
                            catch (IOException e) {
                                e.printStackTrace();
                            }

                            // }
                        }
                        catch (IOException e) {
                            e.printStackTrace();
                        }

                        try {
                            DOS.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        }

1 个答案:

答案 0 :(得分:0)

您正在尝试将字符串解析为整数,而它不是数字

  

java.lang.NumberFormatException:无效的int:&#34; hi&#34;

这可能是这一行

Integer.parseInt(et_port.getText().toString())

或者

DOS.write(Integer.parseInt(et_message.getText().toString()));

它在Mainactivity第75行

因此,要么删除Integer.parseInt()调用,要么输入有效的数字整数值