How do I pass the IP address typed in the command-line to the client socket constructor in Java?

StackOverflow https://stackoverflow.com/questions/22548933

  •  18-06-2023
  •  | 
  •  

문제

I do not know how to read the IP address and use it when I am initializing a socket.

How do I do that?

Here is my socket code on the client side, if needed:

 Socket socket;
 socket = new Socket("localhost", 16789);
도움이 되었습니까?

해결책

public class Echo {
    public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
            Socket socket;
            socket = new Socket(s, 16789);
        }
    }
}

Note that this code would parse multiple command line arguments. If you only have one you can use args[0] and get rid of the for loop.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top