0

我在这里挣扎...

我正在尝试确定数据是否已使用该OutputStream对象通过 TCP 套接字成功发送到服务器。用于测试模拟器套接字通信在 30 秒后丢失。对于写入数据OutputStream.write();,它不会抛出异常,并且本地服务器连续运行它不会崩溃,一段时间后只有 tcp 套接字连接丢失。套接字类中的所有方法都返回,就好像套接字处于活动状态并且正在工作一样。我在这里做错了什么吗?当流/套接字实际上没有在缓冲区中发送数据时,是否有任何套接字实现或流实现可以用来获取异常或错误?在套接字上设置setSoTimeout()似乎也没有做任何事情。

请指导我...

这是我的代码:

private void sendRec() {
    int lstream;
    int port = 1012;
    byte[] byterecv = new byte[1040];

    while (true) {
        System.out.println("POOL-2");
        synchronized (recSendThread) {
            try {
                recSendThread.wait(20);
            } catch (InterruptedException e) {
            }
        }

        if (stopcall == true) {
            // break;
        }

        try {
            // Provides a client-side TCP socket
            Socket clientRec = new Socket();

            // serverSocket = new ServerSocket(port);
            // serverSocket.setSoTimeout(5000);

            // Connects this socket to the given remote host address and
            // port
            clientRec.connect(new InetSocketAddress("192.168.1.36", port));

            System.out.println("Just connected to "
                    + clientRec.getRemoteSocketAddress());
            System.out.println("SENTS Rec BEFORE");

            // output streams that write data to the network
            OutputStream outToServerRec = clientRec.getOutputStream();
            DataOutputStream outStreamRec = new DataOutputStream(
                    outToServerRec);

            outStreamRec.write(bData);
            System.out.println("SENTS Rec AFTER");

            // input streams that read data from network
            InputStream inFromServerRec = clientRec.getInputStream();
            // clientRec.setSoTimeout(5000);
            DataInputStream inStreamRec = new DataInputStream(
                    inFromServerRec);
            while ((lstream = inStreamRec.read(byterecv)) != -1) {
                System.out.println("startrec bytearray -- "
                        + byterecv.length);
                bos1.write(byterecv, 0, lstream);
            }

            inStreamRec.close();// for closing dataouputstream
            clientRec.close();// for closing socket connection
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}





Here is my receiver and player code..



  /**
   * start receiving the voice data from server
   * */
       protected void startplay() {
    System.arraycopy(frndid, 0, playByteData, 0, 4);
    System.arraycopy(userid, 0, playByteData, 4, 4);

        ByteBuffer.wrap(sessionid).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer().
            put(call_sessionid);
    System.arraycopy(sessionid, 0, playByteData, 8, 4);
    int lstream;
    int port = 1014;
    while (true) {
        System.out.println("POOL-3");
        try {
            if (stopcall == true) {
                System.out.println("BREAKEDDDD1111");
                //break;
            }
            // Host name
            // port++;
            InetAddress addressPlay = InetAddress.getByName("192.168.1.36");
            // Creates a new streaming socket connected to the target host
            Socket clientPlay = new Socket(addressPlay, port);
            System.out.println("Just connected to play : " + 
                                                clientPlay.getRemoteSocketAddress());
            System.out.println("SENTS Play BEFORE");

            // output streams that write data

            OutputStream outToServer = clientPlay.getOutputStream();
            DataOutputStream outStreamPlay = new DataOutputStream(outToServer);
            outStreamPlay.write(playByteData);


            System.out.println("SENTS Play after");

            // input streams that read data
            InputStream inFromServerPlay = clientPlay.getInputStream();
            DataInputStream inStreamPlay = new DataInputStream(inFromServerPlay);
            //clientPlay.setSoTimeout(5000);
            while ((lstream = inStreamPlay.read(byteArray)) != -1) {
                System.out.println("startplay() bytearray -- " + 
                                                          byteArray.length);
                bos.write(byteArray, 0, lstream);
            }

            inStreamPlay.close();
            clientPlay.close();// for closing play socket connection

            responseBuffer = bos.toByteArray();
            System.out.println("BAOSSIZE " + bos.size());
            bos.reset();
            bos.flush();
            bos.close();
            playing = true;
            System.out.println("res length -- " + responseBuffer.length);
            rcvbb=ByteBuffer.wrap(responseBuffer).order(ByteOrder.LITTLE_ENDIAN).
                                                    asShortBuffer().get(playShortData);

              playVoiceReceived();// plays received data

            } catch (ParseException e) {
            e.printStackTrace();
            } catch (IOException e) {
            e.printStackTrace();
        }
    }
    }

  /**
   * start playing received the voice data from server
    * */
     public void playVoiceReceived() {

    System.out.println("POOL-4");
      try {
        if (at != null) {
            if (at.getPlayState() != AudioTrack.PLAYSTATE_PLAYING) {
                at.play();// starts playing
            } else {
                System.out.println("Play BEFORE WRITE");
                // Writes the audio data to the audio hardware for playback.
                at.write(playShortData, 0, BufferElements2Play);
                System.out.println("Play AFTER WRITE");
                at.flush();
            }
        }
          } catch (Exception e) {
        e.printStackTrace();
      }
    }
4

2 回答 2

0

套接字已将数据发送到本地套接字发送缓冲区。之后发生的事情取决于本地 TCP 堆栈、网络、远程 TCP 堆栈和远程应用程序。如果您想知道远程应用程序是否获得了数据,它必须向您发送回复。

于 2013-03-07T04:55:30.640 回答
0

运营商write不检查数据是否已交付,否则将不得不等待太长时间。如果网络连接确实断开了,操作系统的 TCP 层无论如何都会尝试发送数据,但会在稍晚(即 1 分钟后)检测到问题,因为它不会收到对方的确认消息。然后它将尝试多次重新发送数据,看到问题仍然存在,然后才会报告套接字上的异常情况。要知道套接字处于异常状态,您需要对套接字执行一些操作,即再次尝试写入。尝试在这样的循环中写入:

while (true) { outStreamRec.write (data); Thread.sleep (1000L); }

它应该在网络关闭后大约 2 分钟抛出错误。

注意,与write操作相反,操作connect是同步的,所以它实际上是在等待对面的响应,如果没有响应,就会抛出异常。

于 2013-03-07T04:55:49.360 回答