Hi,
I am trying to implement a ping client that is multithreading. The
approach I have used is to create a ping class which I instantiate
from the main thread and which contains a method "SendPingAsync" that
calls the Send method using an asynchronous delegate, so as to queue
the send method on the threadpool.
Within the send method
public void Send(string host, Guid TestGuid)
I use the Socket.SendTo to send the ping request.
if ((nBytes = socket.SendTo(sendbuf, PacketSize, 0, epServer)) ==
SOCKET_ERROR)
and then call the Socket.BeginReceive
m_asynResult = socket.BeginReceive(theSocPkt.dataBuffer,0,256
,0,pfnCallBack,theSocPkt);
to which I attach the theSocPkt object (contains a refrence to the
socket, the buffer to be filled and some other information).
On reception of data on the socket the callback method referenced by
pfnCallBack
public void OnDataReceived(IAsyncResult asyn)
is called and I cast the IAsyncResult asyn object to the CSocketPacket
class.
CSocketPacket theSockId = (CSocketPacket)asyn.AsyncState ;
from this I recover the reference to the originating socket and to the
data buffer theSocPkt.dataBuffer
The problem that occurs is that the data found in the data buffer
theSocPkt.dataBuffer (icmp reply packet) does not correspond to the
socket theSocPkt.thisSocket, but to a socket running on an another
thread. I demonstate this by placing a Guid in the ping packet sent,
and attaching the same Guid to the CSocketPacket type object. On
comparing the two on data reception, often they do not match.
Controlling all the hashcodes of objects in the CSocketPacket object
on datareception, I find no discrepancies. Only that data in the
databuffer does not correspond to the socket.
Does anyone have any idea of what is happening here??? Am I breaching
thread safety in some way, or is this a bug in the Socket object?? Any
Ideas????
I am using
Microsoft Development Environment 2003 Version 7.1.3088 and
Microsoft .Net Framework 1.1 Version 1.1.4322
Any help will be much appreciated, as I no longer know where to bang
my head...
Regards
Rekkie