Multithreading Socket Problem

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Rekkie (rekcut11_at_excite.com)
Date: 02/10/04


Date: 10 Feb 2004 04:17:55 -0800

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



Relevant Pages

  • Re: not able to create socket ,coz of insufficient user rights
    ... including Ping, the ping function is openning Raw ... Socket is in System.Net.Sockets Namespace not in ... >> failing, I am not able to debug this code. ...
    (microsoft.public.security)
  • ICMP Ping ohne Admin
    ... Nun mache ich eine Socket Verbindung vom Type ICMP auf. ... Klappt auch wünderschon aber wenn man mit eingeschränkten Benutzerrechten arbeitet wird der Socket anfrage geblockt und die Fehlermeldung "" ausgegeben. ... Rein aus Interesse, wenn ich die Ping Klasse aus .NET 2.0 nehme, gibt es dann auch dieses Problem? ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: Sockets of domain controller cant be accessed by domain users
    ... > utility 'ping' and my utility 'ping'? ... > I run them under the same domain user account and they behave differently. ... > I assume they both make use raw socket and work essentially the same way. ... I try to run ping utility to ping the box. ...
    (microsoft.public.win2000.security)
  • Re: Ping ohne Admin-Account
    ... // Initialize a Socket of Type ICMP ... Bevor ich den Browser mit einer speziellen URL anwerfe moechte ich die Netzverbindung zum entsprechenden Server einfach per Ping ... Was auch ohne Probleme mit Adminrechten funkt. ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: not the time for the async method?
    ... I'm using the ping class in the system.net.networkinformation namespace. ... And in order to stop it and need a variable that is accessible to both the calling function and the callback function to track when the loop should stop, and that variable should be locked whenever accessed for read/write with the lock statement... ...
    (microsoft.public.dotnet.languages.csharp)