1

私はこの問題にしばらく悩まされており、私の努力と友人たちのおかげでこの問題を乗り越えることはできません。

私の問題は、実際には非常に一般的なソケットを使用してクライアントとサーバー間の接続を確立しようとしていることですが、何らかの理由でクライアントがサーバーに接続できないようで、理由がわかりません。問題を解決するための私の試みは次のとおりです

1- http://portforward.com/を使用して、ルーターで使用されているタイプ "zone" のポートを開きました 2- ポートを複数回変更し、そのたびに PFPortChecker を使用してポートが開いているかどうかを確認しました

私のコードはかなり単純で、サーバーを開き、クライアントがそれに接続すると、サーバーは日付と時刻を送信します

私のサーバーコードは次のようになります

public class DateServer {

    /** Runs the server. */
    public static void main(String[] args) throws IOException {
        ServerSocket listener = new ServerSocket(6780);
        try {
            while (true) {
                Socket socket = listener.accept();
                try {
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    out.println(new Date().toString());
                } finally {
                    socket.close();
                }
            }
        } finally {
            listener.close();
        }
    }
}

私のクライアントコードは次のようになります

public class DateClient {

    /** Runs the client as an application. First it displays a dialog box asking for the IP address or hostname of a host running the date server, then connects to it and displays the date that it serves. */
    public static void main(String[] args) throws IOException {
        //I used my serverAddress is my external ip address 
        Socket s = new Socket(serverAddress, 6780);
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String answer = input.readLine();
        JOptionPane.showMessageDialog(null, answer);
        System.exit(0);
    }
}

挑戦を続けたのに

3-念のためファイアウォールを閉じました

4- サーバーソケットに接続タイムアウトを追加しました

すべての試行で、常にこのエラーが発生します

Exception in thread "main" 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 DateClient.main(DateClient.java:13)

DateClient.java:13 がこの行であることに注意してください。 Socket s = new Socket(serverAddress, 6780);

この問題で私を助けてください、事前に感謝します

4

2 に答える 2

1

あなたのコードを実行してみました。まず、localhost (127.0.0.1) で問題を解決できます。反対側では、ポートと IP を自分のものに変更しましたが、正常に動作します (外部 IP も)。したがって、ポート/IP に問題がある可能性があります。

  • localhost を使用して動作する場合は、IP が正しくないか、コンピューター上の何かが外部接続をブロックしています。クライアント コードは次のようになりますが、何らかの理由で機能しませんnew Socket(String host, int port)

    public class DateClient {
    
    /** Runs the client as an application. First it displays a dialog box asking for the IP address or hostname of a host running the date server, then connects to it and displays the date that it serves. */
    public static void main(String[] args) throws IOException {
        //I used my serverAddress is my external ip address 
        InetAddress serverAddress = InetAddress.getByName(String host);
        Socket s = new Socket(serverAddress, 6780);
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String answer = input.readLine();
        JOptionPane.showMessageDialog(null, answer);
        System.exit(0);
    }
    

    }

  • localhost を使用しても機能しない場合は、ポートが正しく転送されていません。ルーターにログインして、そこからポートを転送してみてください。

実際、@Audrius Meškauskas が言ったように、 ServerSockect を閉じる直前に、サーバー上の PrintWriter を閉じたい場合がありますlistener

于 2013-05-15T19:00:23.510 に答える