0

Below sender/receiver code attempts to transfer the image file (49692 bytes) from client to server (Uses UDP socket).Although the size of the bytearray is exactly equal to the bytebuffer, i was thrown the exception (mentioned in line no,30 in receiver side). Any help?

Sender Side:

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());
    }

}

Receiver Side:

 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
12
  • Integer.SIZE/4? Commented Jan 24, 2017 at 14:12
  • Are you sure the recvDataPacket is that size? You don't check it and use that to allocate the buffer, then write more. Commented Jan 24, 2017 at 14:13
  • I checked it in the sender's side and hence hard coded it. i am pretty sure that 49692 has been transferred to receiver's side (I could print that as well). Commented Jan 24, 2017 at 14:15
  • Try using rewind or position(0).I am not sure flip is doing what you think is doing. If it works let me know. I will put as answer Commented Jan 24, 2017 at 14:19
  • 1
    flip also sets the limit which is the current position Commented Jan 24, 2017 at 14:21

1 Answer 1

1

Move the flip() to after the put(). It is only used before write() or any flavour of get(). It is not used before read() or any flavour of put().

Sign up to request clarification or add additional context in comments.

2 Comments

You can also get rid of the clear(). A brand new buffer is already clear.
Not unless you put it back before the write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.