Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4326264

java.net.Socket ignores buffers size settings for UPD

XMLWordPrintable

    • beta
    • sparc
    • solaris_2.6



      Name: dfC67450 Date: 03/30/2000



      Javadoc for java.net.Socket.setReceiveBufferSize(size) states:

           * For UDP, this sets the maximum size of a packet that may be sent on this
           * Socket.
           *
           * Because SO_RCVBUF is a hint, applications that want to
           * verify what size the buffers were set to should call getReceiveBufferSize().


      Socket ignores value set by setReceiveBufferSize call and may receive a packet
      of length greater than size returned by getReceiveBufferSize() returns. The
      problem may lie in the getReceiveBufferSize(). The same words are acceptable for
      [set/get]SendBufferSize methods.

      Here is the test demonstrating the bug:
      ---------------------------------------------
      import java.net.*;
      import java.io.*;


      public class Test {

          public static void main (String args[]){
             

              try {
                  DatagramSocket ds = new DatagramSocket(0);
                  int port = ds.getLocalPort();
                  InetAddress address = ds.getLocalAddress();

                  Socket soc = new Socket(address, port, false);
                  int sPort = soc.getLocalPort();

                  InputStream is = soc.getInputStream();
                  OutputStream os = soc.getOutputStream();
                  soc.setReceiveBufferSize(4);
                  soc.setSendBufferSize(3);
                  

                  byte data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

                  DatagramPacket p1 = new DatagramPacket(data, data.length, address, sPort);
                  DatagramPacket p2 = new DatagramPacket(data, data.length);

                  boolean passed = true;

                  ds.send(p1);
                  int receivedSize = is.available();
                  int rbs = soc.getReceiveBufferSize();
                  if (receivedSize != rbs) {
                      System.out.println("received packet lenght: " + receivedSize);
                      System.out.println("ReceivedBufferSize: " + rbs);
                      passed = false;
                  }
       

                  os.write(data);
                  ds.receive(p2);
                  int sentSize = p2.getLength();
                  int sbs = soc.getSendBufferSize();
                  if (sentSize != sbs) {
                      System.out.println("sent packet length: " + sentSize);
                      System.out.println("SendBufferSize: " + sbs);
                      passed = false;
                  }

                  if (passed)
                      System.out.println("Test passed");
                  else
                      System.out.println("Test failed");
              } catch (Exception e) {
                  System.out.println(" " + e);
              }
          }
      }


      ------------- output from the test ---------
      received packet lenght: 10
      ReceivedBufferSize: 4
      sent packet length: 10
      SendBufferSize: 3
      Test failed
      ---------------------------------------------

      ======================================================================

            michaelm Michael McMahon
            fdasunw Fda Fda (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: