发送实时流数据时套接字连接丢失

时间:2013-03-07 04:28:56

标签: java android sockets voip serversocket

我在这里挣扎......

我正在尝试确定数据是否已使用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();
      }
    }

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

网络崩溃后大约2分钟会出现错误。

注意,与write操作相反,操作connect是同步的,所以它实际上等待来自对方的响应,如果没有respose,它将抛出异常。< / p>