Exemplo n.º 1
0
        //返回信息
        public void AddMessage(object sender, AddMessageEventArgs e)
        {
            string message = e.mess;
            string appendText;

            string[] sep     = message.Split('>');
            string[] sepIp   = sep[0].Split('<', ':');
            bool     checkIp = true;

            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                if (listBox1.Items[i].ToString() == sepIp[1])
                {
                    checkIp = false;
                }
            }
            if (checkIp && sep[1] != "断开")
            {
                this.listBox1.Items.Add(sepIp[1].Trim());
                chatTo = sepIp[1];
            }
            appendText = sep[0] + ">:   " + System.DateTime.Now.ToString() + Environment.NewLine + sep[1] + Environment.NewLine;
            int txtGetMsgLength = this.richTextBox1.Text.Length;

            this.richTextBox1.AppendText(appendText);
            this.richTextBox1.Select(txtGetMsgLength, appendText.Length - Environment.NewLine.Length * 2 - sep[1].Length);
            this.richTextBox1.SelectionColor = Color.Red;
            this.richTextBox1.ScrollToCaret();
        }
Exemplo n.º 2
0
        private void Listen()
        {
            try
            {
                IPAddress  addr            = new IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
                IPEndPoint ipLocalEndPoint = new IPEndPoint(addr, 5656);

                tcp1 = new TcpListener(ipLocalEndPoint);
                tcp1.Start();

                while (listenerRun)
                {
                    Socket s      = tcp1.AcceptSocket();
                    string remote = s.RemoteEndPoint.ToString();
                    Byte[] stream = new Byte[1024];
                    int    i      = s.Receive(stream);
                    string msg;
                    string str = System.Text.Encoding.UTF8.GetString(stream);
                    if (str.Substring(0, 1) == "1")
                    {
                        string        str_      = "欢迎登陆!";
                        TcpClient     tcpc      = new TcpClient(((IPEndPoint)s.RemoteEndPoint).Address.ToString(), 5657);
                        NetworkStream tcpStream = tcpc.GetStream();
                        Byte[]        data      = System.Text.Encoding.UTF8.GetBytes(str_);
                        tcpStream.Write(data, 0, data.Length);
                        tcpStream.Close();
                        tcpc.Close();

                        msg = "<" + remote + ">" + "上线";
                        AddMessageEventArgs arg = new AddMessageEventArgs();
                        arg.mess = msg;
                    }
                    else if (str.Substring(0, 1) == "0")
                    {
                        msg = "<" + remote + ">" + "断开";
                        AddMessageEventArgs argRe = new AddMessageEventArgs();
                        argRe.mess = remote.ToString();
                        OnIpRemod(this, argRe);
                    }

                    else
                    {
                        msg = "<" + remote + ">" +
                              System.Text.UTF8Encoding.UTF8.GetString(stream);
                        AddMessageEventArgs arg = new AddMessageEventArgs();
                        arg.mess = msg;
                        OnAddMessage(this, arg);
                    }
                }
            }
            catch (System.Security.SecurityException)
            {
                MessageBox.Show("防火墙禁止连接!");
            }
            catch (Exception) { }
        }
Exemplo n.º 3
0
 //下线
 public void IpRemo(object sender, AddMessageEventArgs e)
 {
     string[] sep = e.mess.Split(':');
     try
     {
         int index = 0;
         for (int i = 0; i < listBox1.Items.Count; i++)
         {
             if (listBox1.Items[i].ToString() == sep[0].ToString())
             {
                 index = i;
                 this.listBox1.Items.RemoveAt(index);
             }
         }
     }
     catch
     {
         MessageBox.Show("没有这个IP");
     }
 }