메뉴 건너뛰기

조회 수 119 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

■ TCP 소켓 프로그래밍 구현 과정


 

 

1. Server측에서는 ServerSocket을 생성하고 accept() 메서드를 호출함으로써 Client의 접속을 대기합니다.

 

2. Client측에서는 Server에 접속을 함으로써 Server와의 통신을 위한 Socket을 생성합니다.

 

3. 마찬가지로 Server측에서 Client 접속이 이루어지면 해당 Client와 통신 할 수 있는 Socket을 반환받습니다.

 

4. Clinet와 Server는 생성된 소켓을 통하여 각각 상대에게 데이터를 내보내기 위한 출력 스트림과 데이터를 읽어 들이기 위한 입력 스트림을 생성합니다.

 

5. 생성된 스트림을 통하여 Server/Client 서로간에 데이터를 송수신합니다.

 

6. 통신이 끝나면 Client와 Server측에서 각각 socket.close()를 해줌으로써 통신을 종료하게 됩니다.

■ 구현

Server측 

public class Server {

    
    public static void main(String arg[])
    {
        Socket socket = null;                //Client와 통신하기 위한 Socket
        ServerSocket server_socket = null;  //서버 생성을 위한 ServerSocket 
        BufferedReader in = null;            //Client로부터 데이터를 읽어들이기 위한 입력스트림
        PrintWriter out = null;                //Client로 데이터를 내보내기 위한 출력 스트림
        
        try{
            server_socket = new ServerSocket(특정 포트 입력);
            
        }catch(IOException e)
        {
            System.out.println("해당 포트가 열려있습니다.");
        }
        try {
            
            System.out.println("서버 오픈!!");
            socket = server_socket.accept();    //서버 생성 , Client 접속 대기
            
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));    //입력스트림 생성
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()))); //출력스트림 생성
            
            String str = null;
            str = in.readLine();                //Client로부터 데이터를 읽어옴

            System.out.println("Client로 부터 온 메세지 : " + str);
            
            out.write(str);
            out.flush();
            socket.close();
        }catch(IOException e){}
    }
}

Client측

public class Client {
    public static void main(String[] arg)
    {
        Socket socket = null;            //Server와 통신하기 위한 Socket
        BufferedReader in = null;        //Server로부터 데이터를 읽어들이기 위한 입력스트림
        BufferedReader in2 = null;        //키보드로부터 읽어들이기 위한 입력스트림
        PrintWriter out = null;            //서버로 내보내기 위한 출력 스트림
        InetAddress ia = null;
        try {
            ia = InetAddress.getByName("서버 주소 입력");    //서버로 접속
            socket = new Socket(ia,서버가 열어놓은 포트 입력);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            in2 = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())));
            
            System.out.println(socket.toString());
        }catch(IOException e) {}
        
        try {
            System.out.print("서버로 보낼 메세제 : ");
            String data = in2.readLine();            //키보드로부터 입력
            out.println(data);                        //서버로 데이터 전송
            out.flush();

            String str2 = in.readLine();            //서버로부터 되돌아오는 데이터 읽어들임
            System.out.println("서버로부터 되돌아온 메세지 : " + str2);
            socket.close();
            
        }catch(IOException e) {}
    }
}

 

 

 


List of Articles
번호 제목 날짜 조회 수
131 회원가입 폼(form)으로 보는 Validator 구현하는 방법 file 2016.08.29 7108
130 Select statements cannot be executed as an update. 에러 해결방법 file 2016.08.29 3853
129 Database Connections 생성하기 (오라클) file 2016.08.29 4072
128 Database Connections 생성하기 (Mysql) file 2016.08.29 3365
127 전자정부 표준프레임워크 설치하기 file 2016.08.29 4250
126 간단한 개인홈페이지 만들어보기 file 2016.08.29 4751
125 war로 묶지 않아도 컴파일된 소스 위치 확인하기 file 2016.08.29 4116
124 MySQL에 All-in-one 설치시 webmaster로 로그인 안되는 문제 해결을 위한 2가지 수정사항 file 2016.08.29 4954
123 전자정부표준프레임워크 - 설치 file 2016.09.02 6303
122 Spring Boot 프로젝트 생성 file 2016.09.02 4160
121 전자정부프레임워크 v2.5, v2.6 오라클 세팅하기 file 2016.09.12 5109
120 JSP, Spring, GMail 메일발송 간단 예제 2016.09.12 32791
119 변수의 종류 2016.09.13 3980
118 클래스멤버와 인스턴스멤버간의 참조와 호출 2016.09.13 3346
117 클래스 메서드와 인스턴스 메서드 2016.09.13 3720
116 static 2016.09.13 3354
115 컬렉션 프레임워크 file 2016.09.13 3705
114 제네릭 file 2016.09.13 3297
113 파일I/O 개요 file 2016.09.13 3507
112 바이트 기반의 스트림 file 2016.09.13 3763
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved