.net UDP客户端可以';连接后无法接收
我正在使用此代码侦听端口9999 udp.net UDP客户端可以';连接后无法接收,.net,vb.net,udpclient,.net,Vb.net,Udpclient,我正在使用此代码侦听端口9999 udp Dim remoteSender As New IPEndPoint(IPAddress.Any, 0) client = New UdpClient(9999) Dim state As New UdpState(client, remoteSender) client.BeginReceive(New AsyncCallback(AddressOf DataReceived), state) 它工作得很好,如果我向127.0
Dim remoteSender As New IPEndPoint(IPAddress.Any, 0)
client = New UdpClient(9999)
Dim state As New UdpState(client, remoteSender)
client.BeginReceive(New AsyncCallback(AddressOf DataReceived), state)
它工作得很好,如果我向127.0.0.1:9999发送udp消息,我收到了对子DataReceived()触发的事件
然而,因为我想创建一个连接到udp服务器并等待服务器响应的程序。所以我在创建套接字之后插入了一个connect命令
Dim remoteSender As New IPEndPoint(IPAddress.Any, 0)
client = New UdpClient(9999)
client.Connect("127.0.0.1", 1000)
Dim state As New UdpState(client, remoteSender)
client.BeginReceive(New AsyncCallback(AddressOf DataReceived), state)
但当服务器将数据包发送回127.0.0.1:9999时,我无法从服务器收到任何响应,事件不会像第一个代码一样触发
那么我的代码怎么了?我知道C#和Vb.net,所以这两种语言的答案都很好。:
如果调用Connect方法,则从指定默认地址以外的地址到达的任何数据报都将被丢弃
您希望接收的数据报来自不同的地址。可能发送方使用的是LAN或WAN IP地址,而不是环回(127.0.0.1
)地址
如果不需要Connect
调用,只需将其删除即可