Ejemplo n.º 1
0
        private void AcceptClient()
        {
            srReceiver = new StreamReader(tcpClient.GetStream());
            swSender   = new StreamWriter(tcpClient.GetStream());


            currUser = srReceiver.ReadLine();


            if (currUser != "")
            {
                if (Server.htUsers.Contains(currUser) == true)
                {
                    swSender.WriteLine("0");
                    swSender.Flush();
                    CloseConnection();
                    return;
                }
                else if (currUser == "")
                {
                    swSender.WriteLine("0");
                    swSender.Flush();
                    CloseConnection();
                    return;
                }
                else
                {
                    swSender.WriteLine("1");
                    swSender.Flush();

                    Server.AddUser(tcpClient, currUser);
                }
            }
            else
            {
                CloseConnection();
                return;
            }

            try
            {
                while ((strResponse = srReceiver.ReadLine()) != "")
                {
                    if (strResponse == null)
                    {
                        Server.RemoveUser(tcpClient);
                    }
                    else
                    {
                        Server.SendMessage(currUser, strResponse);
                    }
                }
            }
            catch
            {
                Server.RemoveUser(tcpClient);
            }
        }
Ejemplo n.º 2
0
        public static void MessageHandler(Server serv, Socket socket)
        {
            string message = "";

            while (true)
            {
                message = Server.ReceiveMessage(serv, socket);
                foreach (var client in serv.ClientList)
                {
                    if (client.Key == socket)
                    {
                        continue;
                    }
                    serv.SendMessage(client.Key, serv.ClientList[socket], message);
                }
            }

            serv.ThreadList[socket].Abort();
            serv.ThreadList.Remove(socket);
            serv.StopWorking();
        }