예제 #1
0
 static void Main(string[] args)
 {
     if (args.Length == 1)
     {
         ServerObject server = new ServerObject(Convert.ToInt32(args[0]));
         //server.Disconnect();
     }
     else
     {
         ServerObject server = new ServerObject();
         //server.Disconnect();
     }
 }
예제 #2
0
파일: Game.cs 프로젝트: MaXSem13/MathDuel
 public Game(ClientObject client1, ClientObject client2, ServerObject server, string idGame)
 {
     player1           = client1;
     player1.numinGame = 1;
     player2           = client2;
     player2.numinGame = 2;
     this.server       = server;
     this.idGame       = idGame;
     //добавить в БД информацию что пользователи в игре
     server.SetInGame(player1.GetName());
     server.SetInGame(player2.GetName());
     server.AddGame(player1.GetName(), player2.GetName(), idGame);
 }
예제 #3
0
 static Thread listenThread;     // потока для прослушивания
 static void Main(string[] args)
 {
     try
     {
         server       = new ServerObject();
         listenThread = new Thread(new ThreadStart(server.Listen));
         listenThread.Start();
     }
     catch (Exception ex)
     {
         server.Disconnect();
         Console.WriteLine(ex.Message);
     }
 }
예제 #4
0
 static void Main(string[] args)
 {
     try
     {
         _server = new ServerObject();
         _listenThread = new Thread(_server.Listen);
         _listenThread.Start();
     }
     catch (Exception ex)
     {
         _server.Disconnect();
         Console.WriteLine(ex.Message);
     }
 }
예제 #5
0
 static Thread listenThread; // потока для прослушивания
 public static void ServerInitialize()
 {
     try
     {
         server       = new ServerObject();
         listenThread = new Thread(new ThreadStart(server.Listen));
         listenThread.Start(); //старт потока
     }
     catch (Exception ex)
     {
         server.Disconnect();
         Console.WriteLine(ex.Message);
     }
 }
예제 #6
0
 private void ConnectServer(int port)
 {
     try
     {
         server = new ServerObject(port);
         server.ServerMessage += PrintLog;
         listenThread          = new Thread(new ThreadStart(server.Listen));
         listenThread.Start(); //старт потока
     }
     catch (Exception ex)
     {
         server.Disconnect();
         throw;
     }
 }
예제 #7
0
 static void Main(string[] args)
 {
     try
     {
         Console.Write("Введите номер порта, который вы хотите использовать :");
         _hostPort    = Int32.Parse(Console.ReadLine());
         server       = new ServerObject(_hostPort);
         listenThread = new Thread(new ThreadStart(server.Listen));
         listenThread.Start();
     }
     catch (Exception ex)
     {
         server.Disconnect();
         Console.WriteLine(ex.Message);
     }
 }
예제 #8
0
파일: Program.cs 프로젝트: TCP-IP-CHAT/CHAT
        static Thread listenThread; // потока для прослушивания

        static void Main(string[] args)
        {
            Console.WriteLine("Введите ключ сервера");
            Vasya = Int32.Parse(Console.ReadLine());

            try
            {
                server       = new ServerObject();
                listenThread = new Thread(new ThreadStart(server.Listen));
                listenThread.Start(); //старт потока
            }
            catch (Exception ex)
            {
                server.Disconnect();
                Console.WriteLine(ex.Message);
            }
        }
예제 #9
0
        static void Main(string[] args)
        {
            try
            {
                server = new ServerObject();
#if RELEASE
                Thread thread = new Thread(new ThreadStart(server.Listen));
                thread.Start();
#elif DEBUG
                server.Listen();
#endif
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                server.disconnect(server);
            }
        }
예제 #10
0
        static void Main(string[] args)
        {
            Task         task   = null;
            ServerObject server = null;

            try
            {
                server = new ServerObject();
                task   = Task.Factory.StartNew(() => server.Listen());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                server.Disconect();
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
            Task.WaitAll(task);
        }
예제 #11
0
        public void Process()
        {
            try
            {
                Stream = client.GetStream();

                // получаем имя пользователя
                string message = GetMessage();
                userName = message;
                ClientObject.clientName = userName;

                ServerObject.BroadcastId(Id);
                ServerObject.BroadcastDelUser(userName, Id);

                message = "\n" + userName + " - вошел(-ла) в чат (количество подключенных: " + ServerObject.clients.Count + " )\n";
                ServerObject.LogChat(message);
                server.BroadcastMessage(message, this.Id);

                // в бесконечном цикле получаем сообщения от клиента
                while (true)
                {
                    try
                    {
                        message = GetMessage();
                        if (ItIsCommand(message))                    // проверка, является ли сообщение командой
                        {
                            server.BroadcastNoify(message, this.Id); // рассылка уведомления от сервера
                        }
                        else
                        {
                            Console.WriteLine(message);
                            if (message.StartsWith("[@"))
                            {
                                Console.WriteLine("Личное сообщение");
                                ServerObject.BroadcastPrivateMessages(message, Id, userName);
                            }
                            else if (message.Contains("admin admin"))
                            {
                                ServerObject.Admin(message, userName);
                                message = $"{userName} стал(-a) админом";
                                ServerObject.LogChat(message);
                                server.BroadcastMessage(message, this.Id);
                            }
                            else if (message.Contains("del"))
                            {
                                ServerObject.AdminDelUser(message, userName);
                                message = $"{userName} удалил пользователя";
                                ServerObject.LogChat(message);
                                //server.BroadcastMessage(message, this.Id);
                            }
                            else
                            {
                                message = String.Format("{0}: {1}", userName, message);
                                ServerObject.LogChat(message);
                                server.BroadcastMessage(message, this.Id);
                            }
                        }
                    }
                    catch
                    {
                        message = String.Format("\n" + "{0} - покинул(-а) чат" + "\n", userName);
                        ServerObject.LogChat(message);
                        server.BroadcastMessage(message, this.Id);
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // в случае выхода из цикла закрываем ресурсы
                server.RemoveConnection(this.Id);
                Close();
            }
        }
예제 #12
0
        ServerObject server;// объект сервера

        public ClientObject(TcpClient tcpClient, ServerObject serverObject)
        {
            Id     = Guid.NewGuid().ToString();
            client = tcpClient;
            server = serverObject;
        }
예제 #13
0
파일: Program.cs 프로젝트: MrCloudFox/Chat
 static void Main(string[] args)
 {
     server       = new ServerObject();
     listenThread = new Thread(new ThreadStart(server.Listen));
     listenThread.Start();
 }
예제 #14
0
 public void disconnect(ServerObject server)
 {
     DelConnection(server.clientObject.id);
     server.tcpListener.Stop();
     server.client.Close();
 }
예제 #15
0
        public void Procces()
        {
            try
            {
                userName = GetMessege();
                Console.WriteLine(userName);
                server.SendToAll(userName + " вошел в чат", this.id);

                string msg_info;
                string msg;
                while (true)
                {
                    try
                    {
                        msg_info = Response();
                        msg      = GetMessege();

                        Console.WriteLine(msg);

                        if (msg_info == "-1")
                        {
                            server.SendToAll(msg, this.id);
                            continue;
                        }
                        else if (msg_info == "-2")
                        {
                            foreach (byte[] clientdata in ServerObject.GetList())
                            {
                                stream.Write(clientdata);
                            }
                            continue;
                        }

                        ClientObject tmp = ServerObject.getClientFromNAME(msg_info);
                        if (tmp != null && tmp.active)
                        {
                            ServerObject.AddConectionWith(this, tmp);
                            server.SendTo(msg, this.id, msg_info);
                        }
                        else
                        {
                            Console.WriteLine("пользователя не существует");
                        }
                    }
                    catch
                    {
                        Console.WriteLine("-" + userName);
                        server.SendToAll(userName + " покинул чат", this.id);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                ServerObject.DelConnection(id);
                stream.Close();
                tcp.Close();
            }
        }