C# udp 不能接收数据

lin080605120 2010-11-08 11:30:59
发送没有问题,就是接收不到内容,就在哪里卡住了,非常郁闷,希望大虾能帮我一下,先谢谢了!
源码:

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.Sockets;
using System.IO;
using System.Net;
namespace UDP
{
public partial class Form1 : Form
{


public Form1()
{
InitializeComponent();
}
IPEndPoint ipEndPoint;
//发送
private void button1_Click(object sender, EventArgs e)
{
UdpClient udpClient;
udpClient = new UdpClient(12345);
ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000);
byte[] mybyte = Encoding.Default.GetBytes("nihao");
udpClient.Send(mybyte, mybyte.Length,ipEndPoint);

}
//接收
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("1");
UdpClient udpServer;
// IPEndPoint ipEndPoint;
udpServer = new UdpClient(12355);
MessageBox.Show("2");
if (udpServer != null)
{
MessageBox.Show("bukong");
}
ipEndPoint = new IPEndPoint(new IPAddress(0),0);
MessageBox.Show("3");
byte[] data = udpServer.Receive(ref ipEndPoint);//执行到这步就卡住了
MessageBox.Show("4");
MessageBox.Show ( Encoding.Default.GetString(data));
MessageBox.Show("5");

}
}
}
...全文
204 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
deepmist 2010-11-09
  • 打赏
  • 举报
回复
发送和接收的端口要一致
lonyjianhui 2010-11-09
  • 打赏
  • 举报
回复
这样也能接收那网络就不得了了
萨拉嘿 2010-11-09
  • 打赏
  • 举报
回复
//发送
udpClient = new UdpClient();
ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 12355);
大黄鸭在发光 2010-11-09
  • 打赏
  • 举报
回复
你的IPAddress那么写不对吧
IPHostEntry iphost = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint ipend = new IPEndPoint(iphost.AddressList[0], 5566);
UdpClient uc = new UdpClient(ipend);
byte[] recvData = uc.Receive(ref ipend);
CalenNet 2010-11-08
  • 打赏
  • 举报
回复
你的程序发并且接?
网络通讯只能发出去就接,因此必须两个程序运行才可以

另外ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000);
这里是60000端口,已经接近65535了,注意,不过没事
ipEndPoint = new IPEndPoint(new IPAddress(0),0);
这里好像指定的端口是0,两个一致才可以
ipEndPoint = new IPEndPoint(new IPAddress(0),60000);

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧