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.
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).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With