2
我使用此代碼來偵聽端口9999 udp。連接後無法接收UdpClient
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)
它完美地工作,我收到了子DataReceived()如果我發送一個UDP消息127.0.0.1:9999觸發的事件。
但是,因爲我想創建一個連接到udp服務器並等待服務器響應的程序。所以我在創建套接字後插入了一個連接命令。
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,所以兩種語言的答案都很好。