简体   繁体   中英

Java IO, Socket server address

Newbie in Java with most likely very trivial question: I have an code for 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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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