用vs2005做的socket聊天室,其中button2是发送信息,做的是服务端和客户端能互发信息
服务端代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace Server
{
   
    public partial class Form1 : Form
    {
        private IPEndPoint MyServer;
        private Socket sock;
        private bool bb = true;
        private Socket aaa;
        public Form1()
        {
            InitializeComponent();
        }
        private void targett()
        {
            if (aaa.Connected)
            {
                toolStripStatusLabel1.Text = "与客户建立连接。";
                while (bb)
                {
                    Byte[] bbb = new Byte[64];
                    aaa.Receive(bbb, bbb.Length, 0);
                    string ccc = System.Text.Encoding.BigEndianUnicode.GetString(bbb);
                    richTextBox1.AppendText(ccc + "\r\n");
                   
                }
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Byte[] bytee = new Byte[64];
                string send =  richTextBox2.Text + "\r\n";
                bytee = System.Text.Encoding.BigEndianUnicode.GetBytes(send.
                   ToCharArray());
                aaa.Send(bytee, bytee.Length, 0);
            }
            catch { MessageBox.Show("连接尚未建立! 无法发送!"); }
        }
private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                sock.Close();
                toolStripStatusLabel1.Text =  "监听停止!";
            }
            catch { MessageBox.Show("监听尚未开始,关闭无效!"); }        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try 
            {
                MyServer = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 999);
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,ProtocolType.Tcp);
                sock.Bind(MyServer);
                sock.Listen(50);
                toolStripStatusLabel1.Text = "开始监听 ...";
                aaa = sock.Accept();
                Thread thread = new Thread(new ThreadStart(targett));
                thread.Start(); 
                sock.BeginAccept(new AsyncCallback(AcceptCallback), sock);
            }
            catch (Exception ee) { toolStripStatusLabel1.Text = ee.Message; }        }
        private void AcceptCallback(IAsyncResult ar)
        {
            Socket listener=(Socket)ar.AsyncState;
            aaa=listener.EndAccept(ar);
            aaa = ((Socket)ar.AsyncState).EndAccept(ar);
            Thread thread = new Thread(new ThreadStart(targett));
            thread.Start();
        }
    }
}
客户端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace Client
{
    public partial class Form1 : Form
    {
        private IPEndPoint MyServer;
        private Socket sock;
        private bool bb=true;
        public Form1()
        {
            InitializeComponent();
        }
        private void targett()
        {
            while (bb)
            {
                Byte[] bbb = new Byte[640];
                sock.Receive(bbb, bbb.Length, 0);
                string aaaaa = System.Text.Encoding.BigEndianUnicode.GetString(bbb);
                richTextBox1.AppendText(aaaaa);
            }
        }        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Byte[] bytee = new Byte[640];
                string send =  richTextBox2.Text + "\r\n";
                bytee = System.Text.Encoding.BigEndianUnicode.GetBytes(send.ToCharArray());
                sock.Send(bytee, bytee.Length, 0);
            }
            catch { MessageBox.Show("连接尚未建立! 无法发送!"); }
        }
private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                sock.Close();
                toolStripStatusLabel1.Text =  "断开连接!";
            }
            catch { MessageBox.Show("连接尚未建立,断开无效!"); }
        }
        private void Form1_Load(object sender, EventArgs e)
        { 
            try
            {
                MyServer = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 999);
                sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
            ProtocolType.Tcp);
                sock.Connect(MyServer);
                toolStripStatusLabel1.Text =  "连接成功!";
                Thread thread = new Thread(new ThreadStart(targett));
                thread.Start();
            }
            catch (Exception ee){MessageBox.Show(ee.Message);}        }    }
}
程序显示 richTextBox1.AppendText(ccc + "\r\n");错误
网上查查说可以用委托的方法,不知具体怎么解决,请各位朋友帮帮忙,急啊!!

解决方案 »

  1.   

    第1个方法 this.Invoke((MethodInvoker)delegate {
                                richTextBox1.AppendText(ccc + "\r\n");           });            
    简单的方法
    在窗体LOAD里加  Control.CheckForIllegalCrossThreadCalls = false;
      

  2.   

    Control.CheckForIllegalCrossThreadCalls = false
    加上这一句话,就可以了,还建议你不要用委托,如果你不懂委托还是先不要用。
      

  3.   


    this.Invoke((MethodInvoker)delegate { 
                                richTextBox1.AppendText(ccc + "\r\n");          });  这个写在哪里