title: Unable to set the offset for buffer in the datagramPacket object
body: <p>In the java library, <a href="http://download.java.net/jdk7/archive/b123/docs/api/java/net/DatagramPacket.html" rel="nofollow">http://download.java.net/jdk7/archive/b123/docs/api/java/net/DatagramPacket.html</a></p>
  
  <p>I want to construct my datagram packet object using this constructor;</p>
  
  <pre><code>DatagramPacket(byte[] buf, int offset, int length, InetAddress address, int port)
  </code></pre>
  
  <p>Here is my code: </p>
  
  <pre><code>      ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
        outputStream.write( by); // by is a buffer that contains filename
        outputStream.write(buffer); // buffer contains the actual data for the packet
        byte combined[] = outputStream.toByteArray( );
        System.out.println("combined length is "+combined.length);
        sndPkt = new DatagramPacket(combined,by.length, combined.length, ip, portNum);
  
  
        sendsocket.send(sndPkt);
        Thread.sleep(1);
  </code></pre>
  
  <p>"by" is a buffer that contains filename. "buffer" contains the file data. I want to combine them together and send in one packet. However, when I create the DatagramPacket object with the above mentioned constructor. I got this error message: </p>
  
  <pre><code>   java.lang.IllegalArgumentException: illegal length or offset
  </code></pre>
  
  <p>When I change it to zero, no error is reported, but I need to set the offset value same as my filename length so that I am able to get it at the  server side. Can anyone tell me where the problem is? I am able to send the filename over to the server by sending it as the first packet, but now  I want to embed the filename in every packet I send. Is this the standard way of doing this?</p>