Example #1
0
        private void SendClientNames(Socket thisClient)
        {
            try
            {
                string names = "ClientsNameList:";

                foreach (Socket otherClient in _clientSockets)
                {
                    if (thisClient != otherClient)
                    {
                        lock (this)
                        {
                            names += _clientsName[otherClient.RemoteEndPoint.ToString()] != string.Empty
                                ? _clientsName[otherClient.RemoteEndPoint.ToString()] + ";" : "";
                        }
                    }
                }
                thisClient.Send(Encoding.GetEncoding(1251).GetBytes(names));
                Application.Current.Dispatcher.Invoke(() =>
                {
                    _logsTextBox.Text += $"[{Now.ToLongTimeString()}] Отправки списка клиентов клиенту {_clientsName[thisClient.RemoteEndPoint.ToString()]}\r\n";
                });
            }
            catch
            {
                // ignored
            }
        }
Example #2
0
        public async void Run()
        {
            Application.Current.Dispatcher.Invoke(() =>
            {
                _logsTextBox.Text += $"[{Now.ToLongTimeString()}] Сервер слушает порт #{_port}\r\n";
            });


            while (true)
            {
                _logsTextBox.Text += $"[{Now.ToLongTimeString()}] Ждем подключения нового клиента...\r\n";

                Socket newClient = await Task.Factory.FromAsync(_serverSocket.BeginAccept, _serverSocket.EndAccept, true);

                _logsTextBox.Text += $"[{Now.ToLongTimeString()}] Клиент пришел {newClient.RemoteEndPoint}\r\n";

                lock (this)
                {
                    _clientSockets.Add(newClient);
                    _clientsName.Add(newClient.RemoteEndPoint.ToString(), string.Empty);
                }

                ConversationStart(newClient);
            }
        }
Example #3
0
        static void Main(string[] args)
        {
            var p = new Pessoa()
            {
                Matricula = 123,
                Nome      = "Guinther",
                SobreNome = "Pauli"
            };

            // WriteLine("Bem-vindo " + p.Nome + " " + p.SobreNome + "(" +
            // p.Matricula + "), este é o C# 6");
            //WriteLine(string.Format("Bem-vindo {0} {1} ({2}), este é o C# 6",
            //p.Nome, p.SobreNome, p.Matricula));
            WriteLine($"Bem-vindo {p.Nome, 10} de B. {p.SobreNome, 10} ({p.Matricula}), este é o C# 6");
            WriteLine($"Olá, agora são {Now.ToLongTimeString()}");
            ReadLine();
        }
Example #4
0
        private void ConversationStart(Socket thisClient)
        {
            Task.Run(() =>
            {
                byte[] buff = new byte[1024];
                int len;
                string msg;

                while (true)
                {
                    msg = string.Empty;
                    try
                    {
                        len = thisClient.Receive(buff);

                        if (len > 0)
                        {
                            msg = Encoding.GetEncoding(1251).GetString(buff, 0, len);

                            int startIndex;
                            if (msg.IndexOf("get user list", StringComparison.Ordinal) != -1)
                            {
                                SendClientNames(thisClient);
                                msg = string.Empty;
                            }
                            else if (msg.IndexOf("set name ", StringComparison.Ordinal) != -1)
                            {
                                startIndex      = "set name ".Length;
                                string userName = msg.Substring(startIndex, msg.Length - startIndex);

                                string remoteIpPort = _clientsName.FirstOrDefault(t => t.Value.Equals(userName)).Key;

                                if (string.IsNullOrEmpty(remoteIpPort))
                                {
                                    _clientsName[thisClient.RemoteEndPoint.ToString()] = userName;

                                    Application.Current.Dispatcher.Invoke(() =>
                                    {
                                        _logsTextBox.Text +=
                                            $"[{Now.ToLongTimeString()}] Клиенту с RemoteEndPoint = {thisClient.RemoteEndPoint} установленно имя -> {_clientsName[thisClient.RemoteEndPoint.ToString()]}\r\n";
                                    });

                                    thisClient.Send(Encoding.GetEncoding(1251).GetBytes("set_name=true"));
                                }
                                else
                                {
                                    thisClient.Send(Encoding.GetEncoding(1251).GetBytes("set_name=false"));
                                }

                                msg = string.Empty;

                                foreach (Socket otherClient in _clientSockets)
                                {
                                    if (thisClient != otherClient)
                                    {
                                        SendClientNames(otherClient);
                                    }
                                }
                            }
                            else
                            {
                                Application.Current.Dispatcher.Invoke(() =>
                                {
                                    _logsTextBox.Text += $"[{Now.ToLongTimeString()}] Клиент {_clientsName[thisClient.RemoteEndPoint.ToString()]} отправил сообщение {msg}\r\n";
                                });
                            }
                        }
                    }
                    catch (SocketException)
                    {
                        lock (this)
                        {
                            _clientsName.Remove(thisClient.RemoteEndPoint.ToString());
                            _clientSockets.Remove(thisClient);
                        }
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            _logsTextBox.Text += $"[{Now.ToLongTimeString()}] Клиент ушел {_clientsName[thisClient.RemoteEndPoint.ToString()]}\r\n";
                        });

                        foreach (Socket otherClient in _clientSockets)
                        {
                            SendClientNames(otherClient);
                        }
                        break;
                    }
                    if (!thisClient.Connected)
                    {
                        Application.Current.Dispatcher.Invoke(() =>
                        {
                            _logsTextBox.Text += $"[{Now.ToLongTimeString()}] Клиент отключился {_clientsName[thisClient.RemoteEndPoint.ToString()]}\r\n";
                        });

                        lock (this)
                        {
                            _clientsName.Remove(thisClient.RemoteEndPoint.ToString());
                            _clientSockets.Remove(thisClient);
                        }
                        foreach (Socket otherClient in _clientSockets)
                        {
                            SendClientNames(otherClient);
                        }
                        break;
                    }

                    foreach (var m in msg.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        if (m.IndexOf("private message to user", StringComparison.Ordinal) != -1)
                        {
                            int start       = m.IndexOf("=") + 1;
                            string userName = m.Substring(start, m.Length - start);

                            string remoteIpPort = _clientsName.FirstOrDefault(t => t.Value.Equals(userName)).Key;

                            if (!string.IsNullOrEmpty(remoteIpPort))
                            {
                                Socket clientSocket =
                                    _clientSockets.FirstOrDefault(t => t.RemoteEndPoint.ToString().Equals(remoteIpPort));

                                if (clientSocket != null)
                                {
                                    start = m.IndexOf("private message to user", StringComparison.Ordinal);
                                    clientSocket.Send(Encoding.GetEncoding(1251).GetBytes(
                                                          $"[{_clientsName[thisClient.RemoteEndPoint.ToString()]}] [private message] ->" + m.Substring(0, start)));
                                }
                            }
                        }
                        else
                        {
                            foreach (Socket otherClient in _clientSockets)
                            {
                                if (thisClient != otherClient)
                                {
                                    lock (this)
                                    {
                                        otherClient.Send(
                                            Encoding.GetEncoding(1251).GetBytes(
                                                $"[{_clientsName[thisClient.RemoteEndPoint.ToString()]}] ->" + m));
                                    }
                                }
                            }
                        }
                    }
                }
            });
        }
Example #5
0
 public static void Main()
 {
     WriteLine(Now.ToShortDateString());
     WriteLine(Now.ToLongTimeString());
 }
Example #6
0
 private void OnGUI()
 {
     GUILayout.Label(Now.ToLongTimeString());
 }