无法和Android的客户端应用程序读取来自TCP服务器数据 [英] Cant read data from TCP Server with Android Client APP

查看:161
本文介绍了无法和Android的客户端应用程序读取来自TCP服务器数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Java中我的Andr​​oid应用程序。我试图创建TCP服务器的TCP连接。我可以连接到服务器的另一个应用程序,这样我可以发送和TCP服务器接收。用我的code和我的节目,我可以将消息发送到服务器非常EAZY,但我有麻烦从服务器接收邮件。

 专用插座插座;
私人最终诠释SERVERPORT = 6060;
私人最终字符串SERVER_IP =192.168.0.8;
公众的TextView电视;
私人PrintWriter的输出;
私人的InputStream的;


@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    电视=(TextView中)this.findViewById(R.id.textView1);
     新主题(新ClientThread())启动()。

}
 

下面是我的问题,我不知道如何来接收字符串或字节的服务器。当我在手机上运行我的应用程序会关闭打开的窗口,说程序停止工作。如果我删除本条code(公共无效ONCLICK2)我可以发送邮件到服务器。

 公共无效ONCLICK2(查看视图){

       尝试 {
        在= socket.getInputStream();
    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
        字节数组[] =新的字节[1];
        尝试 {
            INT I = in.read(阵列);
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }


  }
 

所以,请帮助我的code的部分。我怎样才能收到TCP服务器字符串发送。

 公共无效的onClick(视图查看){
                尝试 {
                的EditText等=(EditText上)findViewById(R.id.editText1);
                。字符串str = et.getText()的toString();

                    通过out.println(STR);
                    了out.flush();

                }赶上(例外五){
                    e.printStackTrace();
                }
            }


类ClientThread实现Runnable {

     @覆盖
            公共无效的run(){

               尝试 {
                   InetAddress类serverAddr = InetAddress.getByName(SERVER_IP);

                    插座=新的Socket(serverAddr,SERVERPORT);

                    OUT =的新PrintWriter(新的BufferedWriter(
                            新OutputStreamWriter(socket.getOutputStream())),
                                    真正);



           }赶上(UnknownHostException异常E1){
                     e1.printStackTrace();
         }赶上(IOException异常E1){
                    e1.printStackTrace();
                }

         }

}
 

解决方案

简单地说,问题是你拦截一个未知长度的字节数组,并试图将它们存储在阵列块的大小。此外,理想的是追加前的数据分组大小的分组,并且或许创建截距在单独的线程等待传入分组。

要只是解决您的ONCLICK2,你应该做到以下几点:

 字节[]数据=新的字节[6556];
尝试 {
   在= socket.getInputStream();
   //注:本数据字节数组将包含空值,如果
   //的6556尺寸下
   INT大小= in.read(数据);

   //发送到LogCat中
   Log.e(字符串,data.toString());
}赶上(例外五){
   e.printStackTrace();
}
 

我没有测试此code,但是这应该解决您的问题。

That is my program in java for my android app. I tried to create tcp connection with tcp server. I can connect to server with another applications so that i can send and receive from tcp server. With my code and with my program i can send messages to server very eazy, but i have troubles with receiving messages from server.

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();

}   

Here is my problem i dont know how to receive strings or bytes from server. When i run my app on phone it closes the open window and say that program stop working. If i delete this section of code(public void ONCLICK2) i can transmit messages to server.

  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();
        }


  }

So please help me with that section of code. How can i receive string send from TCP server.

    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();
                }

         }

}

解决方案

Simply put, the problem is you are intercepting the byte array of an unknown length and attempting to store them in array the size of one. Furthermore, it is ideal to append the packet size prior to the data in the packet and perhaps create the intercept in a separate thread waiting for incoming packets.

To just fix your ONCLICK2 you should do the following:

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();
}

I have not tested this code, but this should fix your problem.

这篇关于无法和Android的客户端应用程序读取来自TCP服务器数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆