Scanner sc = new Scanner(System.in);

 

소켓은 인스턴스 생성시 접속할 IP주소와 포트번호가 필요하다
소켓 생성시 서버가 올바르다면 접속이 완료된다

try(
        Socket s = new Socket("192.168.0.1", 9090);
) {
    try (
            PrintStream out = new PrintStream(s.getOutputStream());
            BufferedInputStream in = new BufferedInputStream(s.getInputStream());
    ) {
        System.out.println(new String(in.readAllBytes()));
        while (true) {
        out.println("> ");
        out.println(sc.nextLine());
        }
    }
} catch (UnknownHostException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

'JAVA' 카테고리의 다른 글

Thread  (0) 2024.07.08
PrintStream  (0) 2024.07.05
Buffered  (0) 2024.07.04
AutoClose  (0) 2024.07.01
File  (0) 2024.06.29

+ Recent posts