如何在不关闭输出流的情况下连续发送和接收数据?

问题描述:

我正在使用热点服务器和客户端连接在两台设备之间使用wifi传输数据的应用程序。如何在不关闭输出流的情况下连续发送和接收数据?

我正在使用以下代码将数据从客户端发送到服务器。

OutputStream stream = socket.getOutputStream(); 

        stream.write("".getBytes()); 
        ContentResolver cr = context.getContentResolver(); 


        InputStream is = null; 
        try { 
         is = new ByteArrayInputStream(data.getBytes()); 
        } catch (Exception e) { 
         Log.d("TEST", e.toString()); 
        } 
        copyFile(is, stream); 

这是我的复制文件方法。

public static boolean copyFile(InputStream inputStream, OutputStream out) { 
      byte[] buf = new byte[1024]; 
      while (true) { 
       try { 
        int len = inputStream.read(buf); 
        if (len != -1) { 
         out.write(buf, 0, len); 
        } else { 

         //out.flush(); 
//out.close(); 


         return true; 
        } 
       } catch (IOException e) { 
        Log.d("TEST","faaaaaaaaaaaaaaaa::::"+ e.toString()); 
        return false; 
       } 
      } 
     } 

而且,这是我的客户端代码。

try { 

       AppFonts.serverSocket = new ServerSocket(8988); 
       Log.d("TEST", "Server: Socket opened0000000000"); 
       AppFonts.socket = AppFonts.serverSocket.accept(); 

      Log.d("TEST", "Server: connection done0000000000000000"); 
      // OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream()); 


      String gotData = convertStreamToString(AppFonts.socket.getInputStream()); 
      Log.d("TEST", "Server: Socket Files,,,,,,"); 
      Log.d("TEST", "server: copying files0000000000 " + gotData); 

      return gotData; 
     } catch (IOException e) { 
      Log.e("TEST", "fsat , " + e.getMessage()); 
      e.printStackTrace(); 
      return null; 
     } catch (Exception e2) { 
      e2.printStackTrace(); 
      return null; 

     } 

在copyFile()方法中,我尝试了out.flush(),但数据未在客户端接收。

-

  • 我不想收的OutputStream,因为我想在两个设备之间更多的交流。
  • 当我使用out.close();数据成功发送到客户端,但没有使用该数据没有达到客户端。
  • 服务器从copyFile()方法向客户端发送数据。从客户端收到的数据与使用outputstream.close()时发送的内容完全相同。但是当我删除outputstream.close()时没有任何反应。或/和使用outputstream.flush();.在看到Android: Socket is closed这个链接后,我尝试了flush()方法。
+0

问题的标题与您的问题无关。你说客户端没有收到数据,但服务器不发送任何*数据。另外,你不会说服务器收到了什么*。 –

+0

服务器从copyFile()方法向客户端发送数据。从客户端收到的数据与使用outputstream.close()时发送的数据完全一致;但是当我删除outputstream.close();或者使用outputstream.flush()时,什么都不会发生。在看到https://*.com/questions/14478450/android-socket-is-closed这个链接后,我尝试了flush()方法。 –

+0

“我正在使用以下代码将数据从客户端发送到服务器。”这就是客户端代码,对吧?而服务器端代码是服务器端的一面,你说的*是客户端,对吧?也许你应该编辑你的问题来澄清它。 –

为了有连续流中的数据,你可以做这样的事情

Timer scheduler = new Timer(); 
scheduler.scheduleAtFixedRate(task,1000,1000); 

final Runnable task = new Runnable() { 
    public void run() { 
     try { 
      is = new ByteArrayInputStream(data.getBytes()); 
     } catch (Exception e) { 
      log.d("TEST", e.toString()); 
     } 
    } 
}; 

为了不断获取数据的更新流。

关闭输出流并不意味着流中的数据将不再提供给您。这意味着您正在使用的输出流已从您的内存中解除分配,并且为了读取您需要读取下一个数据流所需的新数据。

+0

我希望双方的沟通不要只从一方发送。并关闭输出流意味着写入数据流完成,现在它可以发送到接收器,但当我关闭输出流套接字关闭,所以我不得不重新连接它再次发送数据,这不是最好的编码习惯。 –

+0

当您调用accept()时,服务器将等待任何连接。我建议你仔细阅读这个链接。 http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html。这将清除你的许多疑问 –