コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: Saroko-dnd/My_DZ
        public void RunConnectionWithClient(Socket SocketForClient)
        {
            try
            {
                BoolClass CloseCurrentSocketsGate = new BoolClass();
                StringBuilder BuilderForTextBox = new StringBuilder();
                byte[] BufferForMessage = new byte[500];
                string ClientListenSocket = string.Empty;
                SocketForClient.Receive(BufferForMessage);
                ClientListenSocket = Encoding.UTF8.GetString(BufferForMessage).TrimEnd('\0');
                string CurrentClientName = ClientListenSocket.Split(CharSeparator)[2];
                Socket SocketForMessaging = null;

                lock (AllConnectedSockets)
                {
                    if (!ServerShutDown)
                    {
                        SocketForMessaging = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        AllConnectedSockets.Add(SocketForMessaging);
                        lock (AllSendingSockets)
                        {
                            AllSendingSockets.Add(SocketForMessaging);
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                IPEndPoint EndPointForUDPClient = new IPEndPoint(IPAddress.Parse(ClientListenSocket.Split(CharSeparator)[0]),
                    Int32.Parse(ClientListenSocket.Split(CharSeparator)[3]));
                lock (ListOfIPEndPointsForTimeSending)
                {
                    ListOfIPEndPointsForTimeSending.Add(EndPointForUDPClient);
                }

                ThreadPool.QueueUserWorkItem(o => SendMessagesToClient(CloseCurrentSocketsGate, SocketForMessaging, new IPEndPoint(IPAddress.Parse(ClientListenSocket.Split(CharSeparator)[0]),
                    Int32.Parse(ClientListenSocket.Split(CharSeparator)[1])), ClientListenSocket.Split(CharSeparator)[2]));
                BuilderForTextBox.AppendLine(MyResourses.Texts.ConnectionFound);
                BuilderForTextBox.AppendLine(SocketForClient.RemoteEndPoint.ToString());
                lock (MainStringBuilder)
                {
                    ++StringCounter;
                    MainStringBuilder.AppendLine(BuilderForTextBox.ToString());
                    Application.Current.Dispatcher.Invoke(new Action(() => ConsoleTextBox.Text = MainStringBuilder.ToString()));
                }
                while (true)
                {
                    byte[] BufferForMessage_2 = new byte[500];
                    BuilderForTextBox.Clear();
                    SocketForClient.Receive(BufferForMessage_2);
                    string MessageText = Encoding.UTF8.GetString(BufferForMessage_2).TrimEnd('\0');
                    if (MessageText == MyResourses.Texts.ClientOff)
                    {
                        lock (ListOfIPEndPointsForTimeSending)
                        {
                            ListOfIPEndPointsForTimeSending.Remove(EndPointForUDPClient);
                        }
                        lock (CloseCurrentSocketsGate)
                        {
                            CloseCurrentSocketsGate.SocketClosed = true;
                            SocketForClient.Close();
                            SocketForMessaging.Close();
                        }
                        lock (AllConnectedSockets)
                        {
                            AllConnectedSockets.Remove(SocketForMessaging);
                            AllConnectedSockets.Remove(SocketForClient);
                        }
                        lock (AllSendingSockets)
                        {
                            AllSendingSockets.Remove(SocketForMessaging);
                        }
                        BuilderForTextBox.AppendLine(MyResourses.Texts.ClientOffServerLogMessage);
                        BuilderForTextBox.Append(" " + CurrentClientName);
                        lock (MainStringBuilder)
                        {
                            ++StringCounter;
                            MainStringBuilder.AppendLine(BuilderForTextBox.ToString());
                            Application.Current.Dispatcher.Invoke(new Action(() => ConsoleTextBox.Text = MainStringBuilder.ToString()));
                        }
                        return;
                    }
                    else if (MessageText == ClientListenSocket)
                    {
                        //do nothing, wait for proper message
                    }
                    else
                    {
                        if (MessageText.Split(CharSeparator).Length > 1)
                        {
                            BuilderForTextBox.AppendLine(MyResourses.Texts.ServerGetMessage);
                            BuilderForTextBox.AppendLine(CurrentClientName);
                            lock (AllClientsMessages)
                            {
                                AllClientsMessages.Add(new ClientMessage(MessageText.Split(CharSeparator)[0], CurrentClientName, MessageText.Split(CharSeparator)[1]));
                            }
                            lock (MainStringBuilder)
                            {
                                ++StringCounter;
                                MainStringBuilder.AppendLine(BuilderForTextBox.ToString());
                                Application.Current.Dispatcher.Invoke(new Action(() => ConsoleTextBox.Text = MainStringBuilder.ToString()));
                            }
                        }
                    }
                }
            }
            catch (Exception CurrentException)
            {
                if (!ServerShutDown)
                {
                    MessageBox.Show(CurrentException.Message + "ConnectionWithClient", MyResourses.Texts.Error, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: Saroko-dnd/My_DZ
        public void SendMessagesToClient(BoolClass CloseCurrentSocketsGate,Socket SocketForMessaging, IPEndPoint EndPointForMessaging, string CurrentClientName)
        {
            try
            {
                StringBuilder BuilderForTextBox = new StringBuilder();
                lock(CloseCurrentSocketsGate)
                {
                    if (!CloseCurrentSocketsGate.SocketClosed)
                    {
                        SocketForMessaging.Connect(EndPointForMessaging);
                        //посылаем клиенту сигнал что сервер готов к приему сообщений
                        SocketForMessaging.Send(Encoding.UTF8.GetBytes(MyResourses.Texts.ServerAnswer));
                    }
                    else
                    {
                        return;
                    }
                }

                //Посылаем клиенту предназначенные ему сообщения и удаляем их
                while (true)
                {
                    lock (AllClientsMessages)
                    {
                        List<ClientMessage> DeleteList = new List<ClientMessage>();
                        foreach (ClientMessage CurrentMessage in AllClientsMessages)
                        {
                            if (CurrentMessage.ClientName == CurrentClientName)
                            {
                                BuilderForTextBox.AppendLine(MyResourses.Texts.ServerSendMessage);
                                BuilderForTextBox.Append(" " + CurrentClientName);
                                lock (CloseCurrentSocketsGate)
                                {
                                    if (!CloseCurrentSocketsGate.SocketClosed)
                                    {
                                        SocketForMessaging.Send(Encoding.UTF8.GetBytes(CurrentMessage.SenderName + CharSeparator + CurrentMessage.Message));
                                    }
                                    else
                                    {
                                        return;
                                    }                                 
                                }
                                DeleteList.Add(CurrentMessage);
                                lock (MainStringBuilder)
                                {
                                    ++StringCounter;
                                    MainStringBuilder.AppendLine(BuilderForTextBox.ToString());
                                    Application.Current.Dispatcher.Invoke(new Action(() => ConsoleTextBox.Text = MainStringBuilder.ToString()));
                                }
                                BuilderForTextBox.Clear();
                            }
                        }
                        foreach (ClientMessage CurrentMessage in DeleteList)
                        {
                            AllClientsMessages.Remove(CurrentMessage);
                        }
                    }
                    Thread.Sleep(300);
                }
            }
            catch (Exception CurrentException)
            {
                if (!ServerShutDown)
                {
                    MessageBox.Show(CurrentException.Message + "Messaging", MyResourses.Texts.Error, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }