Skip to content

Commit 31fc335

Browse files
authored
Use socket isConnected() method rather than isBound() before con… (TooTallNate#964)
Use socket isConnected() method rather than isBound() before connect
2 parents f427b64 + cc030e3 commit 31fc335

3 files changed

Lines changed: 146 additions & 2 deletions

File tree

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public void run() {
457457
socket.setTcpNoDelay( isTcpNoDelay() );
458458
socket.setReuseAddress( isReuseAddr() );
459459

460-
if (!socket.isBound()) {
460+
if (!socket.isConnected()) {
461461
InetSocketAddress addr = new InetSocketAddress(dnsResolver.resolve(uri), this.getPort());
462462
socket.connect(addr, connectTimeout);
463463
}

src/test/java/org/java_websocket/issues/AllIssueTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@
4242
org.java_websocket.issues.Issue764Test.class,
4343
org.java_websocket.issues.Issue765Test.class,
4444
org.java_websocket.issues.Issue825Test.class,
45-
org.java_websocket.issues.Issue834Test.class
45+
org.java_websocket.issues.Issue834Test.class,
46+
org.java_websocket.issues.Issue962Test.class
4647
})
4748
/**
4849
* Start all tests for issues
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* Copyright (c) 2010-2019 Olivier Ayache
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
package org.java_websocket.issues;
26+
27+
import java.io.IOException;
28+
import java.net.InetAddress;
29+
import java.net.InetSocketAddress;
30+
import java.net.Socket;
31+
import java.net.URI;
32+
import java.net.URISyntaxException;
33+
import java.net.UnknownHostException;
34+
import javax.net.SocketFactory;
35+
import org.java_websocket.WebSocket;
36+
import org.java_websocket.client.WebSocketClient;
37+
import org.java_websocket.handshake.ClientHandshake;
38+
import org.java_websocket.handshake.ServerHandshake;
39+
import org.java_websocket.server.WebSocketServer;
40+
import org.java_websocket.util.SocketUtil;
41+
import org.junit.Assert;
42+
import org.junit.Test;
43+
44+
public class Issue962Test {
45+
46+
private static class TestSocketFactory extends SocketFactory {
47+
48+
private final SocketFactory socketFactory = SocketFactory.getDefault();
49+
private final String bindingAddress;
50+
51+
public TestSocketFactory(String bindingAddress) {
52+
this.bindingAddress = bindingAddress;
53+
}
54+
55+
@Override
56+
public Socket createSocket() throws IOException {
57+
Socket socket = socketFactory.createSocket();
58+
socket.bind(new InetSocketAddress(bindingAddress, 0));
59+
return socket;
60+
}
61+
62+
@Override
63+
public Socket createSocket(String string, int i) throws IOException, UnknownHostException {
64+
Socket socket = socketFactory.createSocket(string, i);
65+
socket.bind(new InetSocketAddress(bindingAddress, 0));
66+
return socket;
67+
}
68+
69+
@Override
70+
public Socket createSocket(String string, int i, InetAddress ia, int i1) throws IOException, UnknownHostException {
71+
throw new UnsupportedOperationException();
72+
}
73+
74+
@Override
75+
public Socket createSocket(InetAddress ia, int i) throws IOException {
76+
Socket socket = socketFactory.createSocket(ia, i);
77+
socket.bind(new InetSocketAddress(bindingAddress, 0));
78+
return socket;
79+
}
80+
81+
@Override
82+
public Socket createSocket(InetAddress ia, int i, InetAddress ia1, int i1) throws IOException {
83+
throw new UnsupportedOperationException();
84+
}
85+
86+
}
87+
88+
@Test
89+
public void testIssue() throws IOException, URISyntaxException, InterruptedException {
90+
int port = SocketUtil.getAvailablePort();
91+
WebSocketClient client = new WebSocketClient(new URI("ws://127.0.0.1:" + port)) {
92+
@Override
93+
public void onOpen(ServerHandshake handshakedata) {
94+
}
95+
96+
@Override
97+
public void onMessage(String message) {
98+
}
99+
100+
@Override
101+
public void onClose(int code, String reason, boolean remote) {
102+
}
103+
104+
@Override
105+
public void onError(Exception ex) {
106+
Assert.fail(ex.toString() + " sould not occur");
107+
}
108+
};
109+
110+
String bindingAddress = "127.0.0.1";
111+
112+
client.setSocketFactory(new TestSocketFactory(bindingAddress));
113+
114+
WebSocketServer server = new WebSocketServer(new InetSocketAddress(port)) {
115+
@Override
116+
public void onOpen(WebSocket conn, ClientHandshake handshake) {
117+
}
118+
119+
@Override
120+
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
121+
}
122+
123+
@Override
124+
public void onMessage(WebSocket conn, String message) {
125+
}
126+
127+
@Override
128+
public void onError(WebSocket conn, Exception ex) {
129+
}
130+
131+
@Override
132+
public void onStart() {
133+
}
134+
};
135+
136+
server.start();
137+
client.connectBlocking();
138+
Assert.assertEquals(bindingAddress, client.getSocket().getLocalAddress().getHostAddress());
139+
Assert.assertNotEquals(0, client.getSocket().getLocalPort());
140+
Assert.assertTrue(client.getSocket().isConnected());
141+
}
142+
143+
}

0 commit comments

Comments
 (0)