By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,280 Members | 1,232 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,280 IT Pros & Developers. It's quick & easy.

Question about UdpClient?

P: n/a
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?

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <[void]> wrote in message
news:uf**************@TK2MSFTNGP09.phx.gbl...
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?

Nov 21 '05 #2

P: n/a
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" wrote:
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" <[void]> wrote in message
news:uf**************@TK2MSFTNGP09.phx.gbl...
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?


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.