@@ -58,8 +58,8 @@ public class WebSocketImpl implements WebSocket {
5858
5959 public SelectionKey key ;
6060
61- /* only used to obtain the socket addresses*/
62- public final Socket socket ;
61+ private final InetSocketAddress localSocketAddress ;
62+ private final InetSocketAddress remoteSocketAddress ;
6363 /** the possibly wrapped channel object whose selection is controlled by {@link #key} */
6464 public ByteChannel channel ;
6565 /**
@@ -122,13 +122,22 @@ public WebSocketImpl( WebSocketListener listener , List<Draft> drafts , Socket s
122122 * crates a websocket with client role
123123 */
124124 public WebSocketImpl ( WebSocketListener listener , Draft draft , Socket sock ) {
125+ if ( listener == null || sock == null || ( draft == null && role == Role .SERVER ) )
126+ throw new IllegalArgumentException ( "parameters must not be null" );
127+ if ( !sock .isBound () ) {
128+ throw new IllegalArgumentException ( "socket has to be bound" );
129+ }
125130 this .outQueue = new LinkedBlockingQueue <ByteBuffer >();
126131 inQueue = new LinkedBlockingQueue <ByteBuffer >();
127132 this .wsl = listener ;
128133 this .role = Role .CLIENT ;
129134 if ( draft != null )
130135 this .draft = draft .copyInstance ();
131- this .socket = sock ;
136+
137+ localSocketAddress = (InetSocketAddress ) sock .getLocalSocketAddress ();
138+ remoteSocketAddress = (InetSocketAddress ) sock .getRemoteSocketAddress ();
139+ assert ( localSocketAddress != null );
140+ assert ( remoteSocketAddress != null );
132141 }
133142
134143 /**
@@ -682,12 +691,12 @@ public String toString() {
682691
683692 @ Override
684693 public InetSocketAddress getRemoteSocketAddress () {
685- return ( InetSocketAddress ) socket . getRemoteSocketAddress () ;
694+ return remoteSocketAddress ;
686695 }
687696
688697 @ Override
689698 public InetSocketAddress getLocalSocketAddress () {
690- return ( InetSocketAddress ) socket . getLocalSocketAddress () ;
699+ return localSocketAddress ;
691700 }
692701
693702 @ Override
0 commit comments