昨天我用了你的服务器,跟我自己的客户端,想组成聊天工具(偷懒),发现可以连接,但是从客户端主动跟服务器断开连接,或者是从客户端向服务器上发送消息的时候,都指出我加黑加下划线的那句出现以下问题:线程间操作无效: 从不是创建控件“richTextBox1”的线程访问它。我将部分客户端代码贴出来:
byte[] m_dataBuffer = new byte[10];
IAsyncResult result;
public AsyncCallback pfnCallBack;
public Socket clientSocket;
//等待数据
public void WaitForData()
{
try
{
if (pfnCallBack == null)
{
//当连接上的客户有写的操作的时候,调用回调函数
pfnCallBack = new AsyncCallback(OnDataReceived);
}
SocketPacket socketPacket = new SocketPacket();
socketPacket.Equals=clientSocket;
result =clientSocket.BeginReceive(socketPacket.dataBuffer,0,
socketPacket.dataBuffer.Length,SocketFlags.None,pfnCallBack,
socketPacket);
}
catch(SocketException se)
{
MessageBox.Show(se.Message,"提示");
}
}
//该类保存Socket以及发送给服务器的数据
public class SocketPacket
{
public System.Net.Sockets.Socket thisSocket;
public byte[] dataBuffer = new byte[1024];//发送给服务器的数据
}
//接收数据
public void OnDataReceived(IAsyncResult asyn)
{
try
{
SocketPacket theSockId = (SocketPacket)asyn.AsyncState;
//异步调用
int iRx = theSockId.thisSocket.EndReceive(asyn);
//
char[] chars = new char[iRx + 1];
//
System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();
int charLen = decoder.GetChars(theSockId.dataBuffer, 0, iRx, chars, 0);
System.String szData = new System.String(chars);
//将接收的信息显示在信息列表中
richTextBox1.Text = richTextBox1.Text + szData;
//等待数据
WaitForData();
}
catch (ObjectDisposedException)
{
System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived:Socket 已经关闭!\n");
}
catch (SocketException se)
{
if (se.ErrorCode == 10054)
{
string msg = "服务器" + "停止服务!" + "\n";
richTextBox1.Text = richTextBox1.Text + msg;
clientSocket.Close();
clientSocket = null;
UpdateControls(false);
}
else
{
MessageBox.Show(se.Message, "提示");
}
}
}
//更新控件
private void UpdateControls(bool connected)
{
button13.Enabled = !connected;
button14.Enabled = connected;
if (connected)
{
status.Text = "已连接";
}
else
{
status.Text = "无连接";
}
private void button9_Click(object sender, EventArgs e)//发送按钮
{
try
{
//如果客户端与服务器有连接,允许发送信息
if (clientSocket.Connected)
{
string msg = "\n"+label19.Text + "to " + textBox5.Text; //+ "\n";
//用UTF8格式来将string信息转化为byte数组形式
byte[] byData = System.Text.Encoding.UTF8.GetBytes(msg);
if (clientSocket != null)
{
//发送数据
clientSocket.Send(byData);
}
}
}
catch (Exception se)
{
MessageBox.Show(se.Message, "提示");
}
//int wHotkey;
//wHotkey = (HOTKEYF_Enter) * 0x0100;
//SendMessage(this.Handle, WM_SETHOTKEY, wHotkey, 0);
}
private void button13_Click(object sender, EventArgs e)//连接服务器
{
//ip地址不能为空 连接按钮
if (txtIP.Text == "" || txtPort.Text == "")
{
MessageBox.Show("请填写服务器地址和端口!", "提示");
return;
}
try
{
//创建Socket实例
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
//得到服务器的ip地址
IPAddress ipAddress = IPAddress.Parse(txtIP.Text);
Int32 port = Int32.Parse(txtPort.Text);
//创建远程终结点
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
//连接到远程服务器
clientSocket.Connect(remoteEP);
if (clientSocket.Connected)
{
UpdateControls(true);
WaitForData();//异步等待数据
}
}
catch (SocketException se)
{
MessageBox.Show(se.Message, "提示");
UpdateControls(false);
}
}
怎么修改呢???大概能看懂了吧?我做的是c/s模型的
[此贴子已经被作者于2007-2-2 11:17:07编辑过]