Reputation: 44448
I've created a chat program which required the user to select either the client or server role. My approach at removing this requirement is to have every user start their own server where they'll get messages from. This should allow me to have two clients talk to the the other without having to put a server in between them.
Right now I've modified my program in such a way that the client side does the sending and the server does the receiving.
Note that communication between the two programs worked perfectly fine up untill these changes. However, now that I've changed some stuff an error occurs as early as when I create a socket.
The flow of my program untill the problem is as follows:
After some debugging I've found that this socket is never created.
When the flow enters this stage (last item in the list), only the 'First test' gets executed.
public void run() {
System.out.println("First test");
createConnection();
System.out.println("Second test");
initiateIO();
}
private void createConnection() {
try {
socket = new Socket(host, port);
} catch (UnknownHostException e) {
OutputUtil.showErrorMessage("Couldn't bind socket to unknown host", "Unknown host");
} catch (IOException e) {
OutputUtil.showErrorMessage("General IO error at creating client socket", "IO error");
}
}
private void initiateIO() {
try {
outbound = new PrintWriter(socket.getOutputStream(), true);
} catch (IOException e) {
OutputUtil.showErrorMessage("Couldn't load IO streams from client", "IO Error");
}
}
Output:
Console: First test
Popup: General IO error at creating client socket
Console: Second test
Console: NPE at `outbound.close()`
I'm assuming the NPE is a result of the first error, considering a method from the socket is invoked when creating the PrintWriter
. It should also be noted that it takes around 10 seconds to show the first error.
At first I thought the error might be introduced because both the local server and the connection with the other client use port 6666, but after creating a link on port 6667 the problem still occurred. Which makes sense upon review.
When my debugger points at the line where outbound
is initialized (after the "second test" message, socket has value null
.
My question is: why can't the socket be created? The documentation only specifies
IOException - if an I/O error occurs when creating the socket.
which isn't of much use.
Full source code can be found here for a better overview.
Edit: Printed the stacktrace from the first, main error.
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at core.Client.createConnection(Client.java:30)
at core.Client.run(Client.java:64)
at java.lang.Thread.run(Unknown Source)
Upvotes: 0
Views: 14266
Reputation: 311050
This is a firewall problem. The target port you specified wasn't open in the target's firewall.
The server may also not have been running, but if that was the only problem it would have been 'connection refused', not 'connection timed out: connect'.
Upvotes: 1