Java Socket setTrafficClass() method

The setTrafficClass() method of Java Socket class sets the traffic class or type-of-service in the IP header for packets sent from the specified socket. The parameter (tc) should be in the range from 0 to 255(0<=tc<=255) else an IllegalArgumentException will be thrown.

Syntax

Parameter

The parameter 'tc' represents an integer value for the bitset.

Return

NA

Throws

SocketException - if an error is there in the setting of the traffic class or type-of-service.

Example 1

Test it Now

Output:

Traffic class: 98

Example 2

Test it Now

Output:

Exception in thread "main" java.lang.IllegalArgumentException: tc is not in range 0 -- 255
	at java.net.Socket.setTrafficClass(Socket.java:1377)
	at com.javaTpoint.JavaSocketSetTrafficClassExample2.main(JavaSocketSetTrafficClassExample2.java:13)

Example 3

Test it Now

Output:

Exception in thread "main" java.net.SocketException: Socket is closed
	at java.net.Socket.setTrafficClass(Socket.java:1380)
	at com.javaTpoint.JavaSocketSetTrafficClassExample3.main(JavaSocketSetTrafficClassExample3.java:15)




Latest Courses