java - 无法解决 BufferOverflow 异常 - Java

标签 java networking udp bytebuffer

下面的发送者/接收者代码尝试将图像文件(49692字节)从客户端传输到服务器(使用UDP套接字)。尽管字节数组的大小完全等于字节缓冲区,但我还是抛出了异常(在行中提到)否,接收端 30)。有什么帮助吗?

发送方:

public class P_Sender
{
  public static DatagramSocket sock =null;
  public static DatagramPacket sendPacket,recvPacket=null;
  public static int port=15000;
  public static InetAddress ip;
  public static ByteBuffer fileBuffer;
  public static byte[] sendBytes;   

  public static byte[] int2Byte(int i) throws IOException
   {
     ByteArrayOutputStream baos=new ByteArrayOutputStream(Integer.SIZE/4);
     DataOutputStream dos=new DataOutputStream(baos);
     dos.writeInt(i);
     byte[] result=baos.toByteArray();
     dos.close();    
     return result;
   }

  public static void main(String[] args) throws Exception
    {
      FileChannel inChannel = new FileInputStream("abc.jpeg").getChannel();
      fileBuffer = ByteBuffer.allocate((int) inChannel.size());
      inChannel.read(fileBuffer);
      inChannel.close();
      fileBuffer.flip();
      ip=InetAddress.getByName("localhost");
      sock=new DatagramSocket();
      sendBytes=new byte[fileBuffer.capacity()];
      System.out.println("Length:" +fileBuffer.capacity());
      fileBuffer.get(sendBytes);
      sendPacket=new DatagramPacket(sendBytes,sendBytes.length,ip,port);
      sock.send(sendPacket);
      System.out.println("sent Packet Length:" +sendPacket.getLength());
    }

}

接收方:

 public class P_Recv 
{
    public static DatagramSocket sock;
    public static DatagramPacket recvDataPacket;
    public static byte[] recvBytes;
    public static int port;
    byte[] dataBuf;
    public static InetAddress ip;
    public static ByteBuffer fileBuffer;

    public static void main(String args[]) throws Exception
    {
      SrReceiver srobj=new SrReceiver();
      sock=new DatagramSocket(15000);
      recvBytes=new byte[49692];
      recvDataPacket=new DatagramPacket(recvBytes,recvBytes.length);
      sock.receive(recvDataPacket);
      System.out.println("Received Packet Length: "+recvDataPacket.getLength());
      fileBuffer= ByteBuffer.allocate(recvDataPacket.getLength());
      System.out.println(fileBuffer.capacity());
      fileBuffer.clear();
      fileBuffer.flip();
      fileBuffer.put(recvBytes);//Here i am thrown BufferOverflowException
      FileChannel outchannel = new FileOutputStream(new File("abcd.jpeg")).getChannel();
      outchannel.write(fileBuffer);
      outchannel.close();
      sock.close();
    }//main
}//class

最佳答案

flip()移至put()之后。它仅在 write() 或任何 get() 风格之前使用。它不在 read() 或任何 put() 风格之前使用。

关于java - 无法解决 BufferOverflow 异常 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41830212/

相关文章:

java - 如何在Spring框架中实现UDP

java - 将表连接添加到现有项目会导致无限递归

java - 根据 "best efforts 1PC"模式实现Spring ChainedTransactionManager

udp - 多播数据包丢失-运行同一应用程序的两个实例

linux - 如何在 Linux 上安装curl?

networking - 如何在 tcp 之上构建协议(protocol)?

java - 安卓广播地址

java - 读取 Java 属性文件而不转义值

java - 使用 Spring 和 AspectJ 在类上定位基于注释的方面

sockets - 当send()成功时,WSAWaitForMultipleEvents返回超时