发送端:
public partial class Form1 : Form
{
UdpClient udpClient;
IPEndPoint ipEndPoint;
public Form1()
{
InitializeComponent();
udpClient = new UdpClient();
ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
udpClient.Client.Connect(ipEndPoint);
} private void button1_Click(object sender, EventArgs e)
{
if (udpClient.Client.Connected)
{ byte[] mybyte = Encoding.Default.GetBytes("this is my firs udp message");
int t = udpClient.Send(mybyte, mybyte.Length, ipEndPoint);
if (t == 0)
{
MessageBox.Show("failed");
}
}
else
{
MessageBox.Show("failed");
}
} }接收端:
public partial class Form1 : Form
{
UdpClient udpServer;
IPEndPoint ipEndPoint;
public Form1()
{
InitializeComponent();
udpServer = new UdpClient();
ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
udpServer.Client.Connect(ipEndPoint); Thread thread = new Thread(new ThreadStart(Listener));
thread.IsBackground = true;
thread.Start();
} private void Listener()
{
while (udpServer != null)
{
try
{
byte[] message = udpServer.Receive(ref ipEndPoint);
string txt = Encoding.UTF8.GetString(message);
Debug.WriteLine("--------------" + txt);
this.textBox1.Text += txt + Environment.NewLine;
}
catch
{
break;
}
}
} }
在调试程序的时候老是卡在byte[] message = udpServer.Receive(ref ipEndPoint);
这条语句上不往下执行了,希望大哥大姐们帮忙解决解决、
再次感谢
public partial class Form1 : Form
{
UdpClient udpClient;
IPEndPoint ipEndPoint;
public Form1()
{
InitializeComponent();
udpClient = new UdpClient();
ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
udpClient.Client.Connect(ipEndPoint);
} private void button1_Click(object sender, EventArgs e)
{
if (udpClient.Client.Connected)
{ byte[] mybyte = Encoding.Default.GetBytes("this is my firs udp message");
int t = udpClient.Send(mybyte, mybyte.Length, ipEndPoint);
if (t == 0)
{
MessageBox.Show("failed");
}
}
else
{
MessageBox.Show("failed");
}
} }接收端:
public partial class Form1 : Form
{
UdpClient udpServer;
IPEndPoint ipEndPoint;
public Form1()
{
InitializeComponent();
udpServer = new UdpClient();
ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8001);
udpServer.Client.Connect(ipEndPoint); Thread thread = new Thread(new ThreadStart(Listener));
thread.IsBackground = true;
thread.Start();
} private void Listener()
{
while (udpServer != null)
{
try
{
byte[] message = udpServer.Receive(ref ipEndPoint);
string txt = Encoding.UTF8.GetString(message);
Debug.WriteLine("--------------" + txt);
this.textBox1.Text += txt + Environment.NewLine;
}
catch
{
break;
}
}
} }
在调试程序的时候老是卡在byte[] message = udpServer.Receive(ref ipEndPoint);
这条语句上不往下执行了,希望大哥大姐们帮忙解决解决、
再次感谢
这个同步阻止模式就是,直到UDPClient收到ipEndPoint 这个终端发送的数据之前,程序会一直阻塞在此处,所以它才会一直卡在这里。如果你想让程序走到这里后继续往下运行,你就应该用异步的接收方法或者使用线程,在线程里接收数据,这样界面也不会卡了。
我已经开了一个线程做接收了呀,界面也一直没有卡,发送端发送数据,但是接收端一直都接收不到数据,这点比较郁闷。
你说的msdn我都看了很多遍了
能把你的想法说的仔细点嘛?谢了
如果你只是接收本机的程序以127.0.0.1:8001作为发送端往你写的这个程序上发送数据的话,你的接收程序是没问题的,但是如果你想接收所有的终端发过来的数据的话,你就应当这样定义:
ipEndPoint = new IPEndPoint(IPAddress.Any, 0);//这么写是让你的UDPClient能接收所有终端发过来的数据。
udpServer.Client.Connect(ipEndPoint);
//把上面两句改为:
ipEndPoint = new IPEndPoint(IPAddress.Any, 0);//这么写是让你的UDPClient能接收所有终端发过来的数据。
// udpServer.Client.Connect(ipEndPoint);
//另外,你Catch到异常之后不应该直接Break,应该看一下异常原因。
一下是我写的测试程序代码,你参考下吧。
在main函数里调用Init(你要监听的端口号)即可。 /// <summary>
/// 初始化程序
/// </summary>
/// <param name="port">要监听的端口号</param>
static void Init(int port)
{
udpserver = new UdpClient(port);
ShowMsg("正在监听端口:"+port.ToString());
IPEndPoint AllIPEndPoint=new IPEndPoint(IPAddress.Broadcast,0);
byte[] Receive=new byte[0];
try
{
while (true)
{
System.Threading.Thread.Sleep(20);
int iReceiveCount = 0;
Receive = udpserver.Receive(ref AllIPEndPoint);
if (Receive.Length > 0)
{
//w
try
{
ShowMsg("收到数据(" + AllIPEndPoint.ToString() + ") " + NomalFunction.ByteArrayToHexString(Receive));
}
catch (Exception ex)
{
ShowExecptionMsg(ex);
}
}
}
}
catch (Exception ex)
{
ShowMsg("接收UDP数据时出错:");
ShowExecptionMsg(ex);
}
finally
{
udpserver.Close();
} }
#region 消息
/// <summary>
/// 显示消息记录及写日志
/// </summary>
/// <param name="msg"></param>
private static void ShowMsg(string msg)
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss.fff") + ": " + msg); }
/// <summary>
/// 显示消息记录及写日志
/// </summary>
/// <param name="msg"></param>
private static void ShowExecptionMsg(Exception ex)
{
Console.WriteLine("[异常]" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:sss.fff") + ": " + ex.ToString()); }
#endregion