By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,920 Members | 1,923 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,920 IT Pros & Developers. It's quick & easy.

Multithreading Socket Problem

P: n/a
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
Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
_
On 10 Feb 2004 04:17:55 -0800, re******@excite.com (Rekkie) wrote:
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.
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 15 '05 #2

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.