简体   繁体   English

Java IO,套接字服务器地址

[英]Java IO, Socket server address

Newbie in Java with most likely very trivial question: I have an code for Server: Java新手,最有可能是一个非常琐碎的问题:我有一个Server代码:

public class DateServer {
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        ServerSocket listener = new ServerSocket(1200);
        try {
            while (true) {
                Socket s1300 = listener.accept();
                try {
                    PrintWriter out = new PrintWriter(s1300.getOutputStream(), true);
                    out.println(new Date(0).toString());

                } catch (Exception e) {

                } finally {
                    s1300.close();
                }

                }
            } finally {
                listener.close();
            }

    }

}

and code for CLient: 和CLient的代码:

public class DateClient {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        Socket s = new Socket("local host", 1200);

        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));

        String answer = input.readLine();

        JOptionPane.showMessageDialog(null, answer);
        System.exit(0);

    }

}

and it doesnt work. 它不起作用。 Obviously there is a problem with Server address but thats what has been consearning me for some time now: how to get an address of an server when its located on same computer? 显然,服务器地址存在问题,但这就是我一段时间以来一直在想的问题:当服务器位于同一台计算机上时,如何获取服务器的地址? For example, i have few different server classes in the same package / how to get an address. 例如,在同一个程序包中/我有几个不同的服务器类/如何获取地址。

localhost is normally "localhost", not "local host". 本地主机通常是“本地主机”,而不是“本地主机”。

Failing that, try using the "home" address (127.0.0.1) 如果失败,请尝试使用“家庭”地址(127.0.0.1)

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

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