473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Question about UdpClient?

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
2 2622
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Stephan Steiner | last post by:
Hi The project I'm currently working on involves sending large UDP broadcasts. As the .NET framework already provides an easy facility for sending and receiving UDP packets I thought it was a...
4
by: Morten Overgaard | last post by:
Hi I'm listening on the SysLog port (514) through UDP. The problem is that I am not receiving anything nut I know that i get messages on the port. When I use KIWI to listen on the same port via...
1
by: Dan Kelley | last post by:
I have 2 projects - 1 Winform project that sends Udp messages using the UdpClient class when a button is clicked, and a Console application that listens for these Udp messages. If I try to use...
2
by: Rick | last post by:
Hi, I'm trying to get a simple UdpClient app working. I've been looking at the MSDN info regarding UdpClient. When I set it up on my own PC and send messages to myself it works OK. If I try to...
2
by: goodmannewz | last post by:
I just cannot run the sample code from msdn. Please tell me why? Thanks a lot. I create a console C# project, parsed the example codes: public class UDPMulticastListener { private static...
3
by: D. André Dhondt | last post by:
In VB.NET 2003, is there a way to create a System.Net.Sockets.UDPClient to listen to any address AND any port? I can get it to listen to any address, but only if I specify a port (for example,...
0
by: matt | last post by:
Hi I built a class to poll an app on a udp port using the sync methods which worked fine. I've since changed to using beginreceive and now the poll works but I don't receive the response from...
7
by: | last post by:
I have the following program using System; using System.Collections; using System.Net.Sockets; using System.Net; using System.IO; public class Test { public static void Main(string s) {
0
by: Johan | last post by:
Suppose: There are two devices on my network that both broadcast UDP datagrams (and they both use the same portnumber 1234). I want to create two executables that use UdpClient internally. Each...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.