使用套接字的c#委托不能很好地工作
本文关键字:不能 很好 工作 套接字 | 更新日期: 2023-09-27 17:49:32
我正在用c# (Winform)编写一个使用socket的聊天程序。这是我的一段源代码:
(Left和Mid函数与VB相同,将返回字符串)
服务器:
Socket Serv;
List<Socket> ClnSocket = new List<Socket>();
Socket socket;
Thread run;
private delegate void sDelegate(string sData, int socketIndex);
private void MainForm_Load(object sender, EventArgs e)
{
Serv = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Serv.Bind(new IPEndPoint(IPAddress.Any, 9180));
Serv.Listen(5);
Thread accept = new Thread(this.Accept);
accept.Start();
}
private void runChatting(object s)
{
byte[] str;
socket = s as Socket;
while (true)
{
try
{
str = new byte[2048];
socket.Receive(str);
sDelegate sdelegate = new sDelegate(this.Receive);
this.Invoke(sdelegate, Encoding.Default.GetString(str), ClnSocket.IndexOf(socket));
}
catch
{
ClnSocket.Remove(socket);
return;
}
}
}
private void Receive(string sData, int socketIndex)
{
this.textBox1.AppendText(sData);
string TempData = Mid(sData, 11, sData.Length);
string[] SpliteData = TempData.Split('#');
if (Left(sData, 10) == "#musicadd#")
{
SendTo("#musicadd#" + SpliteData[1], socketIndex);
}
else
{
Send(sData);
}
}
private void Send(string sData)
{
int i;
for (i = 0; i < ClnSocket.Count; i++)
{
ClnSocket[i].Send(Encoding.Default.GetBytes(sData));
}
}
private void SendTo(string sData, int socketIndex)
{
ClnSocket[socketIndex].Send(Encoding.Default.GetBytes(sData));
}
private void Accept()
{
while (true)
{
ClnSocket.Add(Serv.Accept());
run = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(this.runChatting));
run.Start(ClnSocket[ClnSocket.Count - 1]);
}
}
客户:Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Thread run;
public delegate void sDelegate(string sData);
public Form1()
{
try
{
InitializeComponent();
socket.Connect("localhost", 9180);
run = new Thread(new ParameterizedThreadStart(Run));
run.Start();
}
catch (Exception ex){
MessageBox.Show(ex.ToString());
System.Environment.Exit(0);
}
}
private void Run(object s)
{
byte[] str = new byte[2048];
try
{
while (true)
{
socket.Receive(str);
sDelegate sdelegate = new sDelegate(this.Receive);
IntPtr x;
if (!this.IsHandleCreated) x = this.Handle;
this.Invoke(sdelegate, Encoding.Default.GetString(str));
}
}
catch (Exception e)
{
MessageBox.Show("Connection Lost." + Environment.NewLine + e.ToString());
Application.Exit();
}
}
public void Receive(string sData)
{
if (Left(sData, 10) == "#musicadd#")
{
this.MusicList.Items.Add(Mid(sData, 11, sData.Length));
}
Text_Board.AppendText(sData);
}
private void Text_Chat_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string str = this.Text_Chat.Text;
this.Text_Chat.Text = "";
Send(Encoding.Default.GetBytes(sData));
}
}
public void Send(string sData)
{
socket.Send(Encoding.Default.GetBytes(sData));
}
private void Button_Add_Click(object sender, EventArgs e)
{
Send("#musicadd#" + "Test string");
}
聊天工作正常(Text_Chat_Keydown
),但当我调用Button_Add_Click
时,客户端的ListBox永远不会改变,聊天不工作。
在这一点上我很困惑。有人能看出我做错了什么吗?
首先,你给我们的细节很差,所以我只能假设这可以解决它。
你的函数Receive
(服务器)可能抛出几个异常。我建议将其更改为以下内容:
textBox1.AppendText(sData);
if (sData.Substring(0,10) == "#musicadd#")
SendTo("#musicadd#", sData.Substring(10), socketIndex);
else
Send(sData);
除此之外,我看不出有什么理由它不应该工作。如果它仍然不工作,请发布一个日志,哪些数据将在客户端和服务器之间发送