Android无法使用套接字读取和写入数据

时间:2016-05-19 14:02:31

标签: java android sockets

我正在尝试使用套接字在两个设备之间发送数据基本文本,其中一个将充当 ServerSocket 。我能够连接设备,但我无法从输入流中读取数据是我的代码(服务器和客户端都相同);

代码用于写入数据:

 button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {

                    text = message.getText().toString();                                               
                    out.println(text);
                   update(text);


             }
         });

从输入流中读取数据的代码:

 //Finding problems in this code! Not Recieving exceptions but not able to make toast too.
  try{
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        while (( clientMsg=in.readLine())!=null){


   update(clientMsg);

        }
        in.close();
        socket.close();
        update("Connection Closed");
    }catch (IOException e){
        update(e.toString());
    }

更新方法:

  public void update(final String msg){
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
           Toast.makeText(activity,msg,Toast.LENGTH_LONG).show();
        }
    });

}

当设备收回其OutPutStream中的某些数据时,我并不能获得Toast。我能否知道我的代码有什么问题?

已编辑整个MessageReader类(用于从套接字读取和写入数据)

 public class MessageReader extends Thread{
    private Socket socket;
    private View view;
    private Activity activity;
    EditText message;
    Button send;
    PrintWriter out = null;
    private TextView textView;
    private String text;
    public MessageReader(Socket client, View root){
        socket = client;;
     this.view = root;

         activity = (Activity)root.getContext();
    }


    @Override
    public void run() {

        try {
            out = new PrintWriter(socket.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String clientMsg;
update("Now in MessageReader");
          message = (EditText) view.findViewById(R.id.yourMessage);
             send = (Button) view.findViewById(R.id.send);
             textView = (TextView) view.findViewById(R.id.message);

             send.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
                   update("about to send message");
                  text = message.getText().toString();



                         out.println(text);
                         update(text);


                 }
             });


        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            update("waiting for a message");
            while (( clientMsg=in.readLine())!=null){
                update(clientMsg);
                updateUi(clientMsg);

            }
            in.close();
            socket.close();
            update("Connection Closed");
        }catch (IOException e){
            update(e.toString());
        }




    }

    public void update(final String msg){
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
               Toast.makeText(activity,msg,Toast.LENGTH_LONG).show();
            }
        });

    }
    public void updateUi(final String message){

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textView.setText(message);
            }
        });
    }

}

0 个答案:

没有答案