ServerSocket, Socket

황상익·2024년 6월 24일

Socket 클래스는 client에서 서버로 접속하거나 Server에서 accept 하는데 필요한 클래스다. 이 설명을 이해 하려면 TCP/IP 접속 송수신 과정이해가 선행 되어야 한다.

public class CustomWebApplicationServer {

    private final int port;

    private static final Logger log = LoggerFactory.getLogger(CustomWebApplicationServer.class);

    public CustomWebApplicationServer(int port) {
        this.port = port;
    }

try (ServerSocket serverSocket = new ServerSocket(port)) {
            log.info("CustomWebApplication started {]", port);

            Socket clientSocket;
            log.info("CustomWebApplication waiting for client");

            while ((clientSocket = serverSocket.accept()) != null) {
                log.info("CustomWebApplication client connected");

try {
                    InputStream in = clientSocket.getInputStream();
                    OutputStream out = clientSocket.getOutputStream();
                    //InputStream -> reader로 변경하고 싶었음  (line by line)
                    BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
                    DataOutputStream dos = new DataOutputStream(out);
public class WasPracticeApplication {

	public static void main(String[] args) throws IOException {
		SpringApplication.run(WasPracticeApplication.class, args);
		new CustomWebApplicationServer(8081).start();
	}

}

출처: https://jink1982.tistory.com/182 [돼민이:티스토리]

profile
개발자를 향해 가는 중입니다~! 항상 겸손

0개의 댓글