Java Socket setReceiveBufferSize() method

The setReceiveBufferSize() method of Java Socket class sets the SO_RCVBUF option to the given value for the specified socket. The buffer size should be greater than zero else, it will throw an IllegalArgumentException.

Syntax

Parameter

The parameter 'size' represents the size to which the receive buffer size is to be set.

Return

NA

Throws

IllegalArgumentException - if the size is 0 or less than zero.

SocketException - if an error is there in the underlying protocol, such as a TCP error.

Example 1

Test it Now

Output:

Buffer size: 789

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.IllegalArgumentException: invalid receive size
	at java.net.Socket.setReceiveBufferSize(Socket.java:1267)
	at com.javaTpoint.JavaSocketSetReceiveBufferSizeExample2.main(
	JavaSocketSetReceiveBufferSizeExample2.java:11)

Example 3

Test it Now

Output:

Exception in thread "main" java.net.SocketException: Socket is closed
	at java.net.Socket.setReceiveBufferSize(Socket.java:1270)
	at com.javaTpoint.JavaSocketSetReceiveBufferSizeExample3.main(
	JavaSocketSetReceiveBufferSizeExample3.java:12)




Latest Courses