'통신프로토컬'에 해당되는 글 1건

  1. 2007.09.19 가이스터 통신 프로토콜 by UrC9 2

서버 실행 (7000번 포트 이용)

 final int portNum = 7000;
 
 public testServer() {
  try {
   m_sckServer = new ServerSocket(portNum);
   m_vctClients = new Vector();
   System.out.println(portNum + "번 소켓 생성");
   this.start();
  }
  catch(IOException e) {
   System.out.println("서버소켓생성 에러 : " + e);
  }
 }

7000번 소켓 생성

사용자 삽입 이미지

----------------------------------------------------------------------------------

클라이언트 2개가 접속하면 게임을 진행하고

   System.out.println("연결시도\n");
   socket = new Socket(ip, 7000);
   System.out.println("연결완료\n");


각 클라이언트가 "ready"를 받으면 랜덤함수로 선을 정하고

      ready = true;
      sendMsg("ready");
      System.out.println("ready : " + ready);


사용자 삽입 이미지

----------------------------------------------------------------------------------

    Socket socket = m_sckServer.accept();
    clientSocket client = new clientSocket(socket, this, m_vctClients.size());
    m_vctClients.addElement(client);
   
    System.out.println("연결 소켓수 : " + m_vctClients.size());
    if(m_vctClients.size() == 2) {
//     send("게임시작", 0);
//     send("게임시작", 1);
     
     clientSocket temp = null;
     temp = (clientSocket)m_vctClients.elementAt(0);
     System.out.println("0번 : " + temp.clientReady());
     temp.start();
     temp = (clientSocket)m_vctClients.elementAt(1);
     System.out.println("1번 : " + temp.clientReady());
     temp.start();
     
     Random random=new Random();
     int first;
     first=random.nextInt(2);
     System.out.println("선공 : " + first);
     send("01", first);
     send("00", ((first+1) % 2));
    }



선공에는 01, 후공에는 00 을 보낸다.

사용자 삽입 이미지

----------------------------------------------------------------------------------

메시지의 첫자리는 메시지의 종류를 나타맨
0 : 게임 개시와 선을 알림
1 : 상대가 메시지를 보내옴(상대 가이스터의 위치). 이걸 받음으로써 자신의 차례임을 암

종류가 0일때
둘째자리가 1이면 선공, 0이면 후공
종류가 1일때
둘째자리부터 두자리씩 파란 가이스터 4개의 자리번호와 빨간 가이스터 4개의 자리번호가 차례로 옴 (메시지를 받은 측에서는 상대가 자신 중심으로 자리 번호를 보내기 때문에 이 번호에서 35를 빼서 내 중심으로 변형해야 한다.)
사용자 삽입 이미지

 - 그림에서는 상대의 자리를 받고(파란 가이스터 10,9,14,2 빨간 가이스터 3,4,1,7 - 메시지를 순차적으로 읽은것이 아님)나서 내가 플레이 후에는 메시지 종류는 1로 하고 나의 자리(파란 가이스터 19,32,26,27 빨간 가이스터 33,34,28,31)을 보낸다.

Posted by UrC9
,