Esempio n. 1
0
        public static void Unicast(string msg, ChatClientSocket chat, bool isServerMsg)
        {
            //throw new NotImplementedException();//지금 구현 부분 아니면 남겨둔다!!



            TcpClient tcp = chat.ChatClientSockets;

            if (tcp.Connected)
            {
                NetworkStream ns      = tcp.GetStream();
                byte[]        bytemsg = new byte[tcp.ReceiveBufferSize];
                if (!isServerMsg)                                                            //클라이언트가 보낸 메시지일때
                {
                    bytemsg = Encoding.UTF8.GetBytes(chat.ClientNickName + "님의 메시지:" + msg); //메시지를 바이트배열로 저장
                }
                else//서버가 보낸  메시지 일때
                {
                    string chattingRooms = "";
                    foreach (ChattingElement chatting in chattingList)
                    {
                        chattingRooms += chatting.RoomName;
                    }
                    bytemsg = Encoding.UTF8.GetBytes("서버 메시지:" + msg + " 현재 접속인원:" + GetMember());
                    // FTPServer.Logger.Text += "서버 메시지:" + msg + " 현재 접속인원:" + GetMember() + "\n";
                }
                ns.Write(bytemsg, 0, bytemsg.Length);
                ns.Flush();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 해당 채팅서버를 실행하고 접속을 대기시킴
        /// </summary>
        public void StartMessage()
        {
            var ipaddr = IPAddress.Parse(ipaddress);

            try
            {
                System.Net.Sockets.TcpListener serverListener = new System.Net.Sockets.TcpListener(ipaddr, 3333);
                serverListener.Start();
                FTPServer.Logger.Text += "채팅서버 가동>>>>\n";
                ChattingElement chattingAll = new ChattingElement();//전체 채팅방
                chattingAll.RoomName = "전체";

                chattingList.Add(chattingAll);//전체 사용자에게 전송되는 방,,,

                for (; ;)
                {
                    var    chatClientSocket = serverListener.AcceptTcpClient();//접속된 클라이언트 반환
                    string clientNickName   = null;
                    if (chatClientSocket.Connected)
                    {
                        var    ns       = chatClientSocket.GetStream();
                        Byte[] byteFrom = new Byte[chatClientSocket.SendBufferSize];
                        ns.Read(byteFrom, 0, chatClientSocket.SendBufferSize);
                        clientNickName = Encoding.UTF8.GetString(byteFrom);


                        int index = clientNickName.IndexOf("\0");
                        clientNickName         = clientNickName.Remove(index, clientNickName.Length - index);
                        FTPServer.Logger.Text += "채팅접속을 감지했습니다\n";
                        if (!clientList.Contains(clientNickName))
                        {
                            clientList.Add(clientNickName, chatClientSocket);//채팅참여자 관리
                            FTPServer.Logger.Text += "\n" + clientNickName + "님이 접속했습니다\n";
                            Broadcast(clientNickName + "님 접속했습니다", clientNickName, true);

                            //참여자 목록(clientList)을 클라이언트 접속한 클라이언트에 접속

                            string roomList = "";
                            int    count    = 0;
                            foreach (ChattingElement item in chattingList)
                            {
                                if (count != 0)
                                {
                                    roomList += "," + item.RoomName;
                                }
                                else
                                {
                                    roomList += item.RoomName;
                                }
                                count++;
                            }
                            string memberList = "";
                            count = 0;
                            foreach (DictionaryEntry v in clientList)
                            {
                                if (count != 0)
                                {
                                    memberList += "," + v.Key;
                                }
                                else
                                {
                                    memberList += v.Key;
                                }
                                count++;
                            }


                            Broadcast("접속 인원:" + memberList + "::", clientNickName, true);
                            Broadcast("방 목록:" + roomList + ";;", clientNickName, true);

                            ChatClientSocket client = new ChatClientSocket(chatClientSocket, clientNickName, ChatServer.clientList);
                        }
                        else
                        {
                            Unicast("해당 닉네임은 존재합니다", chatClientSocket);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }//서버가 대기하기 시작함
        }