解决在java中连接客户端到服务器的问题

时间:2013-05-15 18:16:59

标签: java sockets ip-address

我已经有这个问题已经有一段时间了,尽管我的努力和我的朋友帮助我似乎无法解决这个问题。

我的问题是我试图在客户端和服务器之间建立连接使用其实很常见的套接字,但由于某种原因客户端似乎无法连接到服务器,不知道为什么,这是我的尝试解决问题

1-我使用http://portforward.com/打开路由器上使用的端口,类型为“zhone” 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);

请先帮助我解决这个问题

2 个答案:

答案 0 :(得分:1)

  • 关闭服务器端的PrintWriter
  • 请确保使用localhost(尝试127.0.0.1)作为服务器地址。根据您如何连接Internet,外部Internet地址(如各种&#34;获取我的IP&#34; Web上的工具所示)可能与实际地址不同,因为您的网络接口已配置且无法正常工作来自同一台机器。更多here

答案 1 :(得分: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无效,则无法正确转发您的端口。尝试登录路由器并从那里转发端口。

事实上,就像@AudriusMeškauskas所说,您可能希望在关闭ServerSockect listener之前关闭服务器上的PrintWriter。