Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ServerSocket.setSocketFactory static?

I'm trying to use a custom SocketImpl with SSL in Java. Because of that I need to set ServerSocket's socket factory. I now noticed that it's static, which creates some hassle for me as a I want to supply it with some paramaters that differs between in each ServerSocket-instance. Does anyone know the rationale behind making it static? To me it feels like an unnecessary constraint that only introduces more global state in your app.

Update There seems to be some confusion on why this is a hassle. The problem this creates is that it forces me to use the same factory across the entire application. What if I want to use the default SocketImpl in one place and a custom one in another? That can't be done without resorting to some ugly reflection-hacks, because the factory can't be changed once it has been set. I also can't make my factory create default implementation because SocksSocketImpl is package private.

like image 463
Yrlec Avatar asked Aug 16 '12 07:08

Yrlec


People also ask

What is a ServerSocket and how is it used?

ServerSocket is a java.net class that provides a system-independent implementation of the server side of a client/server socket connection. The constructor for ServerSocket throws an exception if it can't listen on the specified port (for example, the port is already being used).

Why public socket accept () is used?

Explanation: The Public socket accept () method is used by the ServerSocket class to accept the connection request of exactly one client at a time. The client requests by initializing the socket object with the servers IP address.

What is the difference between ServerSocket and socket?

The Key Difference between Socket and ServerSocket Class is that Socket is used at the client side whereas ServerSocket is used at the server side.


1 Answers

SocketImpl is an abstract class that provides an interface for customizing socket implementations. Subclasses of SocketImpl are not intended to be explicitly instantiated. Instead, they are created by a SocketImplFactory. The motivation for this design is to allow you to change the default type of socket used by an entire application by calling Socket.setSocketImplFactory(), making it unnecessary to rewrite all of your networking code. Custom sockets are necessary for features such as SSL and firewall tunneling. Unfortunately, the global nature of setSocketImplFactory() is rather limiting, as it doesn't allow you to use multiple types of sockets within a single application. You can get around this by subclassing Socket and using the protected Socket(SocketImpl) constructor that was introduced in JDK 1.1.

Reference:http://www.devx.com/tips/Tip/26363

like image 119
Sreenath S Avatar answered Sep 22 '22 03:09

Sreenath S