Java Socket close() methodLast Updated : 17 Mar 2025 The close() method of Java Socket class closes the specified socket. Once the socket has been closed, it is not available for further networking use. If the socket has a channel, the channel is also closed. SyntaxParameterNA ReturnNA Specified by
ThrowsIOException - if an I/O error occurs when closing this socket. Example 1Output: Inet address: localhost/127.0.0.1 Port number: 1085 After calling the close() method... Inet address: localhost/127.0.0.1 Port number: 1085 Example 2Output: Exception in thread "main" java.net.SocketException: Socket is closed at java.net.Socket.bind(Socket.java:625) at com.tpointtech.JavaSocketCloseExample2.main(JavaSocketCloseExample2.java:17) 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