java 네트워크 프로그래밍 클라이언트가 문자열을 입력하고 서버 측이 이를 반전시켜 클라이언트에게 출력하면 클라이언트가 표시합니다

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Test1_Client {

    /** * @param args * @throws IOException * @throws UnknownHostException */
    public static void main(String[] args) throws UnknownHostException, IOException {

        Socket socket = new Socket("127.0.0.1",6060);
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        PrintStream ps = new PrintStream(socket.getOutputStream());

        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        ps.println(line);
        System.out.println(br.readLine());
    }

}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Test1_Server {

    /** * @param args * @throws IOException */
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(6060);

        while(true){
            final Socket socket = server.accept();
            new Thread(){
                public void run() {
                    try {
                        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        PrintStream ps = new PrintStream(socket.getOutputStream());
                        String line = br.readLine();
                        line = new StringBuilder(line).reverse().toString();
                        ps.println(line);
                        socket.close();   
                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }

}

좋은 웹페이지 즐겨찾기