想实现的功能,
一个服务端,多个客户端,
服务端可以向任何一个客户端发信息
任何一个客户端也能向服务端发信息代码如下:
服务端:
namespace UDPServer
{
public partial class Form1 : Form
{
private Thread t1; UdpClient udpclient;
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
udpclient = new UdpClient(12000);
t1 = new Thread(new ThreadStart(StartListener));
t1.IsBackground = true;
t1.Start();
} private void StartListener()
{
string returnData = "";
while (true)
{
try
{
byte[] bytes = udpclient.Receive(ref ep);
returnData = "这是来自客户端节的消息:" + Encoding.UTF8.GetString(bytes);
this.Invoke((EventHandler)delegate
{
this.txtReceive.Text += "\r\n" + returnData;
});
}
catch
{
}
}
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
t1.Abort();
} private void btnSend_Click(object sender, EventArgs e)
{
byte[] sendBytes = Encoding.UTF8.GetBytes(txtCommand.Text.Trim());
udpclient.Connect(ep);
udpclient.Send(sendBytes, sendBytes.Length);
}
}
}客户端a:
namespace UDPClient
{
public partial class Form1 : Form
{
UdpClient udpclient = new UdpClient(15000);
//IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.2.92"), 12000);
Thread t;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(StartListen));
t.IsBackground = true;
t.Start();
} private void Init()
{
//udpclient = new UdpClient(12000);
} private void btnSend_Click(object sender, EventArgs e)
{
try
{
byte[] sendBytes = Encoding.UTF8.GetBytes(txtSend.Text);
udpclient.Connect(ep);
udpclient.Send(sendBytes, sendBytes.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
} private void StartListen()
{
IPEndPoint ipe = null;
while (true)
{
try
{
string returnData = Encoding.UTF8.GetString(udpclient.Receive(ref ipe));
this.Invoke((EventHandler)delegate
{
this.txtReceive.Text += "\r\n" + returnData;
});
}
catch
{
}
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
t.Abort();
}
}客户端b:
namespace UDPClient
{
public partial class Form1 : Form
{
UdpClient udpclient = new UdpClient(15001);
//IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.2.92"), 12000);
Thread t;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(StartListen));
t.IsBackground = true;
t.Start();
} private void Init()
{
//udpclient = new UdpClient(12000);
} private void btnSend_Click(object sender, EventArgs e)
{
try
{
byte[] sendBytes = Encoding.UTF8.GetBytes(txtSend.Text);
udpclient.Connect(ep);
udpclient.Send(sendBytes, sendBytes.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
} private void StartListen()
{
IPEndPoint ipe = null;
while (true)
{
try
{
string returnData = Encoding.UTF8.GetString(udpclient.Receive(ref ipe));
this.Invoke((EventHandler)delegate
{
this.txtReceive.Text += "\r\n" + returnData;
});
}
catch
{
}
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
t.Abort();
}
}
}
出现的问题:
如果只运行一个客户端,服务端和客户端交互没有问题
如果运行两个客户端,服务端如果不向客户端发消息,那么能接收任何一个客户端的消息 ,
一旦服务端向某个客户端(aclient)发消息 后,服务端就不能接收非aclient发送的消息 ,也不能向非aclient发送消息,就相当于只能是服务端和aclient通信
才接触这个,不明白这种情况出现是啥原因造成的,请大家帮忙,谢谢C#通信UDP对象服务器
一个服务端,多个客户端,
服务端可以向任何一个客户端发信息
任何一个客户端也能向服务端发信息代码如下:
服务端:
namespace UDPServer
{
public partial class Form1 : Form
{
private Thread t1; UdpClient udpclient;
IPEndPoint ep = new IPEndPoint(IPAddress.Any, 0);
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
udpclient = new UdpClient(12000);
t1 = new Thread(new ThreadStart(StartListener));
t1.IsBackground = true;
t1.Start();
} private void StartListener()
{
string returnData = "";
while (true)
{
try
{
byte[] bytes = udpclient.Receive(ref ep);
returnData = "这是来自客户端节的消息:" + Encoding.UTF8.GetString(bytes);
this.Invoke((EventHandler)delegate
{
this.txtReceive.Text += "\r\n" + returnData;
});
}
catch
{
}
}
} private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
t1.Abort();
} private void btnSend_Click(object sender, EventArgs e)
{
byte[] sendBytes = Encoding.UTF8.GetBytes(txtCommand.Text.Trim());
udpclient.Connect(ep);
udpclient.Send(sendBytes, sendBytes.Length);
}
}
}客户端a:
namespace UDPClient
{
public partial class Form1 : Form
{
UdpClient udpclient = new UdpClient(15000);
//IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.2.92"), 12000);
Thread t;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(StartListen));
t.IsBackground = true;
t.Start();
} private void Init()
{
//udpclient = new UdpClient(12000);
} private void btnSend_Click(object sender, EventArgs e)
{
try
{
byte[] sendBytes = Encoding.UTF8.GetBytes(txtSend.Text);
udpclient.Connect(ep);
udpclient.Send(sendBytes, sendBytes.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
} private void StartListen()
{
IPEndPoint ipe = null;
while (true)
{
try
{
string returnData = Encoding.UTF8.GetString(udpclient.Receive(ref ipe));
this.Invoke((EventHandler)delegate
{
this.txtReceive.Text += "\r\n" + returnData;
});
}
catch
{
}
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
t.Abort();
}
}客户端b:
namespace UDPClient
{
public partial class Form1 : Form
{
UdpClient udpclient = new UdpClient(15001);
//IPEndPoint ep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse("192.168.2.92"), 12000);
Thread t;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(StartListen));
t.IsBackground = true;
t.Start();
} private void Init()
{
//udpclient = new UdpClient(12000);
} private void btnSend_Click(object sender, EventArgs e)
{
try
{
byte[] sendBytes = Encoding.UTF8.GetBytes(txtSend.Text);
udpclient.Connect(ep);
udpclient.Send(sendBytes, sendBytes.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
} private void StartListen()
{
IPEndPoint ipe = null;
while (true)
{
try
{
string returnData = Encoding.UTF8.GetString(udpclient.Receive(ref ipe));
this.Invoke((EventHandler)delegate
{
this.txtReceive.Text += "\r\n" + returnData;
});
}
catch
{
}
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
t.Abort();
}
}
}
出现的问题:
如果只运行一个客户端,服务端和客户端交互没有问题
如果运行两个客户端,服务端如果不向客户端发消息,那么能接收任何一个客户端的消息 ,
一旦服务端向某个客户端(aclient)发消息 后,服务端就不能接收非aclient发送的消息 ,也不能向非aclient发送消息,就相当于只能是服务端和aclient通信
才接触这个,不明白这种情况出现是啥原因造成的,请大家帮忙,谢谢C#通信UDP对象服务器
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货