Beispiel #1
0
        void SendPlayerNum()
        {// 클라이언트에게 플레이어 번호를 보내줌 - 서버에서 사용
            // 현재 클라이언트의 소켓을 가져온다.
            Socket socket = connected_clients[client_nickname];

            // 서버가 대기 중인지 확인한다.
            if (!socket.IsBound)
            {
                MsgBoxHelper.Warn("플레이어 번호 전송 : 서버가 실행되고 있지 않습니다!");
                return;
            }

            // 보낼 플레이어 번호
            int player_num = ++player_count;

            if (player_count > 4)   // -> 작동이 제대로 되는지 아직 모름.
            {
                MsgBoxHelper.Warn("방이 가득 찼습니다!");
                return;
            }

            // 문자열을 utf8 형식의 바이트로 변환한다.
            byte[] bDts = Encoding.UTF8.GetBytes(player_num.ToString());

            // 서버에 전송한다.
            socket.Send(bDts);
        }
Beispiel #2
0
        public void SendNickname()
        {// 서버에게 닉네임을 보내줌 - 클라이언트에서 사용
            // 서버가 대기 중인지 확인한다.
            if (!client_socket.IsBound)
            {
                MsgBoxHelper.Warn("닉네임 전송 : 서버가 실행되고 있지 않습니다!");
                return;
            }

            // 보낼 닉네임
            string nickname = my_nickname;

            //if (string.IsNullOrEmpty(nickname))
            //{
            //    MsgBoxHelper.Warn("닉네임을 입력하세요!");
            //    txt_nickname.Focus();
            //    return;
            //}

            // 문자열을 utf8 형식의 바이트로 변환한다.
            byte[] bDts = Encoding.UTF8.GetBytes(nickname);

            // 서버에 전송한다.
            client_socket.Send(bDts);
        }
Beispiel #3
0
        void Send()
        {
            if (is_server)  // 함수를 호출한 프로세스가 서버일 경우
            {
                // 서버가 대기 중인지 확인한다.
                if (!server_socket.IsBound)
                {
                    MsgBoxHelper.Warn("서버가 실행되고 있지 않습니다!");
                    return;
                }

                // 보낼 텍스트 지정
                string tts = txt_send.Text.Trim();

                // 텍스트박스 예외처리
                //if(string.IsNullOrEmpty(tts)) {
                //    //MsgBoxHelper.Warn("텍스트가 입력되지 않았습니다!");
                //    txt_send.Focus();
                //    return;
                //}

                // 보낼 텍스트가 비어 있지 않다면
                if (!string.IsNullOrEmpty(tts))
                {
                    // 서버의 닉네임과 메세지 및 플레이어 번호(1)를 전송한다.
                    // 문자열을 utf8 형식의 바이트로 변환한다. ('\x01'은 구분자.)
                    byte[] bDts = Encoding.UTF8.GetBytes(server_nickname + '\x01' + tts + '\x01' + "1");

                    // 연결된 모든 클라이언트에게 전송한다.
                    foreach (string nickname in connected_clients.Keys)
                    {
                        Socket socket = connected_clients[nickname];
                        try {
                            socket.Send(bDts);
                        }
                        catch {
                            // 오류 발생하면 전송 취소하고 리스트에서 삭제한다.
                            try {
                                socket.Dispose();
                            }
                            catch {
                            }
                            connected_clients.Remove(nickname);
                        }
                    }

                    // 전송 완료 후 텍스트 박스에 추가하고, 원래의 내용은 지운다.
                    AppendText(rtb_chat, string.Format("(1P){0}: {1}", server_nickname.ToString(), tts));
                    txt_send.Clear();

                    // 커서를 텍스트박스로
                    txt_send.Focus();
                }
            }
            else  // 함수를 호출한 프로세스가 클라이언트이면
            {
                // 서버가 대기 중인지 확인한다.
                if (!client_socket.IsBound)
                {
                    MsgBoxHelper.Warn("서버가 실행되고 있지 않습니다!");
                    return;
                }

                // 보낼 텍스트 지정
                string tts = txt_send.Text.Trim();

                // 텍스트박스가 비어 있는 경우
                //if(string.IsNullOrEmpty(tts)) {
                //    //MsgBoxHelper.Warn("텍스트가 입력되지 않았습니다!");
                //    txt_send.Focus();
                //    return;
                //}

                // 보낼 텍스트가 비어 있지 않으면
                if (!string.IsNullOrEmpty(tts))
                {
                    // 클라이언트의 닉네임과 메세지를 전송한다.
                    // 문자열을 utf8 형식의 바이트로 변환한다. ('\x01'은 구분자)
                    byte[] bDts = Encoding.UTF8.GetBytes(my_nickname + '\x01' + tts + '\x01' + my_player_num.ToString());

                    // 서버에 전송한다.
                    client_socket.Send(bDts);

                    // 전송 완료 후 텍스트 박스에 추가하고, 원래의 내용은 지운다.
                    AppendText(rtb_chat, string.Format("({0}P){1}: {2}", my_player_num, my_nickname, tts));
                    txt_send.Clear();

                    // 커서를 텍스트박스로
                    txt_send.Focus();
                }
            }
        }