Reputation: 543
I am currently trying to write a Java application to check if the server of game Archeage is online. I've got the following data: Archeage EU server IP: 193.105.173.130 Used ports: 80, 1237, 1239, 1250
I managed to check the ports using tool known as Tcping, but I want to write a Java application which will have some GUI and will play a sound or do something else when connection is established.
Currently I am stuck with the following code (which isn't working for some reason):
package Code;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.swing.JOptionPane;
public class SocketCheck {
public static void main(String[] args) throws IOException {
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 80:");
Socket s = new Socket(serverAddress, 80);
BufferedReader input =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
JOptionPane.showMessageDialog(null, answer);
s.close();
System.exit(0);
}
}
The error messages I get:
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Code.SocketCheck.main(SocketCheck.java:29)
What am I doing wrong?
Upvotes: 0
Views: 287
Reputation: 311039
Get rid of the readLine()
and the BufferedReader
and all that. An HTTP server isn't going to send you anything, it is waiting for you to send a request, and it will time you out eventually. No point in waiting for that, or in doing any I/O at all. The fact that you managed to establish a connection is sufficient proof that the server is up.
Upvotes: 2