Multithreading Socket Problem

R

Rekkie

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
 
?

_

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);

You should be using Socket.BeginReceiveFrom ... UDP sockets do not have
connections and so you have to look at the address BeginReceiveFrom reports to
know which send is being replied to.
 
R

Rekkie

Hi Zane,

Thanx for the reply, I was starting to have a funny suspicion that it
was something along those lines....

Many times when you're working on a project you loose sight of what
you're doing and need to get put back on track hehehe.

Thank you much appreciated.

Rekkie
 

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