udpclient receive port# ?

  • Thread starter Thread starter swartzbill2000
  • Start date Start date
S

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
 
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
 
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 said:
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


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
 
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
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 said:
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


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
 
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.



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
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 said:
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


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
 
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 said:
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.



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
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


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


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
 
Thank you. I am just writing the server.
Bill

Stephany said:
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 said:
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.



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


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


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
 
Back
Top