Home All Groups Group Topic Archive Search About

udpclient receive port# ?

Author
12 Sep 2006 3:50 PM
swartzbill2000
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

Author
12 Sep 2006 4:19 PM
Miro
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 RemoteIpEndPoint As New _
System.Net.IPEndPoint(System.Net.IPAddress.Any, SocketNO)

'then the sub i had as a receiver
Public Sub Receive_Application_Parameters()
        Dim receiveBytes As [Byte]() =
receivingUdpClient.Receive(RemoteIpEndPoint)

       ... more code

End Sub


<swartzbill2***@yahoo.com> wrote in message
Show quoteHide quote
news:1158076259.251382.93480@h48g2000cwc.googlegroups.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
>
Author
12 Sep 2006 4:25 PM
Miro
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
            receivingUdpClient.Close()
            'Abort the thread - This doesnt always work alone - need to
close socket as well.
            ThreadReceive.Abort()

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


Show quoteHide quote
"Miro" <miron***@golden.net> wrote in message
news:%23CWWtbo1GHA.4796@TK2MSFTNGP03.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 RemoteIpEndPoint As New _
> System.Net.IPEndPoint(System.Net.IPAddress.Any, SocketNO)
>
> 'then the sub i had as a receiver
> Public Sub Receive_Application_Parameters()
>        Dim receiveBytes As [Byte]() =
> receivingUdpClient.Receive(RemoteIpEndPoint)
>
>       ... more code
>
> End Sub
>
>
> <swartzbill2***@yahoo.com> wrote in message
> news:1158076259.251382.93480@h48g2000cwc.googlegroups.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
>>
>
>
Author
12 Sep 2006 5:52 PM
swartzbill2000
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:
Show quoteHide quote
> 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
>             receivingUdpClient.Close()
>             'Abort the thread - This doesnt always work alone - need to
> close socket as well.
>             ThreadReceive.Abort()
>
> 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" <miron***@golden.net> wrote in message
> news:%23CWWtbo1GHA.4796@TK2MSFTNGP03.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 RemoteIpEndPoint As New _
> > System.Net.IPEndPoint(System.Net.IPAddress.Any, SocketNO)
> >
> > 'then the sub i had as a receiver
> > Public Sub Receive_Application_Parameters()
> >        Dim receiveBytes As [Byte]() =
> > receivingUdpClient.Receive(RemoteIpEndPoint)
> >
> >       ... more code
> >
> > End Sub
> >
> >
> > <swartzbill2***@yahoo.com> wrote in message
> > news:1158076259.251382.93480@h48g2000cwc.googlegroups.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
> >>
> >
> >
Author
12 Sep 2006 6:20 PM
Miro
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.



<swartzbill2***@yahoo.com> wrote in message
Show quoteHide quote
news:1158083535.664044.226520@i42g2000cwa.googlegroups.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
>>             receivingUdpClient.Close()
>>             'Abort the thread - This doesnt always work alone - need to
>> close socket as well.
>>             ThreadReceive.Abort()
>>
>> 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" <miron***@golden.net> wrote in message
>> news:%23CWWtbo1GHA.4796@TK2MSFTNGP03.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 RemoteIpEndPoint As New _
>> > System.Net.IPEndPoint(System.Net.IPAddress.Any, SocketNO)
>> >
>> > 'then the sub i had as a receiver
>> > Public Sub Receive_Application_Parameters()
>> >        Dim receiveBytes As [Byte]() =
>> > receivingUdpClient.Receive(RemoteIpEndPoint)
>> >
>> >       ... more code
>> >
>> > End Sub
>> >
>> >
>> > <swartzbill2***@yahoo.com> wrote in message
>> > news:1158076259.251382.93480@h48g2000cwc.googlegroups.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
>> >>
>> >
>> >
>
Author
14 Sep 2006 11:06 AM
Stephany Young
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.Client.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.WriteLine(CType(UdpClient.Client.LocalEndPoint, IPEndPoint).Port)



Show quoteHide quote
"Miro" <miron***@golden.net> wrote in message
news:OucxQfp1GHA.4388@TK2MSFTNGP03.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.
>
>
>
> <swartzbill2***@yahoo.com> wrote in message
> news:1158083535.664044.226520@i42g2000cwa.googlegroups.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
>>>             receivingUdpClient.Close()
>>>             'Abort the thread - This doesnt always work alone - need to
>>> close socket as well.
>>>             ThreadReceive.Abort()
>>>
>>> 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" <miron***@golden.net> wrote in message
>>> news:%23CWWtbo1GHA.4796@TK2MSFTNGP03.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 RemoteIpEndPoint As New _
>>> > System.Net.IPEndPoint(System.Net.IPAddress.Any, SocketNO)
>>> >
>>> > 'then the sub i had as a receiver
>>> > Public Sub Receive_Application_Parameters()
>>> >        Dim receiveBytes As [Byte]() =
>>> > receivingUdpClient.Receive(RemoteIpEndPoint)
>>> >
>>> >       ... more code
>>> >
>>> > End Sub
>>> >
>>> >
>>> > <swartzbill2***@yahoo.com> wrote in message
>>> > news:1158076259.251382.93480@h48g2000cwc.googlegroups.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
>>> >>
>>> >
>>> >
>>
>
>
Author
14 Sep 2006 2:16 PM
swartzbill2000
Thank you. I am just writing the server.
Bill

Stephany Young wrote:
Show quoteHide quote
> 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.Client.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.WriteLine(CType(UdpClient.Client.LocalEndPoint, IPEndPoint).Port)
>
>
>
> "Miro" <miron***@golden.net> wrote in message
> news:OucxQfp1GHA.4388@TK2MSFTNGP03.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.
> >
> >
> >
> > <swartzbill2***@yahoo.com> wrote in message
> > news:1158083535.664044.226520@i42g2000cwa.googlegroups.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
> >>>             receivingUdpClient.Close()
> >>>             'Abort the thread - This doesnt always work alone - need to
> >>> close socket as well.
> >>>             ThreadReceive.Abort()
> >>>
> >>> 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" <miron***@golden.net> wrote in message
> >>> news:%23CWWtbo1GHA.4796@TK2MSFTNGP03.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 RemoteIpEndPoint As New _
> >>> > System.Net.IPEndPoint(System.Net.IPAddress.Any, SocketNO)
> >>> >
> >>> > 'then the sub i had as a receiver
> >>> > Public Sub Receive_Application_Parameters()
> >>> >        Dim receiveBytes As [Byte]() =
> >>> > receivingUdpClient.Receive(RemoteIpEndPoint)
> >>> >
> >>> >       ... more code
> >>> >
> >>> > End Sub
> >>> >
> >>> >
> >>> > <swartzbill2***@yahoo.com> wrote in message
> >>> > news:1158076259.251382.93480@h48g2000cwc.googlegroups.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
> >>> >>
> >>> >
> >>> >
> >>
> >
> >