Skip to content

Commit a0c7123

Browse files
committed
added more flexibility for socket bind
1 parent dd08884 commit a0c7123

3 files changed

Lines changed: 46 additions & 24 deletions

File tree

‎example/AutobahnServerTest.java‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import java.io.IOException;
1+
import java.net.InetAddress;
2+
import java.net.InetSocketAddress;
3+
import java.net.UnknownHostException;
24

35
import net.tootallnate.websocket.Draft;
46
import net.tootallnate.websocket.WebSocket;
@@ -7,9 +9,13 @@
79

810
public class AutobahnServerTest extends WebSocketServer {
911
private static int counter = 0;
10-
11-
public AutobahnServerTest( int port , Draft d ) {
12-
super( port );
12+
13+
public AutobahnServerTest( int port, Draft d ) throws UnknownHostException {
14+
super( new InetSocketAddress( InetAddress.getLocalHost(), port ),d );
15+
}
16+
17+
public AutobahnServerTest( InetSocketAddress address, Draft d ) {
18+
super( address, d );
1319
}
1420

1521
@Override
@@ -49,7 +55,7 @@ public void onMessage( WebSocket conn, byte[] blob ) {
4955
}
5056
}
5157

52-
public static void main( String[] args ) {
58+
public static void main( String[] args ) throws UnknownHostException {
5359
WebSocket.DEBUG = true;
5460
int port;
5561
try {

‎example/ChatServer.java‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import java.io.BufferedReader;
22
import java.io.IOException;
33
import java.io.InputStreamReader;
4+
import java.net.InetAddress;
5+
import java.net.InetSocketAddress;
6+
import java.net.UnknownHostException;
47

58
import net.tootallnate.websocket.WebSocket;
69
import net.tootallnate.websocket.WebSocketServer;
@@ -10,8 +13,12 @@
1013
*/
1114
public class ChatServer extends WebSocketServer {
1215

13-
public ChatServer( int port ) {
14-
super( port );
16+
public ChatServer( int port ) throws UnknownHostException {
17+
super( new InetSocketAddress( InetAddress.getLocalHost(), port ) );
18+
}
19+
20+
public ChatServer( InetSocketAddress address ) {
21+
super( address );
1522
}
1623

1724
public void onClientOpen( WebSocket conn ) {

‎src/net/tootallnate/websocket/WebSocketServer.java‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package net.tootallnate.websocket;
22

33
import java.io.IOException;
4+
import java.net.InetAddress;
5+
import java.net.InetSocketAddress;
6+
import java.net.UnknownHostException;
47
import java.nio.channels.SelectionKey;
58
import java.nio.channels.Selector;
69
import java.nio.channels.ServerSocketChannel;
@@ -29,7 +32,7 @@ public abstract class WebSocketServer extends WebSocketAdapter implements Runnab
2932
* The port number that this WebSocket server should listen on. Default is
3033
* WebSocket.DEFAULT_PORT.
3134
*/
32-
private int port;
35+
private InetSocketAddress address;
3336
/**
3437
* The socket channel for this WebSocket server.
3538
*/
@@ -50,8 +53,8 @@ public abstract class WebSocketServer extends WebSocketAdapter implements Runnab
5053
* Nullary constructor. Creates a WebSocketServer that will attempt to
5154
* listen on port WebSocket.DEFAULT_PORT.
5255
*/
53-
public WebSocketServer() {
54-
this( WebSocket.DEFAULT_PORT, null );
56+
public WebSocketServer() throws UnknownHostException {
57+
this( new InetSocketAddress( InetAddress.getLocalHost(), WebSocket.DEFAULT_PORT ) , null );
5558
}
5659

5760
/**
@@ -61,8 +64,8 @@ public WebSocketServer() {
6164
* @param port
6265
* The port number this server should listen on.
6366
*/
64-
public WebSocketServer( int port ) {
65-
this( port, null );
67+
public WebSocketServer( InetSocketAddress address ) {
68+
this( address, null );
6669
}
6770

6871
/**
@@ -75,17 +78,18 @@ public WebSocketServer( int port ) {
7578
* The version of the WebSocket protocol that this server
7679
* instance should comply to.
7780
*/
78-
public WebSocketServer( int port , Draft draft ) {
81+
public WebSocketServer( InetSocketAddress address , Draft draft ) {
7982
this.connections = new CopyOnWriteArraySet<WebSocket>();
8083
this.draft = draft;
81-
setPort( port );
84+
setAddress( address );
8285
}
8386

8487
/**
8588
* Starts the server thread that binds to the currently set port number and
8689
* listeners for WebSocket connection requests.
90+
* @throws IllegalStateException
8791
*/
88-
public void start() throws IllegalStateException {
92+
public void start() {
8993
if( thread != null )
9094
throw new IllegalStateException( "Already started" );
9195
new Thread( this ).start();
@@ -181,20 +185,25 @@ public WebSocket[] connections() {
181185
* @param port
182186
* The port number to listen on.
183187
*/
184-
public void setPort( int port ) {
185-
this.port = port;
188+
public void setAddress( InetSocketAddress address ) {
189+
this.address = address;
186190
}
187191

192+
public InetSocketAddress getAddress() {
193+
return this.address;
194+
}
195+
188196
/**
189197
* Gets the port number that this server listens on.
190198
*
191199
* @return The port number.
192200
*/
193201
public int getPort() {
194-
return this.port;
202+
return getAddress().getPort();
195203
}
204+
196205

197-
public Draft getDraft() {
206+
public Draft getDraft(){
198207
return this.draft;
199208
}
200209

@@ -206,8 +215,8 @@ public void run() {
206215
try {
207216
server = ServerSocketChannel.open();
208217
server.configureBlocking( false );
209-
server.socket().bind( new java.net.InetSocketAddress( port ) );
210-
218+
server.socket().bind( address );
219+
//InetAddress.getLocalHost()
211220
selector = Selector.open();
212221
server.register( selector, server.validOps() );
213222
} catch ( IOException ex ) {
@@ -242,14 +251,14 @@ public void run() {
242251
// then the server is ready to read
243252
if( key.isReadable() ) {
244253
conn = (WebSocket) key.attachment();
245-
conn.handleRead( );
254+
conn.handleRead();
246255
}
247256

248257
// if isWritable == true
249258
// then we need to send the rest of the data to the client
250259
if( key.isValid() && key.isWritable() ) {
251260
conn = (WebSocket) key.attachment();
252-
conn.handleWrite( );
261+
conn.handleWrite();
253262
key.channel().register( selector, SelectionKey.OP_READ, conn );
254263
}
255264
}
@@ -262,7 +271,7 @@ public void run() {
262271
conn = it.next();
263272
if( conn.hasBufferedData() ) {
264273
conn.handleWrite();
265-
//key.channel().register( selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn );
274+
// key.channel().register( selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE, conn );
266275
}
267276
}
268277
} catch ( IOException ex ) {

0 commit comments

Comments
 (0)