Name: skT45625 Date: 07/24/2000
java version "1.3.0"
Java(TM) 2 RuntimeEnvironment,Standard Edition(build 1.3.0-c)
Java HotSpot(TM) Client VN(build 1.3.0-c,mixed mode)
Also found in JDK1.2.1,JDK1.1.5.
1. According to the specifications in java.net.Socket setReceiveBufferSize(int
size) must throw an IllegalArgumentException when the size is set to 0 or less
then 0.
2. But,setReceiveBufferSize(int size) accepts 0 and does not throw an
IllegalArgumentException as expected.
3. It proceeds,and invokes 'impl.setOption(SocketOptions.SO_RCVBUF, new
Integer(size));'(from source code) where the size is checked and
'java.net.SocketException: bad parameter for SO_SNDBUF or SO_RCVBUF' is reported
instead of 'java.lang.IllegalArgumentException: negative send size'
4. So the problem faced here is once the user tries to catch
IllegalArgumentException,by passing size value as 0, for
setReceiveBufferSize(int size),he cannot.
5. Also,the setReceiveBufferSize(int size) unnecessarily proceeds and there it
throws java.net.SocketException which should be thrown already as
java.lang.IllegalArgumentException
6. But,the setSendBufferSize(int size) in Socket works correctly and throws the
wanted java.lang.IllegalArgumentException when size is set to 0.
7. The sample source code is,
// The Server side code follows...
// Here setSendBufferSize(int size) is set to 0 and the exception is caught.
import java.net.*;
public class Server
{
public static void main(String args[])
{
ServerSocket ss = null;
Socket s = null;
try
{
ss = new ServerSocket(4000);
System.out.println("Waiting for client connection...");
s = ss.accept();
System.out.println("Server is connected with client");
System.out.println("Going to change sendBufferSize...");
s.setSendBufferSize(0);
System.out.println("sendBufferSize is changed
successfully");
}
catch(IllegalArgumentException e1)
{
System.out.println("IllegalArgumentException is : "+e1);
}
catch(Exception e2)
{
System.out.println("Exception is : "+e2);
}
}
}//Server code ends here.
The output for the above code will be as,
Waiting for client connection...
Server is connected with client
Going to change sendBufferSize...
IllegalArgumentException is : java.lang.IllegalArgumentException: negative send
size
//The Client side code follows here...
//Here setReceiveBufferSize(int size) is set to 0 and the Exception is caught.
import java.net.*;
public class Client
{
public static void main(String args[])
{
Socket s = null;
try
{
s = new Socket("localhost",4000);
System.out.println("Client is connected with server");
System.out.println("Going to change
receiveBufferSize...");
s.setReceiveBufferSize(Integer.parseInt(args[0]));
System.out.println("receiveBufferSize is changed
successfully");
}
catch(IllegalArgumentException e1)
{
System.out.println("IllegalArgumentException is : "+e1);
}
catch(Exception e2)
{
System.out.println("Exception is : "+e2);
}
}
}//The Client side code ends here.
The output for the above code will be as,
Client is connected with server
Going to change receiveBufferSize...
Exception is : java.net.SocketException: bad parameter for SO_SNDBUF or
SO_RCVBUF
8. Thus the two outputs reveals the problem.
9. I have given here a simple workaround for this problem.Please check and use
it.
10. Iam looking for your early reply regarding this problem.
Thank you.
Yours sincerely,
A.Sankar.
(Review ID: 107516)
======================================================================
- duplicates
-
JDK-4326250 java.net.Socket.setReceiveBufferSize(size) does not throw IAE with 0
- Resolved