コード例 #1
0
        private void Listen()
        {
            try
            {
                IPAddress  addr            = new IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
                IPEndPoint ipLocalEndPoint = new IPEndPoint(addr, 5657);
                tcp1 = new TcpListener(ipLocalEndPoint);
                tcp1.Start();


                while (listenerRun)
                {
                    Socket s                = tcp1.AcceptSocket();
                    string remote           = s.RemoteEndPoint.ToString();
                    Byte[] stream           = new Byte[512];
                    int    i                = s.Receive(stream);
                    string 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)
            {
                MessageBox.Show("监听已经停止!");
            }
        }
コード例 #2
0
ファイル: Form1.cs プロジェクト: Ricochn/WeTalk
        public void AddMessage(object sender, AddMessageEventArgs e)
        {
            string message = e.mess;
            string appendText;

            string[] sep = message.Split('>');
            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();
        }