Android的套接字服务器的DataInputStream着秀再次经过DATAS服务器、Android、DATAS、DataInputStream

2023-09-04 03:03:38 作者:把孤独喂好

前几天一直问这个问题一次,但并没有解决我的问题 所以,我还是那句话,我的问题。 在客户端部分,我想应该是没有问题的,因为使用串行端口检查是正常的。

客户端的下面是部分

 尝试{
                byte []的BUF = {(字节)0x80的,
                             (字节)0x70,
                             (字节)地址0x60,
                             (字节)为0x50,
                             (字节)0X40,
                             (字节)的0x30
                        };
                dataOutputStream.write(BUF); // writeBytes(字符串str)
                dataOutputStream.flush();
            }赶上(例外OBJ){

            }
 

现在的问题是服务器, 我真的可以接收数据,但只显示第一组数据,就没有办法显示第二组数据。

即使我发出第二组数据,还是只显示第一组数据的

服务器以下是部分(改版!)

 私人可运行socket_server =新的Runnable(){
    公共无效的run(){
        handler.post(新的Runnable(){
            公共无效的run(){
                test.setText(听......+ getMyIp());
            }
        });
        尝试{
            ServerSocket的=新的ServerSocket(8888);
            客户端的Socket的ServerSocket.accept =();

            handler.post(新的Runnable(){
                公共无效的run(){
                    test.setText(已连接);
                }
            });
            尝试 {
                在的DataInputStream =新的DataInputStream(client.getInputStream());
                    做{
                        byte []的重新=新的字节[6];
                        in.readFully(重);
                        短TMP [] = {(短)(0xFF的&安培;重新[0]),
                               (短)(0xFF的&安培;重新[1]),
                               (短)(0xff的&安培;再[2]),
                               (短)(0xff的&安培;再[3]),
                               (短)(0xFF的&安培;重新[4]),
                               (短)(0xff的&安培;再[5]),};
                        INT ddata0,ddata1,ddata2,ddata3,ddata4,ddata5;
                         ddata0 = TMP [0];
                         ddata1 = tmp中[1];
                         ddata2 = TMP [2];
                         ddata3 = tmp中[3];
                         ddata4 = tmp中[4];
                         ddata5 = tmp中[5];
                         DD0 = Integer.toHexString(ddata0);
                         DD1 = Integer.toHexString(ddata1);
                         DD2 = Integer.toHexString(ddata2);
                         DD3 = Integer.toHexString(ddata3);
                         DD4 = Integer.toHexString(ddata4);
                         DD5 = Integer.toHexString(ddata5);
                         handler.post(新的Runnable(){
                             公共无效的run(){
                                test2.setText(DD0 + DD1 + DD2 + DD3 + DD4 + DD5);
                             }
                         });
                    }而(在!= NULL);
            }赶上(例外五){
                handler.post(新的Runnable(){
                    公共无效的run(){
                        test.setText(错误);
                    }
                });
            }
        }赶上(IOException异常E){
            handler.post(新的Runnable(){
                公共无效的run(){
                    test.setText(套接字失败);
                }
            });
        }
}};
 

解决方案

我看你用两次刷新。在你写字节后。是否有必要为您的操作尽量把线程睡眠第二次或2这样你就可以得到整个流。我怀疑字节的过程是有点慢于预期。

dataOutputStream.write(BUF);

视频下载(1000);

dataOutputStream.flush();

菜鸟学习编程之C 编制同步套接字基础服务器

A few days ago has been asked this question once, but did not solve my problem So I then again my question. In the client part, I think it should be no problem,because the use of serial ports examination is normal

The following are part of the client

try {
                byte[] buf ={(byte) 0x80,
                             (byte) 0x70,
                             (byte) 0x60,
                             (byte) 0x50,
                             (byte) 0x40,
                             (byte) 0x30
                        };                                                          
                dataOutputStream.write(buf); //writeBytes(String str)   
                dataOutputStream.flush();               
            }catch(Exception obj){

            }    

The problem is SERVER, I really can receive data, but only display the first set of data, there is no way to display the second set of data.

Even if I send out a second set of data, still show only the first set of data

The following are part of the servers(revised!)

private Runnable socket_server = new Runnable(){
    public void run(){
        handler.post(new Runnable() {
            public void run() {
                test.setText("Listening...." + getMyIp());
            }
        });
        try{
            serverSocket = new ServerSocket(8888);
            Socket client = serverSocket.accept();

            handler.post(new Runnable() {
                public void run() {
                    test.setText("Connected.");
                }
            });
            try {
                DataInputStream in = new DataInputStream(client.getInputStream());                    
                    do{
                        byte[] re = new byte[6];
                        in.readFully(re);
                        short tmp[] = {(short) (0xff & re[0]),
                               (short) (0xff & re[1]),
                               (short) (0xff & re[2]),
                               (short) (0xff & re[3]),
                               (short) (0xff & re[4]),
                               (short) (0xff & re[5]),};
                        int ddata0,ddata1,ddata2,ddata3,ddata4,ddata5;                 
                         ddata0=tmp[0];
                         ddata1=tmp[1];
                         ddata2=tmp[2];
                         ddata3=tmp[3];
                         ddata4=tmp[4];
                         ddata5=tmp[5];                     
                         dd0=Integer.toHexString(ddata0);
                         dd1=Integer.toHexString(ddata1);
                         dd2=Integer.toHexString(ddata2);
                         dd3=Integer.toHexString(ddata3);
                         dd4=Integer.toHexString(ddata4);
                         dd5=Integer.toHexString(ddata5);
                         handler.post(new Runnable() {
                             public void run() {
                                test2.setText(dd0+dd1+dd2+dd3+dd4+dd5);
                             }
                         });
                    }while(in!=null);                    
            } catch (Exception e) {
                handler.post(new Runnable() {
                    public void run() {
                        test.setText("error");
                    }
                });
            }                   
        }catch(IOException e){
            handler.post(new Runnable() {
                public void run() {
                    test.setText("socket failed");
                }
            });
        }           
}};   

解决方案

I see you use flush twice. Before you write bytes and after. Is it necessary for your operation try to put the thread to sleep for a second or 2 so you can get the whole stream. I suspect the byte process is a little slower than expected.

dataOutputStream.write(buf);

Thread.sleep(1000);

dataOutputStream.flush();