Need help with UdpClient

  • Thread starter Thread starter Emilio
  • Start date Start date
E

Emilio

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.
 
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 to the other pc 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 looked straight forward but I'm getting desparate

Peter
 
Plem,

Please include your source...

I would suggest not using UdpClient due to a timeout not being able to
be set
in the receive method..The receive call has to be in a thread worker,
which needs
to be aborted in a timeout situation.

David.
 
Plem,

Please include your source...

I would suggest not using UdpClient due to a timeout not being able to
be set
in the receive method..The receive call has to be in a thread worker,
which needs
to be aborted in a timeout situation.

David.
 
David,

Good idea...

The code below is a sample from the VS help... very straight forward. No
default host is set with the connect method so it should receive from any
host... it doesn't

It bangs out saying:
---------
System.Net.Sockets.SocketException: Invalid Argument
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 offset,
Int32 size, SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.Socket.ReceiveFrom(Byte[] buffer, Int32 size,
SocketFlags socketFlags, EndPoint& remoteEP)
at System.Net.Sockets.UdpClient.Receive(IPEndPoint& remoteEP)
---------

********
Private Function MicrosoftSample() As Integer
'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, 0)
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

End Function
*******

If I create an instance of an IPEndpoint and set it to the remotehost, this
occurs just the same. If I first use the connect method to set the default
host, the code hangs in the receive method as if there is nothing to receive.
(I KNOW that is not true)

Peter
 
Back
Top