Question

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. 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. please help.

Was it helpful?

Solution

  • You can delete the setReuseAddress() call; 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; it would be more suitable to use 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top