简体   繁体   English

用Java“ ping”服务器

[英]“Pinging” a server in Java

I am currently trying to write a Java application to check if the server of game Archeage is online. 我目前正在尝试编写Java应用程序,以检查游戏Archeage的服务器是否在线。 I've got the following data: Archeage EU server IP: 193.105.173.130 Used ports: 80, 1237, 1239, 1250 我有以下数据:Archeage EU服务器IP:193.105.173.130使用的端口: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. 我设法使用称为Tcping的工具检查端口,但是我想编写一个Java应用程序,该应用程序将具有一些GUI,并在建立连接后会播放声音或执行其他操作。

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? 我究竟做错了什么?

Get rid of the readLine() and the BufferedReader and all that. 摆脱readLine()BufferedReader以及所有这些。 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. HTTP服务器不会向您发送任何信息,它正在等待您发送请求,最终将使您超时。 No point in waiting for that, or in doing any I/O at all. 等待没有任何意义,或者根本没有做任何I / O。 The fact that you managed to establish a connection is sufficient proof that the server is up. 您设法建立连接的事实足以证明服务器已启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM