메뉴 건너뛰기

조회 수 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
번호 제목 날짜 조회 수
31 전자정부프레임워크 v2.5, v2.6 오라클 세팅하기 file 2016.09.12 5109
30 Spring Boot 프로젝트 생성 file 2016.09.02 4160
29 전자정부표준프레임워크 - 설치 file 2016.09.02 6303
28 MySQL에 All-in-one 설치시 webmaster로 로그인 안되는 문제 해결을 위한 2가지 수정사항 file 2016.08.29 4954
27 war로 묶지 않아도 컴파일된 소스 위치 확인하기 file 2016.08.29 4116
26 간단한 개인홈페이지 만들어보기 file 2016.08.29 4751
25 전자정부 표준프레임워크 설치하기 file 2016.08.29 4250
24 Database Connections 생성하기 (Mysql) file 2016.08.29 3365
23 Database Connections 생성하기 (오라클) file 2016.08.29 4072
22 Select statements cannot be executed as an update. 에러 해결방법 file 2016.08.29 3853
21 회원가입 폼(form)으로 보는 Validator 구현하는 방법 file 2016.08.29 7108
20 스프링(Spring)의 Validator 기능을 사용하다가 '정의되지 않음 또는 null 참조인 'type' 속성을 가져올 수 없습니다.'라는 오류를 만났을 때 해결방법 file 2016.08.29 5166
19 배치관리 컴포넌트 생성 후 에러 날 때 해결방법 file 2016.08.29 4269
18 새로 만든 모듈의 iBatis 쿼리를 새로운 xml 파일에 만들고 싶다면 이렇게 하자. file 2016.08.29 3773
17 새로 만든 모듈의 iBatis 쿼리를 새로운 xml 파일에 만들고 싶다면 이렇게 하자. file 2016.08.29 3664
16 No bean name '***Service' is defined 오류나는 이유 및 해결방법 file 2016.08.29 4561
15 회원가입 양식에서 (필수/옵션)필드 추가/제거하기 file 2016.08.29 4471
14 회원가입 양식에서 (필수/옵션)필드 추가/제거하기 file 2016.08.29 5334
13 기존 프로젝트, Table, View 모두 다 삭제하기 file 2016.08.29 4608
12 로그인 컴포넌트 설치시 뷰 생성 에러 해결방법 (ORA-01031: 권한이 불충분합니다) file 2016.08.29 3874
Board Pagination Prev 1 2 3 4 5 6 7 8 Next
/ 8

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved