473,387 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Multithreading Socket Problem

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
2 3945
_
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Glenn Kasten | last post by:
I am wondering which operations in Python are guaranteed to be atomic in the presence of multi-threading. In particular, are assignment and reading of a dictionary entry atomic? For example,...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
1
by: richard | last post by:
OS: Winxp and Win2003 Visual Basic.NET 2003 MS-SQL Server 2000 hey all I am a newbie in vb.net but i have managed to build a simple chat server in vb.net using socket and a client connecting...
1
by: Nicolas Ghesquiere | last post by:
Hello I have a problem with my current program. The meaning of the program is to allow users to login to a server to allow them to access the internet. My program communicates with a MS isa...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
20
by: S.Mohideen | last post by:
Hi Folks, Python is praised about - me too. But at one instance it fails. It fails to behave as a true multi-threaded application. That means utilizing all the CPUs parallely in the SMP...
0
by: JYOTIRANJAN | last post by:
Hi All, My Application is developed using C on linux. It is a multithreaded application and 10 threads are waiting for message in pool.Each thread receives message continusly in a while loop from...
1
by: tsic | last post by:
Bonjour, Voici mon code que j'ai utilisé pour faire une communication entre un serveur et des clients. Pouvez vous m'aider à l'améliorer pour que l'affichage soit meilleur dans les deux coté (je...
42
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I'm currently writing a program and I've got in mind to keep it as portable as possible. In particular I want it to run on Linux and Windows, but I'm also keeping an open mind to any machine that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.