예제 #1
0
        public void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                allDone.Set();

                Profile user = new Profile();

                // Get the socket that handles the client request.
                Socket sock = (Socket)ar.AsyncState;
                int countBytes = -1;
                byte[] buff;
                user.socket = sock.EndAccept(out buff, out countBytes, ar);
                string text = Encoding.Unicode.GetString(buff);
                string[] logAndpPas = text.Split('&');
                user.Login = logAndpPas[0];
                logAndpPas[1] = logAndpPas[1].Remove(logAndpPas[1].Length - 8);
                user.Password = logAndpPas[1];

                lock (locker)
                    clientList.Add(user);

                user.socket.BeginReceive(user.RecBuffer, 0, user.RecBuffer.Length, 0,
                    new AsyncCallback(ReceiveCallback), user);

                MessageManager.ShowMessage(user.Login + "Вошел в чат\r");
                SendSystemMessage("Вошел в чат", user);

                connectDone.Set();
            }
            catch(Exception e)
            {
                //MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
예제 #2
0
        private void Send(Profile user, string data)
        {
            try
            {
                // Convert the string data to byte data using Unicode encoding.
                byte[] byteData = Encoding.Unicode.GetBytes(data);

                // Begin sending the data to the remote device.
                user.socket.BeginSend(byteData, 0, byteData.Length, 0,
                    new AsyncCallback(SendCallback), user);
            }
            catch (Exception e)
            {
                //MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
예제 #3
0
 private void SendSystemMessage(string msg, Profile from)
 {
     try
     {
         lock (locker)
             messageList.Add(string.Format("{0} " + msg + "\r$", from.Login));
     }
     catch (Exception e)
     {
         //MessageBox.Show(e.ToString(),"Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
예제 #4
0
        private void CloseConnect(Profile user)
        {
            try
            {
                user.socket.Close();

                lock (locker)
                    clientList.Remove(user);

                SendSystemMessage("Вышел из чата ", user);
            }
            catch(Exception e)
            {
                //MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }