【问题标题】:Cant read data from TCP Server with Android Client APP无法使用 Android Client APP 从 TCP Server 读取数据
【发布时间】:2014-10-13 05:20:43
【问题描述】:

这是我在 java 中为我的 android 应用程序编写的程序。我试图创建与 tcp 服务器的 tcp 连接。我可以使用其他应用程序连接到服务器,以便我可以从 tcp 服务器发送和接收。使用我的代码和我的程序,我可以非常轻松地向服务器发送消息,但我无法从服务器接收消息。

private  Socket socket;
private  final int SERVERPORT = 6060;    
private  final String SERVER_IP = "192.168.0.8";
public  TextView tv;
private PrintWriter out;
private InputStream in;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv=(TextView)this.findViewById(R.id.textView1);
     new Thread(new ClientThread()).start();

}   

这是我的问题,我不知道如何从服务器接收字符串或字节。当我在手机上运行我的应用程序时,它会关闭打开的窗口并说该程序停止工作。如果我删除这部分代码(public void ONCLICK2),我可以将消息传输到服务器。

  public void   ONCLICK2(View view)  {

       try {
        in=socket.getInputStream();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        byte array[]=new byte[1];
        try {
            int i=in.read(array);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


  }

所以请帮我处理那段代码。如何接收从 TCP 服务器发送的字符串。

    public void onClick(View view) {
                try {
                EditText et = (EditText) findViewById(R.id.editText1);
                String str = et.getText().toString();

                    out.println(str);
                    out.flush();

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


class ClientThread implements Runnable {

     @Override
            public void run() {

               try {
                   InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

                    socket = new Socket(serverAddr, SERVERPORT);

                    out = new PrintWriter(new BufferedWriter(
                            new OutputStreamWriter(socket.getOutputStream())),
                                    true);



           } catch (UnknownHostException e1) {
                     e1.printStackTrace();
         } catch (IOException e1) {
                    e1.printStackTrace();
                }

         }

}

【问题讨论】:

    标签: java android tcp client


    【解决方案1】:

    简单地说,问题是您正在截取未知长度的字节数组并试图将它们存储在大小为 1 的数组中。此外,理想的做法是在数据包中的数据之前附加数据包大小,并可能在等待传入数据包的单独线程中创建拦截。

    要修复您的 ONCLICK2,您应该执行以下操作:

    byte[] data = new byte[6556];
    try {
       in = socket.getInputStream();
       // NOTE: The data byte array will contain empty values if
       // under the size of 6556
       int size = in.read(data);
    
       // send to LogCat
       Log.e("String", data.toString());
    } catch (Exception e) {
       e.printStackTrace();
    }
    

    我尚未测试此代码,但这应该可以解决您的问题。

    【讨论】:

    • 现在我接受了一些数据包,但我无法识别它们。我如何为传入的数据包创建单独的线程并在 textview 或类似的东西中显示它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    相关资源
    最近更新 更多