Question about UdpClient?

  • Thread starter Thread starter Emilio
  • Start date Start date
E

Emilio

Question about UdpClient sample

' This constructor arbitrarily assigns the local port number.
Dim udpClient As New UdpClient()
Try
udpClient.Connect("www.contoso.com", 11000)

' Sends a message to the host to which you have connected.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")

udpClient.Send(sendBytes, sendBytes.Length)

' Sends message to a different host using optional hostname and port
parameters.
Dim udpClientB As New UdpClient()
udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName",
11000)

'Blocks until a message returns on this socket from a remote host.
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)

' Blocks until a message returns on this socket from a remote host.
Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)


I was wondering what is the reason that when we need to receive
a message from a remote host we do IPAddress.Any,0
i.e why do we wait at port 0 rather than 11000, which is where
we sent to?
 
Your RemoteIpEndPoint (with IPAddress.Any, port 0) is used only as a byref
parameter - that is, it is filled in with the actual IPAddress and port that
the data came from. So you're not listening on port 0. That's just a value
you're passing to a constructor because UdpClient.Receive takes a
constructed IPEndPoint - it's not actually used. If you want to limit the
host that you can accept UDP traffic from, specify that in the Connect
method (which you seem to havedone)
 
Philip,

You're obviously getting this to work... I've tried everything to get this
sample to work but I never receive anything. Both pc-s are connected ok; ping
works both ways and a udp test program gets messages across both ways. When I
use the Send method I get messages across fine. Receive only gives me
trouble. I copied the example from HELP. It throws an exception telling me I
'm feeding it an invalid argument...

It all lookde straight forward but I'm getting desparate

Peter

Philip Rieck said:
Your RemoteIpEndPoint (with IPAddress.Any, port 0) is used only as a byref
parameter - that is, it is filled in with the actual IPAddress and port that
the data came from. So you're not listening on port 0. That's just a value
you're passing to a constructor because UdpClient.Receive takes a
constructed IPEndPoint - it's not actually used. If you want to limit the
host that you can accept UDP traffic from, specify that in the Connect
method (which you seem to havedone)


Emilio said:
Question about UdpClient sample

' This constructor arbitrarily assigns the local port number.
Dim udpClient As New UdpClient()
Try
udpClient.Connect("www.contoso.com", 11000)

' Sends a message to the host to which you have connected.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")

udpClient.Send(sendBytes, sendBytes.Length)

' Sends message to a different host using optional hostname and port
parameters.
Dim udpClientB As New UdpClient()
udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName",
11000)

'Blocks until a message returns on this socket from a remote host.
Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)

' Blocks until a message returns on this socket from a remote host.
Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)


I was wondering what is the reason that when we need to receive
a message from a remote host we do IPAddress.Any,0
i.e why do we wait at port 0 rather than 11000, which is where
we sent to?
 
Back
Top