473,785 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about UdpClient?

Question about UdpClient sample

' This constructor arbitrarily assigns the local port number.
Dim udpClient As New UdpClient()
Try
udpClient.Conne ct("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.Lengt h)

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

'Blocks until a message returns on this socket from a remote host.
Dim RemoteIpEndPoin t As New IPEndPoint(IPAd dress.Any, 0)

' Blocks until a message returns on this socket from a remote host.
Dim receiveBytes As [Byte]() = udpClient.Recei ve(RemoteIpEndP oint)
Dim returnData As String = Encoding.ASCII. GetString(recei veBytes)
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 2650
Your RemoteIpEndPoin t (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.Recei ve 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******** ******@TK2MSFTN GP09.phx.gbl...
Question about UdpClient sample

' This constructor arbitrarily assigns the local port number.
Dim udpClient As New UdpClient()
Try
udpClient.Conne ct("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.Lengt h)

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

'Blocks until a message returns on this socket from a remote host.
Dim RemoteIpEndPoin t As New IPEndPoint(IPAd dress.Any, 0)

' Blocks until a message returns on this socket from a remote host.
Dim receiveBytes As [Byte]() = udpClient.Recei ve(RemoteIpEndP oint)
Dim returnData As String = Encoding.ASCII. GetString(recei veBytes)
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 RemoteIpEndPoin t (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.Recei ve 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******** ******@TK2MSFTN GP09.phx.gbl...
Question about UdpClient sample

' This constructor arbitrarily assigns the local port number.
Dim udpClient As New UdpClient()
Try
udpClient.Conne ct("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.Lengt h)

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

'Blocks until a message returns on this socket from a remote host.
Dim RemoteIpEndPoin t As New IPEndPoint(IPAd dress.Any, 0)

' Blocks until a message returns on this socket from a remote host.
Dim receiveBytes As [Byte]() = udpClient.Recei ve(RemoteIpEndP oint)
Dim returnData As String = Encoding.ASCII. GetString(recei veBytes)
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
11866
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 good idea to use UdpClient rather than sockets directly. A few weeks back I ended up rewriting the receiver part to use sockets directly because I had to manipulate some low level socket properties, and those manipulations would fail on the...
4
16050
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 UDP tons of messages arrive.. What am I'm doing wrong My code looks like the following... private UdpClient udpClient;
1
3973
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 the UdpClient class to receive, it never works. The messages are never received (no exceptions). If I use a normal socket configured to listen for Udp messages, it works perfectly. Below is my sample code. It is only test code - I know the...
2
11900
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 send messages to another PC with another IP address I get multiple exception, the first one being "The requested address is not valid in its context". I get this when I create my UdpClient(iplistener) where iplistener is an IPEndPoint. It doesn't...
2
9531
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 readonly IPAddress GroupAddress = IPAddress.Parse("192.168.0.1"); private const int GroupPort = 9200; private static void StartListener()
3
13693
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, port 12345): '----------- Dim udpClient as New Sockets.UdpClient(12345) Dim ipEndPoint as New IPEndPoint(IPAddress.Any, 0) Dim receiveBytes as Byte()
0
2034
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 the app. Both apps run on teh same machine using the loopback address. I listen on all ports and then the callback never fires but if I specify a port and send to that port the
7
3576
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
1654
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 executable should only receive the datagrams from one device. Is this possible? Lets say the devices are 10.0.0.1 and 10.0.0.2. I have already tried something like this: UdpClient udpClient = new UdpClient(1234);
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9953
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.