java socket 통신 실현

1499 단어 학습 노트
문제 설명: 클라이언트가 서버에 메시지를 보내고 서버에서 클라이언트에게 데이터를 써야 합니다
서버 측
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server01 {
    public static void main(String[] args) throws IOException {
        // Socket
        ServerSocket serverSocket = new ServerSocket(8000);
        System.out.println(" , ");
        // , , Socket 
        Socket socket = serverSocket.accept();

        // 
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String str = br.readLine();
        System.out.println(" :");
        System.out.println(str);

        // 
        PrintStream ps=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
        ps.println("you are so cute I like you so much");
        ps.flush();
        ps.close();
        br.close();
    }
}

클라이언트
import java.io.*;
import java.net.Socket;

public class Client01 {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost",8000);
        PrintStream ps=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
        BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));

        ps.println("hello word!!");
        ps.flush();

        String info=br.readLine();
        System.out.println(info);
        ps.close();
        br.close();
    }
}

좋은 웹페이지 즐겨찾기