PC Review


Reply
Thread Tools Rate Thread

Socket Send and Receive Thread Safety

 
 
=?Utf-8?B?Tml6YXI=?=
Guest
Posts: n/a
 
      21st Feb 2005
Is it safe for one thread to call Socket.Receive and another to call
Socket.Send on the same Socket at the same time?

The documentation says that instance members are not guaranteed to be thread
safe, but I'm not sure whether this combination is or not.

I've also heard that the send and receive buffers are disparate and can be
accessed simultaneously.
 
Reply With Quote
 
 
 
 
Ivar
Guest
Posts: n/a
 
      21st Feb 2005

Hi,

Just think about it:
You can do it, it's not leagal but how you gurantee that data is sent to
socket in right order ?

For example:

you need to send:
aaaaaa
bbbbb

You call aaaaaa from 1 thread and bbbbbb on another:
what may happen:
aaaaaa
bbbbb
----- all as you excpect

bbbbb
aaaaa
This may happen if bbbbb thread finishes first.

For recieve, you never can be sure what is recieved by one thread and
another.
may happen following situation:
aaaaabbb
bbbb


"Nizar" <(E-Mail Removed)> wrote in message
news:E365972C-6186-4705-A734-(E-Mail Removed)...
> Is it safe for one thread to call Socket.Receive and another to call
> Socket.Send on the same Socket at the same time?
>
> The documentation says that instance members are not guaranteed to be
> thread
> safe, but I'm not sure whether this combination is or not.
>
> I've also heard that the send and receive buffers are disparate and can be
> accessed simultaneously.



 
Reply With Quote
 
=?Utf-8?B?Tml6YXI=?=
Guest
Posts: n/a
 
      21st Feb 2005
I'm not concerned about simultaneous sends or receives. Let me see if I can
explain it better:

Is it okay to be sending data over a Socket at the same time as data is
being received on it? For example, have one thread that only sends data and
another thread that only reads data without synchronizing access to the
shared Socket. So, one thread calls Socket.Receive at the same time as
another thread calls Socket.Send. Is this okay or not?

"Ivar" wrote:

>
> Hi,
>
> Just think about it:
> You can do it, it's not leagal but how you gurantee that data is sent to
> socket in right order ?
>
> For example:
>
> you need to send:
> aaaaaa
> bbbbb
>
> You call aaaaaa from 1 thread and bbbbbb on another:
> what may happen:
> aaaaaa
> bbbbb
> ----- all as you excpect
>
> bbbbb
> aaaaa
> This may happen if bbbbb thread finishes first.
>
> For recieve, you never can be sure what is recieved by one thread and
> another.
> may happen following situation:
> aaaaabbb
> bbbb
>
>
> "Nizar" <(E-Mail Removed)> wrote in message
> news:E365972C-6186-4705-A734-(E-Mail Removed)...
> > Is it safe for one thread to call Socket.Receive and another to call
> > Socket.Send on the same Socket at the same time?
> >
> > The documentation says that instance members are not guaranteed to be
> > thread
> > safe, but I'm not sure whether this combination is or not.
> >
> > I've also heard that the send and receive buffers are disparate and can be
> > accessed simultaneously.

>
>
>

 
Reply With Quote
 
Ivar
Guest
Posts: n/a
 
      22nd Feb 2005

> Is this okay or not?

Yes, then no problem.


"Nizar" <(E-Mail Removed)> wrote in message
news:FB1871A0-F98A-43C7-99D9-(E-Mail Removed)...
> I'm not concerned about simultaneous sends or receives. Let me see if I
> can
> explain it better:
>
> Is it okay to be sending data over a Socket at the same time as data is
> being received on it? For example, have one thread that only sends data
> and
> another thread that only reads data without synchronizing access to the
> shared Socket. So, one thread calls Socket.Receive at the same time as
> another thread calls Socket.Send. Is this okay or not?
>
> "Ivar" wrote:
>
>>
>> Hi,
>>
>> Just think about it:
>> You can do it, it's not leagal but how you gurantee that data is sent to
>> socket in right order ?
>>
>> For example:
>>
>> you need to send:
>> aaaaaa
>> bbbbb
>>
>> You call aaaaaa from 1 thread and bbbbbb on another:
>> what may happen:
>> aaaaaa
>> bbbbb
>> ----- all as you excpect
>>
>> bbbbb
>> aaaaa
>> This may happen if bbbbb thread finishes first.
>>
>> For recieve, you never can be sure what is recieved by one thread and
>> another.
>> may happen following situation:
>> aaaaabbb
>> bbbb
>>
>>
>> "Nizar" <(E-Mail Removed)> wrote in message
>> news:E365972C-6186-4705-A734-(E-Mail Removed)...
>> > Is it safe for one thread to call Socket.Receive and another to call
>> > Socket.Send on the same Socket at the same time?
>> >
>> > The documentation says that instance members are not guaranteed to be
>> > thread
>> > safe, but I'm not sure whether this combination is or not.
>> >
>> > I've also heard that the send and receive buffers are disparate and can
>> > be
>> > accessed simultaneously.

>>
>>
>>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread safety of DataTable class - Filling on background thread OK? Alan Cobb Microsoft ADO .NET 9 1st Jul 2006 08:27 AM
strange problems with Socket.Send/Socket.Receive Amadej Microsoft C# .NET 1 9th Aug 2004 01:01 PM
Socket class and thread safety =?Utf-8?B?RXRpZW5uZSBCb3VjaGVy?= Microsoft Dot NET Framework 0 21st Apr 2004 07:26 AM
Socket class and thread safety Etienne Boucher Microsoft Dot NET Framework 0 19th Apr 2004 12:04 AM
Stopping Thread Blocking on Socket.Receive Bruce Vander Werf Microsoft C# .NET 2 3rd Oct 2003 06:47 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:48 PM.