【问题标题】:Packet loss in socket programming java套接字编程java中的丢包
【发布时间】:2013-12-18 04:45:19
【问题描述】:

我正在尝试将文件从客户端发送到服务器。下面是我尝试过的代码。但有时,在传输过程中会出现数据包丢失。我不确定我错在哪里。

服务器端代码:

public static void ReadAndWrite(byte[] aByte, Socket clientSocket,
            InputStream inputStream, String fileOutput)
                    throws FileNotFoundException, IOException {
        int bytesRead;
        FileOutputStream fileOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try
        {
        fileOutputStream = new FileOutputStream( fileOutput );
        bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        bytesRead = inputStream.read(aByte, 0, aByte.length);
        System.out.println("The length is "+bytesRead);
        int count = 0;
        do {
            count++;
            byteArrayOutputStream.write(aByte);
            bytesRead = inputStream.read(aByte);
        } while (bytesRead != -1);
        System.out.println("The count is "+count);
        System.out.println("The length is "+byteArrayOutputStream.size());
        bufferedOutputStream.write(byteArrayOutputStream.toByteArray());
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
        clientSocket.close();
        }
        catch(Exception ex)
        {
            Logger.writeLog(ex,Listen.class.getName(), LogType.EXCEPTION);  
            throw ex;
        }

客户端代码:

public  void readByteArrayAndWriteToClientSocket(
            Socket connectionSocket, BufferedOutputStream outToClient, String fileToSend ) throws Exception
    {
        try{
        if (outToClient != null) 
        {
            File myFile = new File(fileToSend);
            System.out.println(myFile.length());
            byte[] byteArray = new byte[(int) myFile.length()];

            FileInputStream fileInputStream = null;

            try {
                fileInputStream = new FileInputStream(myFile);
            } catch (IOException ex) {
                Logger.writeLog(ex, FileUtility.class.getName(), LogType.EXCEPTION);
                throw ex;
            }
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);

            try {
                bufferedInputStream.read(byteArray, 0, byteArray.length);
                outToClient.write(byteArray, 0, byteArray.length);
                outToClient.flush();
                outToClient.close();
                connectionSocket.close();


                return;
            } catch (IOException ex) {
                Logger.writeLog(ex, FileUtility.class.getName(), LogType.EXCEPTION);
                throw ex;
            }

        }
        }catch (Exception e) {
            Logger.writeLog(e, getClass().getName(), LogType.EXCEPTION);
            throw e;
        }
    }

【问题讨论】:

  • 代码看起来没问题。如果你区分文件,它们在哪里不同?
  • “丢包”是什么意思?部分数据没有收到?没有收到任何数据?或者用 Wireshark 之类的东西检查数据包显示数据包被丢弃?在任何有损耗的线路上,偶尔的数据包丢失是不可避免的; TCP 的设计考虑到了这一点,并构建了恢复机制以确保交付(如果可能)并保证顺序和完整性。
  • 您不应在发送文件后立即关闭socket。让它在终止前等待几秒钟。这将确保服务器实际接收到文件。
  • @ExtremeCoders:这绝对不是 Java 套接字的工作方式。如果是,你怎么知道要等多久才能关闭?
  • 我正在尝试传输 3500 KB 的 ZIP 文件。但传输的文件只有 3450 KB 或 3495 KB。我很少得到完整的文件。

标签: java sockets


【解决方案1】:

没有“丢包”,只是代码中的错误。

在 Java 中复制流的规范方法如下:

while ((count = in.read(buffer)) > 0)
{
   out.write(buffer, 0, count);
}

如果你事先知道字节数,并且发送方必须在传输后保持连接打开,则变为:

while (total < expected && (count = in.read(buffer, 0, expected-total > buffer.length ? buffer.length : (int)(expected-total))) > 0)
{
   out.write(buffer, 0, count);
   total += count;
}

忘记所有ByteArrayInput/OutputStreams 和额外的副本。只需从文件中读取并发送到套接字,或者从套接字中读取并写入文件。

【讨论】:

    【解决方案2】:

    sockets read 方法将在它获得您请求的所有字节时返回,或者在它停止从网络接收数据时返回。

    由于任何真实网络中的传输经常中断,因此您需要不断发出读取调用,直到获得所需的字节数。

    你需要这样的代码:

            char [] buffer = new char[1024];
            int expect = 1000;
            int sofar = 0;
           int chars_read;
           try
           {
              while((chars_read = from_server.read(buffer[sofar])) != -1)
              {
                 sofar = sofar + chars_read;
                 if (sofar >= expected) break;
              }
           }
           catch(IOException e)
           {
              to_user.println(e);
           }
    

    【讨论】:

    • 这里的关键是不要忽略read的返回值。 read(buf, off, len) 不保证它会读取 len 字节;它可以读得更少。无论哪种情况,它都会返回实际读取的字节数。这适用于从文件读取和从套接字读取。 Guava 的 ByteStreams.readFully(InputStream, byte[]) 的行为方式与 OP 期望 read 工作的方式相同,尽管 ByteStreams.toByteArray() 更简单。
    • 如何将其与现有代码相匹配。我无法理解。
    • @James:我用你的代码替换了我的代码。仍然面临同样的问题
    • @All:但每次我第二次做的时候都做对了。只有第一次有丢包。我想知道如何..
    • 这甚至不能编译。没有read(char) 方法。并且缓冲区应该是byte[],而不是char[].
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    相关资源
    最近更新 更多