Ping request UDP C# PocketPC

B

Ben

Hello,

I'm writing an application which communicate by UDP protocol from a
PocketPC to a computer. I have to verify if the server (computer) is
still available, or when I'm trying to connect (UdpClient.Connect),
the application freezes. Since I can't catch any exception with the
Connect function, I would like to create a "ping request", using the
Socket class, which verify every x seconds if the server is available.

Here is a bit of my code :


IPEndPoint endPoint = new
IPEndPoint(IPAddress.Parse("10.102.100.127"), 9000);
EndPoint PingEndPoint = (EndPoint)endPoint;
byte[] msg = new byte[1] {1};

ping = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);

try
{
ping.SetSocketOption(SocketOptionLevel.Udp,
SocketOptionName.SendTimeout, 2000);
}
catch (SocketException se)
{
MessageBox.Show(se.ToString() + " " + se.ErrorCode);
}

try
{
ping.SendTo(msg, PingEndPoint);
}
catch (Exception so)
{
// Here it would tell me that the server is offline
MessageBox.Show(so.ToString());
}

I'm having a SocketException in the first try-catch block :

An invalid argument was supplied 10022

Does anyone can tell me how to proceed correctly?
Thanks in advance...
 
P

Paul G. Tobey [eMVP]

I think that send time-out is not settable on Windows CE, hence the error.
I'm not sure what a UDP send time-out would mean anyway, though.

I'd go back to the UdpClient-based code and figure out why Connect() is not
returning. There's no such thing as a 'connected' UDP socket. It's a
datagram protocol. There's no state in the connection. Connect(), as far
as I can tell, should return immediately having done nothing but save the
port number and IPAddress that you passed to it.

Your 'ping' code wouldn't do anything, even if the send time-out meant
something. There's no response from the other end of a UDP datagram send
mandated. Unless your *code* running on the other end responds, as PING's
peer program does, you won't ever get anything back.

Paul T.
 
R

Rick

Ben,

I recently set up a UDP connection between a PC and CE device using .NET. A
couple points:

As someone previously mentioned you don't need to connect since UDP is a
connectionless protocol. I just listened on a separate thread. The endpoint
that I set up was from "Any" not a specific address (just like the example
under MSDN, UdpClient class). When you send just pass the ip address and
port number.

Some of the constructors to create the UdpClient didn't work for me. I was
getting socket exceptions all over the place. Use the one where you just
pass it a port number, that's the only one that worked for me.

Creating the IPEndPoint was a bit of a problem. I had to use different
constructors on the PC and CE.

Good luck.

Rick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top