C# UDP 接受时死掉了,咋回事呀?

色郎中 2012-07-28 10:01:18

远程IP地址和端口
ip="192.168.7.100"
port=3000;
本地接受端口:
localPort=5000


下面是连接设备代码:

public byte[] DeviceComunicationTest(string ip,int Port, byte[] Cmd, int len)
{
udp.Connect(ip, Port);
udp.Send(Cmd, len);
byte[] receive = new byte[256];
return UdpReceiveData(ip, localPort);// 接受设备返回数据

}


接受返回数据:

 public byte[] UdpReceiveData(string ip,int LocalPort)
{
ipadr = new IPEndPoint(IPAddress.Parse(ip), LocalPort);
return udp.Receive(ref ipadr);// 死在这句上面了

}


接受函数里的IP地址:换成本地的,或远程的IP都不行呢

请教一下,哪里出了问题? 谢谢

...全文
118 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizhibin11 2012-07-28
  • 打赏
  • 举报
回复

byte[] receive = new byte[256];
public int DeviceComunicationTest(string ip,int Port, byte[] Cmd, int len)
{
udp.Bind(new IPEndPoint(IPAddress.Any, 5000));
udp.Connect(ip, Port);
udp.Send(Cmd, len);
return udp.Receive(receive);// 返回数据长度,内容就在receive中。
}
SocketUpEx 2012-07-28
  • 打赏
  • 举报
回复
UdpClient udp = new UdpClient(5000);
就是这句
色郎中 2012-07-28
  • 打赏
  • 举报
回复
看你的第一句,,就解决了哈哈
原来声明如下
UdpClient udp = new UdpClient();


用VS2005 时,这么声明,上面的程序使用时也没有问题的

现在改成这样:

  UdpClient udp = new UdpClient(5000);


接收是:
 public byte[] UdpReceiveData(string ip, int remotePort)
{
IPEndPoint ipadr = new IPEndPoint(IPAddress.Parse(ip),remotePort );
return _UDP.Receive(ref ipadr);// 死在这句上面了

}

////


总之,,声明UDP时,,加个本地端口号,就好了。。。

但是奇怪的是,,为什么VS2005时,, 声明时UDP不要 加本地端口号
在接收时,指定端口号可以
用VS2010时,,在接收时,指定端口号就不行呢?



[Quote=引用 9 楼 的回复:]

引用楼主 的回复:
public byte[] UdpReceiveData(string ip,int LocalPort)
{
ipadr = new IPEndPoint(IPAddress.Parse(ip), LocalPort);
return udp.Receive(ref ipadr);// 死在这句上面了

}



接受函数里的IP地址:换成本地的,或远……
[/Quote]
zhui22222 2012-07-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 的回复:]
public byte[] UdpReceiveData(string ip,int LocalPort)
{
ipadr = new IPEndPoint(IPAddress.Parse(ip), LocalPort);
return udp.Receive(ref ipadr);// 死在这句上面了

}



接受函数里的IP地址:换成本地的,或远程的IP都不行呢

请教一下,哪里出了问题? 谢谢
[/Quote]
udp只需绑定本地端口就可以,所有像这个端口发送的数据都是可以收下来的。所以你接收函数里的ip和端口怎么写都无所谓的。为什么不去看看例子呢
色郎中 2012-07-28
  • 打赏
  • 举报
回复
本地接收,是固定在5000端口接收

也就是说 远程设备得IP :192.168.7.100,端口3000

当设备接到命令后,,返回数据到本地的IP(192.168.7.23)地址 ,的5000端口上
所以,本地接受的端口是:5000



[Quote=引用 6 楼 的回复:]

晕,我的IP暴露了
你把数据发到3000,却从5000接收
好像不行吧
是吧
[/Quote]
zhui22222 2012-07-28
  • 打赏
  • 举报
回复
简单的udp接收建议你使用UdpClient类
这是MSDN上的例子

//Creates a UdpClient for reading incoming data.
//这里绑定本地端口,你可以改为本地5000
UdpClient receivingUdpClient = new UdpClient(11000);

//Creates an IPEndPoint to record the IP Address and port number of the sender.
// The IPEndPoint will allow you to read datagrams sent from any source.
//这里指定远端机器的ip和端口,如果只是接收不发送的话,可以随便指定
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
try{

// Blocks until a message returns on this socket from a remote host.
//接收数据,并返回远程机器的ep
Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint);

string returnData = Encoding.ASCII.GetString(receiveBytes);

Console.WriteLine("This is the message you received " +
returnData.ToString());
Console.WriteLine("This message was sent from " +
RemoteIpEndPoint.Address.ToString() +
" on their port number " +
RemoteIpEndPoint.Port.ToString());
}
catch ( Exception e ){
Console.WriteLine(e.ToString());
}

SocketUpEx 2012-07-28
  • 打赏
  • 举报
回复
晕,我的IP暴露了
你把数据发到3000,却从5000接收
好像不行吧
是吧

SocketUpEx 2012-07-28
  • 打赏
  • 举报
回复
private UdpClient _UDP;
private readonly Int32 _RemotePort = 3000;
private readonly String _RemoteIP = "192.168.1.101";

public byte[] DeviceComunicationTest(string ip, int Port, byte[] Cmd, int len)
{
_UDP = new UdpClient(_RemotePort);
_UDP.Connect(_RemoteIP, _RemotePort);

_UDP.Send(Cmd, len);

byte[] receive = new byte[256];
return UdpReceiveData(ip, _RemotePort);// 接受设备返回数据

}

public byte[] UdpReceiveData(string ip, int remotePort)
{
IPEndPoint ipadr = new IPEndPoint(IPAddress.Parse(ip), remotePort);
return _UDP.Receive(ref ipadr);// 死在这句上面了

}

private void button1_Click(object sender, EventArgs e)
{
Byte[] bytes = new Byte[2];
bytes[0] = 1;
bytes[1] = 2;

Byte[] rBytes = DeviceComunicationTest(_RemoteIP, _RemotePort, bytes, bytes.Length);
MessageBox.Show(rBytes[0] + " " + rBytes[1]);
}


zhui22222 2012-07-28
  • 打赏
  • 举报
回复
可以看看msdn的例子,socket接收,如果是udp肯定要先绑定本地端口的,接收的时候,要么使用异步接收,在收到数据后的回调函数里继续调用异步接收函数,要么使用新线程用while循环调用Receive,知道socket关闭。
色郎中 2012-07-28
  • 打赏
  • 举报
回复

我也不懂呢
所以才来问问的

[Quote=引用 2 楼 的回复:]

我也刚学Socket,看不懂啊
但有个问题就是
udp只有连接,没有绑定本地端口
凭什么能接收到数据啊?
[/Quote]
SocketUpEx 2012-07-28
  • 打赏
  • 举报
回复
我也刚学Socket,看不懂啊
但有个问题就是
udp只有连接,没有绑定本地端口
凭什么能接收到数据啊?
xiaoyao1212121 2012-07-28
  • 打赏
  • 举报
回复
接收 包的问题....

110,571

社区成员

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

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

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