Listening for UDP

  • Thread starter Thread starter Scott H
  • Start date Start date
S

Scott H

OK, what am i doing wrong here.....

-------------------------------
Dim ServerIPEP As New System.Net.IPEndPoint(System.Net.IPAddress.Any,
514)

Dim UDPc As New System.net.Sockets.UdpClient

Dim receiveBytes As Byte() = UDPc.Receive(ServerIPEP) 'fails here

Dim returnData As String =
System.Text.Encoding.ASCII.GetString(receiveBytes)

MsgBox(returnData)
--------------------------------

All I'm trying to do is listen for any UDP packets on port 514 and I
get "An invalid argument was supplied" on the .Receive method


Scott H
 
slightly modified MS example

Imports System.Web

Imports System.net

Imports System.net.Sockets

Imports System.Text

Module Module1

Sub Main()

'Creates a UdpClient for reading incoming data.

Dim receivingUdpClient As New UdpClient

'Creates an IPEndPoint to record the IP address and port number of the
sender.

' The IPEndPoint will allow you to read datagrams sent from any source.

Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 514)

Try

' Blocks until a message returns on this socket from a remote host.

Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint)

Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)

Console.WriteLine(("This is the message you received " +
returnData.ToString()))

Console.WriteLine(("This message was sent from " +
RemoteIpEndPoint.Address.ToString() + " on their port number " +
RemoteIpEndPoint.Port.ToString()))

Catch e As Exception

Console.WriteLine(e.ToString())

End Try

Console.ReadLine()



End Sub




Scott said:
OK, what am i doing wrong here.....

-------------------------------
Dim ServerIPEP As New System.Net.IPEndPoint(System.Net.IPAddress.Any,
514)

Dim UDPc As New System.net.Sockets.UdpClient

Dim receiveBytes As Byte() = UDPc.Receive(ServerIPEP) 'fails here

Dim returnData As String =
System.Text.Encoding.ASCII.GetString(receiveBytes)

MsgBox(returnData)
--------------------------------

All I'm trying to do is listen for any UDP packets on port 514 and I
get "An invalid argument was supplied" on the .Receive method


Scott H

Regards - OHM# (e-mail address removed)
 
Thanks.....but I get the same error:
"An Invalid argument was supplied" when it runs the .Receive method
Tried different port numbers too, just in case it was trying to listen
on a port/address that was already listening.

Something wrong with my .NET or windows?

Scott
 
Just in case anyone gets this problem, I just found out how to fix it.

Instead of:

Dim receivingUdpClient As New UdpClient
(as stated in many "help" files, newsgroups and message boards)

I put:

Dim receivingUdpClient As New UdpClient(514)

Even though the next line is:

Dim RemoteIpEndPoint As New
System.Net.IPEndPoint(System.Net.IPAddress.Any, 514)

Does that mean its listening on port 514 for packets on remote port
514? (both server and client on port 514?)

Anyway, it worked, received packets from a SYSLOG server just fine!

Scott H
 
Back
Top