简体   繁体   English

联网单向连接android

[英]networking oneway connection android

my todays problem is the following: 我今天的问题如下:

I have a network connection, at which both sides have to send commands (bytes), but my reader blocks my writer, thats how it seems to be. 我有一个网络连接,双方都必须发送命令(字节),但是我的读者阻止了我的作家,那就是事实。 If I "disable" my reader (by deleting the reader from source) the writer works as he should, But when my reader is there, too, my writer just do the half of the work. 如果我“禁用”我的阅读器(通过从源中删除阅读器),那么作家将按应有的方式工作,但是当我的读者也在那里时,我的作家将完成一半的工作。

Lets say my writer has to send a command with an interval of 15 seconds and stay aware of incoming commands whach has to be answered by a little byte block. 可以说,我的编写者必须每隔15秒发送一次命令,并注意传入的命令,必须用一个小字节块来回答。 The answer block is send, but the command from the interval seems blocked. 答案块已发送,但间隔中的命令似乎被阻止。

Here my source: 这是我的资料来源:

    protected String doInBackground(URL... params) {
        try {
            btw1 = (byte) sendbeg;
            btw2 = (byte) w2;
            btw3 = (byte) w3;
            btw4 = (byte) w4;
            btw5 = (byte) w5;
            if (w5 == 79) {
                btw6 = (byte) mins;
                btw7 = (byte) seks;
                btw8 = (byte) w6;
                btw9 = (byte) sendend;
            } else {
                btw6 = (byte) w6;
                btw7 = (byte) sendend;
            }
            SocketAddress sockaddr = new InetSocketAddress("192.168.0.7", 2001);
            sock = new Socket();
            int timeout = 1000; // 1000 millis = 1 second
            sock.connect(sockaddr, timeout);
            sock.setReuseAddress(true);
            System.out.println(sock);
            DataOutputStream dos = new DataOutputStream(
                    (OutputStream) sock.getOutputStream());
            BufferedWriter wrtr = new BufferedWriter(
                    new OutputStreamWriter(dos), 300);
            DataInputStream dis = new DataInputStream(sock.getInputStream());
            BufferedReader rdr = new BufferedReader(new InputStreamReader(dis),
                    300);
            getbyte((byte) btw1, (byte) btw2, (byte) btw3, (byte) btw4,
                    (byte) btw5, (byte) btw6, (byte) btw7, (byte) btw8,
                    (byte) btw9, (byte) btw10); //getbyte works fine, too. It's just there for putting the single bytes into an array.
            System.out.println(btw.length);
            dos.write(btw);
            diny1 = (dis).read();           
diny2 = (dis).read();           
diny3 = (dis).read();           
diny4 = (dis).read();

            diny5 = (dis).read();           
dinymin = (dis).read();
            dinysek = (dis).read();
            diny6 = (dis).read();
            diny7 = (dis).read();
            if (diny5 != 79) {
                System.out.println("diny" + diny1 + " " + diny2 + " " + diny3
                        + " " + diny4 + " " + diny5 + " " + dinymin + " "
                        + dinysek);
            } else {
                if (diny7 != 5) {
                    diny6 = 0;
                    diny7 = 0;
                }
                System.out.println("diny" + diny1 + " " + diny2 + " " + diny3
                        + " " + diny4 + " " + diny5 + " " + dinymin + " "
                        + dinysek + " " + diny6 + " " + diny7);
            }
            dos.close();
            wrtr.close();
            dis.close();
            rdr.close();
            if (diny5 != 32) {
                sendbeg = 3;
                w2 = diny3;
                w3 = diny2;
                w4 = 48;
                w5 = 32;
                w6 = 11;
                sendend = 5;
                System.out.println(diny5 + " ^^ ");
                doInBackground();

            }
            System.out.println("case 144-49-000.1" + context);
            reccom(context);
            sock.close();

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

            System.out.println("IO error " + e);
        }

        return "Done";
    }

My intervalsource works fine, because it works, if the reader is inactive, so i think the problem is in this code above. 我的intervalsource工作正常,因为如果读者处于非活动状态,它就可以工作,所以我认为问题出在上面的代码中。 please help. 请帮忙。

  • You can delete the setReuseAddress() call; 您可以删除setReuseAddress()调用; it's only for server sockets. 它仅用于服务器套接字。
  • AsyncTask is not exactly the right class for your readers and writers if you stay in a loop there all the time ; 如果您一直呆在一个循环中, AsyncTask并不是完全适合您的读者和作家的类; it would be more suitable to use Thread . 使用Thread更合适。
  • It sounds as if you need a seperate reader and writer Thread if one is not allowed to block the other. 听起来好像您需要一个单独的读取器和写入器线程(如果不允许一个线程阻塞另一个线程)。
  • You'll probably have to use synchronized along the way if reader and writer act independently 如果读者和作家独立行动,则可能必须使用synchronized方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM