473,770 Members | 4,999 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

udpclient receive port# ?

If I do this:
Dim receiver As New UdpClient()
Then the documentation says the system will pick a port. How can I
extract the chosen port number from receiver?
Bill

Sep 12 '06 #1
6 2818
Bill,

This is a great example:
http://www.codeproject.com/vb/net/UDP_Send_Receive.asp

I used it...but basically you do this:

'in my case i made it a public var
Public SocketNO As Integer = 1234 ' where 1234 is your socket no u want
Public RemoteIpEndPoin t As New _
System.Net.IPEn dPoint(System.N et.IPAddress.An y, SocketNO)

'then the sub i had as a receiver
Public Sub Receive_Applica tion_Parameters ()
Dim receiveBytes As [Byte]() =
receivingUdpCli ent.Receive(Rem oteIpEndPoint)

... more code

End Sub
<sw************ @yahoo.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
If I do this:
Dim receiver As New UdpClient()
Then the documentation says the system will pick a port. How can I
extract the chosen port number from receiver?
Bill

Sep 12 '06 #2
One more thing Bill,

Dont forget to close the Receiving UPD client before you close the
hread. - i dont think the example link shows it.
Otherwise what happens is the Application still remains running for a while
even after
you close the form.
So what I did is on the "close" of the form I added this code.

'Close the Socket - which should force the close of the open
thread listener
'Without this the thread could still remain open
receivingUdpCli ent.Close()
'Abort the thread - This doesnt always work alone - need to
close socket as well.
ThreadReceive.A bort()

If you want my mini snippet of code i created to send and recieve strings
between an exe, lemi know.
Ill just paste it all here.

Miro
"Miro" <mi******@golde n.netwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Bill,

This is a great example:
http://www.codeproject.com/vb/net/UDP_Send_Receive.asp

I used it...but basically you do this:

'in my case i made it a public var
Public SocketNO As Integer = 1234 ' where 1234 is your socket no u want
Public RemoteIpEndPoin t As New _
System.Net.IPEn dPoint(System.N et.IPAddress.An y, SocketNO)

'then the sub i had as a receiver
Public Sub Receive_Applica tion_Parameters ()
Dim receiveBytes As [Byte]() =
receivingUdpCli ent.Receive(Rem oteIpEndPoint)

... more code

End Sub
<sw************ @yahoo.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
>If I do this:
Dim receiver As New UdpClient()
Then the documentation says the system will pick a port. How can I
extract the chosen port number from receiver?
Bill


Sep 12 '06 #3
Interesting, but it doesn't answer my question. If the UdpClient ctor
picks a free port number, how can I find out which port number?
Bill

Miro wrote:
One more thing Bill,

Dont forget to close the Receiving UPD client before you close the
hread. - i dont think the example link shows it.
Otherwise what happens is the Application still remains running for a while
even after
you close the form.
So what I did is on the "close" of the form I added this code.

'Close the Socket - which should force the close of the open
thread listener
'Without this the thread could still remain open
receivingUdpCli ent.Close()
'Abort the thread - This doesnt always work alone - need to
close socket as well.
ThreadReceive.A bort()

If you want my mini snippet of code i created to send and recieve strings
between an exe, lemi know.
Ill just paste it all here.

Miro
"Miro" <mi******@golde n.netwrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
Bill,

This is a great example:
http://www.codeproject.com/vb/net/UDP_Send_Receive.asp

I used it...but basically you do this:

'in my case i made it a public var
Public SocketNO As Integer = 1234 ' where 1234 is your socket no u want
Public RemoteIpEndPoin t As New _
System.Net.IPEn dPoint(System.N et.IPAddress.An y, SocketNO)

'then the sub i had as a receiver
Public Sub Receive_Applica tion_Parameters ()
Dim receiveBytes As [Byte]() =
receivingUdpCli ent.Receive(Rem oteIpEndPoint)

... more code

End Sub
<sw************ @yahoo.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
If I do this:
Dim receiver As New UdpClient()
Then the documentation says the system will pick a port. How can I
extract the chosen port number from receiver?
Bill
Sep 12 '06 #4
Sorry, I misread the question.
I read it as you were trying to figure out how to assign the Port.

My bad,
But....

I was under the impression you "Have To" and always should specify a port.
Even if you create an array of ports to use and randomly go thru them.
Otherwise Firewalls will catch and wont let thru a "random port" that you
try to create.

Ive taken a look at all my examples I had when I tried the UPD socket, and
all of them had
a port specified.

Im a VB.net newbie and you have now stumped me.
Hopefully someone else reads this and helps us both out now.

Any reason though you do not want to specify a socket?

M.

<sw************ @yahoo.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
Interesting, but it doesn't answer my question. If the UdpClient ctor
picks a free port number, how can I find out which port number?
Bill

Miro wrote:
>One more thing Bill,

Dont forget to close the Receiving UPD client before you close the
hread. - i dont think the example link shows it.
Otherwise what happens is the Application still remains running for a
while
even after
you close the form.
So what I did is on the "close" of the form I added this code.

'Close the Socket - which should force the close of the open
thread listener
'Without this the thread could still remain open
receivingUdpCli ent.Close()
'Abort the thread - This doesnt always work alone - need to
close socket as well.
ThreadReceive.A bort()

If you want my mini snippet of code i created to send and recieve strings
between an exe, lemi know.
Ill just paste it all here.

Miro
"Miro" <mi******@golde n.netwrote in message
news:%2******* *********@TK2MS FTNGP03.phx.gbl ...
Bill,

This is a great example:
http://www.codeproject.com/vb/net/UDP_Send_Receive.asp

I used it...but basically you do this:

'in my case i made it a public var
Public SocketNO As Integer = 1234 ' where 1234 is your socket no u
want
Public RemoteIpEndPoin t As New _
System.Net.IPEn dPoint(System.N et.IPAddress.An y, SocketNO)

'then the sub i had as a receiver
Public Sub Receive_Applica tion_Parameters ()
Dim receiveBytes As [Byte]() =
receivingUdpCli ent.Receive(Rem oteIpEndPoint)

... more code

End Sub
<sw************ @yahoo.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
If I do this:
Dim receiver As New UdpClient()
Then the documentation says the system will pick a port. How can I
extract the chosen port number from receiver?
Bill

Sep 12 '06 #5
Who mentioned firewalls - itt could just as easily be for communication
between 2processs on the same machine.

Because this end is the will be sending the packet, you don't really care
which port it uses. You only have to know which port to send it to!

If you want to know which port the OS has assigned then you can get from

UdpClient.Clien t.LocalEndPoint

Therre is a caveat that this property returns an EndPoint object and you
have to cast it as IPEndPoint object before you can get the port, e.g.:

Console.WriteLi ne(CType(UdpCli ent.Client.Loca lEndPoint, IPEndPoint).Por t)

"Miro" <mi******@golde n.netwrote in message
news:Ou******** ******@TK2MSFTN GP03.phx.gbl...
Sorry, I misread the question.
I read it as you were trying to figure out how to assign the Port.

My bad,
But....

I was under the impression you "Have To" and always should specify a port.
Even if you create an array of ports to use and randomly go thru them.
Otherwise Firewalls will catch and wont let thru a "random port" that you
try to create.

Ive taken a look at all my examples I had when I tried the UPD socket, and
all of them had
a port specified.

Im a VB.net newbie and you have now stumped me.
Hopefully someone else reads this and helps us both out now.

Any reason though you do not want to specify a socket?

M.

<sw************ @yahoo.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
>Interesting, but it doesn't answer my question. If the UdpClient ctor
picks a free port number, how can I find out which port number?
Bill

Miro wrote:
>>One more thing Bill,

Dont forget to close the Receiving UPD client before you close the
hread. - i dont think the example link shows it.
Otherwise what happens is the Application still remains running for a
while
even after
you close the form.
So what I did is on the "close" of the form I added this code.

'Close the Socket - which should force the close of the open
thread listener
'Without this the thread could still remain open
receivingUdpCli ent.Close()
'Abort the thread - This doesnt always work alone - need to
close socket as well.
ThreadReceive.A bort()

If you want my mini snippet of code i created to send and recieve
strings
between an exe, lemi know.
Ill just paste it all here.

Miro
"Miro" <mi******@golde n.netwrote in message
news:%2****** **********@TK2M SFTNGP03.phx.gb l...
Bill,

This is a great example:
http://www.codeproject.com/vb/net/UDP_Send_Receive.asp

I used it...but basically you do this:

'in my case i made it a public var
Public SocketNO As Integer = 1234 ' where 1234 is your socket no u
want
Public RemoteIpEndPoin t As New _
System.Net.IPE ndPoint(System. Net.IPAddress.A ny, SocketNO)

'then the sub i had as a receiver
Public Sub Receive_Applica tion_Parameters ()
Dim receiveBytes As [Byte]() =
receivingUdpCl ient.Receive(Re moteIpEndPoint)

... more code

End Sub
<sw*********** *@yahoo.comwrot e in message
news:11******* **************@ h48g2000cwc.goo glegroups.com.. .
If I do this:
Dim receiver As New UdpClient()
Then the documentation says the system will pick a port. How can I
extract the chosen port number from receiver?
Bill


Sep 14 '06 #6
Thank you. I am just writing the server.
Bill

Stephany Young wrote:
Who mentioned firewalls - itt could just as easily be for communication
between 2processs on the same machine.

Because this end is the will be sending the packet, you don't really care
which port it uses. You only have to know which port to send it to!

If you want to know which port the OS has assigned then you can get from

UdpClient.Clien t.LocalEndPoint

Therre is a caveat that this property returns an EndPoint object and you
have to cast it as IPEndPoint object before you can get the port, e.g.:

Console.WriteLi ne(CType(UdpCli ent.Client.Loca lEndPoint, IPEndPoint).Por t)

"Miro" <mi******@golde n.netwrote in message
news:Ou******** ******@TK2MSFTN GP03.phx.gbl...
Sorry, I misread the question.
I read it as you were trying to figure out how to assign the Port.

My bad,
But....

I was under the impression you "Have To" and always should specify a port.
Even if you create an array of ports to use and randomly go thru them.
Otherwise Firewalls will catch and wont let thru a "random port" that you
try to create.

Ive taken a look at all my examples I had when I tried the UPD socket, and
all of them had
a port specified.

Im a VB.net newbie and you have now stumped me.
Hopefully someone else reads this and helps us both out now.

Any reason though you do not want to specify a socket?

M.

<sw************ @yahoo.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com.. .
Interesting, but it doesn't answer my question. If the UdpClient ctor
picks a free port number, how can I find out which port number?
Bill

Miro wrote:
One more thing Bill,

Dont forget to close the Receiving UPD client before you close the
hread. - i dont think the example link shows it.
Otherwise what happens is the Application still remains running for a
while
even after
you close the form.
So what I did is on the "close" of the form I added this code.

'Close the Socket - which should force the close of the open
thread listener
'Without this the thread could still remain open
receivingUdpCli ent.Close()
'Abort the thread - This doesnt always work alone - need to
close socket as well.
ThreadReceive.A bort()

If you want my mini snippet of code i created to send and recieve
strings
between an exe, lemi know.
Ill just paste it all here.

Miro
"Miro" <mi******@golde n.netwrote in message
news:%2******* *********@TK2MS FTNGP03.phx.gbl ...
Bill,

This is a great example:
http://www.codeproject.com/vb/net/UDP_Send_Receive.asp

I used it...but basically you do this:

'in my case i made it a public var
Public SocketNO As Integer = 1234 ' where 1234 is your socket no u
want
Public RemoteIpEndPoin t As New _
System.Net.IPEn dPoint(System.N et.IPAddress.An y, SocketNO)

'then the sub i had as a receiver
Public Sub Receive_Applica tion_Parameters ()
Dim receiveBytes As [Byte]() =
receivingUdpCli ent.Receive(Rem oteIpEndPoint)

... more code

End Sub
<sw************ @yahoo.comwrote in message
news:11******** *************@h 48g2000cwc.goog legroups.com...
If I do this:
Dim receiver As New UdpClient()
Then the documentation says the system will pick a port. How can I
extract the chosen port number from receiver?
Bill

Sep 14 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
16047
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;
2
1823
by: Richy Rich | last post by:
Hi, I've developed an application in C# which spawns a thread to receive datagrams. If I use the socket receive method, there are no problems when using the application under NT or XP. I recently moved over to using the UdpClient receive method. This was so I could record the IPEndPoint from
4
5156
by: Emilio | last post by:
In the help pages for the UdpClient there is some sample code which sends a udp message and prints what it received. Can someone help me to finish the code present there? I think basically the missing part is the server. Tia.
1
7847
by: Steve | last post by:
Please take a look at the simple code segment below and advise me what is wrong. According to the help and examples I've seen it should work unless I misunderstand something. The problem is that UdpClient.Receive method always throws following exception, even though I verified that message was successfuly received by devices and responses were sent back:
2
2650
by: Emilio | last post by:
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 () = Encoding.ASCII.GetBytes("Is anybody there?")
1
2182
by: Plem | last post by:
Hi All, I've allready posted this on the C# group (oops). I'm trying to get a UDPclient to receive. I use the sample code provided in HELP but to nbo avail. When the receive method is executed, it throws an exception, claiming an invalid argument was used. The WinAPI description on MSDN says: Invalid argument.
3
13692
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()
2
2898
by: ssmulgund | last post by:
What I'm trying to do seems pretty simple on the face of it -- create a UDP listener on a particular port. My code is as follows: int port = 9000; IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port); UdpClient listener = new UdpClient(endPoint); // or new UdpClient(port); while (true){ byte bytes = listener.Receive(ref endPoint);
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
9592
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
10231
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
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
9871
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
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
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
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.