Java Socket setTrafficClass() methodLast Updated : 17 Mar 2025 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. SyntaxParameterThe parameter 'tc' represents an integer value for the bitset. ReturnNA ThrowsSocketException - if an error is there in the setting of the traffic class or type-of-service. Example 1Output: Traffic class: 98 Example 2Output: Exception in thread "main" java.lang.IllegalArgumentException: tc is not in range 0 -- 255 at java.net.Socket.setTrafficClass(Socket.java:1377) at com.tpointtech.JavaSocketSetTrafficClassExample2.main(JavaSocketSetTrafficClassExample2.java:13) Example 3Output: Exception in thread "main" java.net.SocketException: Socket is closed at java.net.Socket.setTrafficClass(Socket.java:1380) at com.tpointtech.JavaSocketSetTrafficClassExample3.main(JavaSocketSetTrafficClassExample3.java:15) Next TopicJava Socket Class |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India