Hi, im sending bytes over a socket form pocket PC to desktop Pc. Sending is working ok.
Now I wish to close the socket on PocketPC side, so I use the following on pocket PC:
handler.Shutdown(SocketShutdown.Both);
handler.Close();
handler = null;
But on the desktop side the socket is not closing
Heres what I have on desktop side:
private void startListenThread()
{
writeLine("Starting Service...");
while (true)
{
allDone.Reset();
socket.BeginAccept(
new AsyncCallback(acceptCallback),
socket);
allDone.WaitOne();
}
}
public void acceptCallback(IAsyncResult ar)
{
allDone.Set();
// receive
Socket listener = (Socket)ar.AsyncState;
Socket handler = listener.EndAccept(ar);
handlers.Add(handler); // handlers is an arrayList
setToolStripStatusLabel1(handlers.Count + " Pocket PC's Connected");
byte[] mBytes = new byte[5];
while (handler != null && handler.Connected)
{
try
{
handler.Receive(mBytes);
// do whatever with bytes
}
catch
{
handler.Shutdown(SocketShutdown.Both);
handler.Close();
handlers.Remove(handler);
handler = null;
setToolStripStatusLabel1(handlers.Count + " Pocket PC's Connected");
}
}
if (handler != null)
{
handler.Shutdown(SocketShutdown.Both);
handler.Close();
handlers.Remove(handler);
handler = null;
setToolStripStatusLabel1(handlers.Count + " Pocket PC's Connected");
}
}
What should I be doing on desktop so the socket cleans up correctly and not just locks up
Thanks for your time.
Paul.

Little help with closing socket.
K.V.Bharath
I am not sure of details around the Socket.Connected property but I do know that the Receive call should return the number of bytes it actually read from the network. If that return value is 0 then the remote end of connection has been shut down. If your app is hanging because it indefinately iterates in the while loop with Recieve() always returning 0, you should break out of the while loop in that case.
In the case where Receive() is blocking indefinately you may want to verify (if you haven't already) that Socket.Shutdown() is, in fact, being called on the device (maybe an exception is causing execution to skip past it ).
If it isn't that, it may be helpful to try a few troubleshooting experiements such as running the relevant networking device-code on a desktop or a different device to get an indication if the problem is with the code or with the device/network stack. If you really want to investigate you might also use a network traffic sniffing tool like ethereal (www.ethereal.com) to actually see what TCP packets are going back and forth on your connection. http://www.ietf.org/rfc/rfc793.txt around page 37-38 specifies what the packet sequence should be for correctly shutting down a TCP connection.
Hope that helps,
-Noah Falk
.Net Compact Framework