Author |
Message |
Henrik Dah #1 / 25
|
 socket.Send and socket.Receive at the same time
Hello! May socket.Send and socket.Receive for the same socket object be invoked at the same time by two different threads? Best regards, Henrik Dahl
|
Sun, 09 Jan 2005 03:12:45 GMT |
|
 |
Greg Ewin #2 / 25
|
 socket.Send and socket.Receive at the same time
Henrik, no, you can't send and receive from one socket at the same time. You'll have to use a different port/socket if you need to go at the same time or use thread synchronization (check out Monitor, Lock, Mutex) to make sure one or the other is happening but not both. -- Greg http://www.claritycon.com/
Quote: > Hello! > May socket.Send and socket.Receive for the same socket object be invoked at > the same time by two different threads? > Best regards, > Henrik Dahl
|
Sun, 09 Jan 2005 03:17:52 GMT |
|
 |
Nicholas Paldino [.NET/C# MVP #3 / 25
|
 socket.Send and socket.Receive at the same time
Henrik, I don't think you can. As far as I know, making calls on a socket that is created on another thread gives unpredictable results. Hope this helps. -- - Nicholas Paldino [.NET MVP]
Quote: > Hello! > May socket.Send and socket.Receive for the same socket object be invoked at > the same time by two different threads? > Best regards, > Henrik Dahl
|
Sun, 09 Jan 2005 03:20:26 GMT |
|
 |
Henrik Dah #4 / 25
|
 socket.Send and socket.Receive at the same time
Greg, OK, but how do I then program that I may basically always receive something from the server and also be able to send something to it from time to time? Henrik
Quote: > Henrik, no, you can't send and receive from one socket at the same time. > You'll have to use a different port/socket if you need to go at the same > time or use thread synchronization (check out Monitor, Lock, Mutex) to make > sure one or the other is happening but not both. > -- > Greg > http://www.claritycon.com/
> > Hello! > > May socket.Send and socket.Receive for the same socket object be invoked > at > > the same time by two different threads? > > Best regards, > > Henrik Dahl
|
Sun, 09 Jan 2005 04:02:48 GMT |
|
 |
Henrik Dah #5 / 25
|
 socket.Send and socket.Receive at the same time
Nicholas, OK, but how do I then program that I may basically always receive something from the server and also be able to send something to it from time to time? Henrik
Quote: > Henrik, > I don't think you can. As far as I know, making calls on a socket that > is created on another thread gives unpredictable results. > Hope this helps. > -- > - Nicholas Paldino [.NET MVP]
> > Hello! > > May socket.Send and socket.Receive for the same socket object be invoked > at > > the same time by two different threads? > > Best regards, > > Henrik Dahl
|
Sun, 09 Jan 2005 04:03:04 GMT |
|
 |
Chad Myer #6 / 25
|
 socket.Send and socket.Receive at the same time
Well, you can't send and receive at the EXACT SAME time, but you can send and receive. So what you need to do in your threads is, when you're about to read or write, lock on an object (mutex, etc) read or write, then release the lock. If another thread tries to read/write, it will block on the lock call until the other thread is done and then perform its operation. -c
Quote: > Nicholas, > OK, but how do I then program that I may basically always receive something > from the server and also be able to send something to it from time to time? > Henrik
wrote
> > Henrik, > > I don't think you can. As far as I know, making calls on a socket > that > > is created on another thread gives unpredictable results. > > Hope this helps. > > -- > > - Nicholas Paldino [.NET MVP]
> > > Hello! > > > May socket.Send and socket.Receive for the same socket object be invoked > > at > > > the same time by two different threads? > > > Best regards, > > > Henrik Dahl
|
Sun, 09 Jan 2005 04:15:18 GMT |
|
 |
Zane Thomas [.NET MV #7 / 25
|
 socket.Send and socket.Receive at the same time
Quote:
>May socket.Send and socket.Receive for the same socket object be invoked at >the same time by two different threads?
I hope so, I've done it several times! I'm not sure why the others in this thread think it should be a problem since it is fairly common in blocking scenarios for one thread to be a reader while another is a writer. -- *--------={ Fine Art for .NET }=--------*
*---------------------------------------* Turn on, tune in, download.
|
Sun, 09 Jan 2005 04:19:35 GMT |
|
 |
Zane Thomas [.NET MV #8 / 25
|
 socket.Send and socket.Receive at the same time
Quote:
>So what you need to do in your threads is, when you're about to >read or write, lock on an object (mutex, etc) read or write, then >release the lock.
I'm not clear on why everyone thinks there's an issue with threading and sockets. Do you have specific information indicating there's some problem in the BCL with simultaneously executing blocking reads and writes on a single socket or are you speaking from general principles? A socket is not a file. :-) -- *--------={ Fine Art for .NET }=--------*
*---------------------------------------* Turn on, tune in, download.
|
Sun, 09 Jan 2005 04:22:20 GMT |
|
 |
Henrik Dah #9 / 25
|
 socket.Send and socket.Receive at the same time
Yes, but as the server takes the initiative to send something to the client, I suppose that the client should basically receive in order to have the server to succeed, and as the client would like to send the client should be able to do that. How do I make the client in such a way, that the server may succeed in sending to the client when it wants and the client may succeed in sending to the server when it wants?
Quote: > Well, you can't send and receive at the EXACT SAME time, but you > can send and receive. > So what you need to do in your threads is, when you're about to > read or write, lock on an object (mutex, etc) read or write, then > release the lock. > If another thread tries to read/write, it will block on the lock > call until the other thread is done and then perform its operation. > -c
> > Nicholas, > > OK, but how do I then program that I may basically always receive > something > > from the server and also be able to send something to it from time to > time? > > Henrik
> wrote
> > > Henrik, > > > I don't think you can. As far as I know, making calls on a socket > > that > > > is created on another thread gives unpredictable results. > > > Hope this helps. > > > -- > > > - Nicholas Paldino [.NET MVP]
> > > > Hello! > > > > May socket.Send and socket.Receive for the same socket object be > invoked > > > at > > > > the same time by two different threads? > > > > Best regards, > > > > Henrik Dahl
|
Sun, 09 Jan 2005 04:22:22 GMT |
|
 |
Henrik Dah #10 / 25
|
 socket.Send and socket.Receive at the same time
Thanks, that's my point of course.
Quote:
> >May socket.Send and socket.Receive for the same socket object be invoked at > >the same time by two different threads? > I hope so, I've done it several times! I'm not sure why the others in this > thread think it should be a problem since it is fairly common in blocking > scenarios for one thread to be a reader while another is a writer. > -- > *--------={ Fine Art for .NET }=--------*
> *---------------------------------------* > Turn on, tune in, download.
|
Sun, 09 Jan 2005 04:25:51 GMT |
|
 |
Zane Thomas [.NET MV #11 / 25
|
 socket.Send and socket.Receive at the same time
Quote:
>How do I make the client in such a way, that the server may >succeed in sending to the client when it wants and the client may succeed in >sending to the server when it wants?
I think the other people here are wrong. I seem to recall having done blocking reads and writes on a single socket more than once in the past, although I usually prefer to use asynchronous methods. I have some sample projects and powerpoint slides at www.abderaware.com - look in the Free Stuff folder. You can probably adapt code from one or more of the sample projects for whatever it is you're trying to do. -- *--------={ Fine Art for .NET }=--------*
*---------------------------------------* Turn on, tune in, download.
|
Sun, 09 Jan 2005 04:35:56 GMT |
|
 |
Chad Myer #12 / 25
|
 socket.Send and socket.Receive at the same time
Quote:
> >So what you need to do in your threads is, when you're about to > >read or write, lock on an object (mutex, etc) read or write, then > >release the lock. > I'm not clear on why everyone thinks there's an issue with threading and > sockets. Do you have specific information indicating there's some problem in the > BCL with simultaneously executing blocking reads and writes on a single socket > or are you speaking from general principles? A socket is not a file. :-)
I was goind to say that I though socket supported simultaneous reads and writes, but everyone seemed to agree that it didn't, so I figured I must've been wrong and that I got lucky my program didn't crash =) -c
|
Sun, 09 Jan 2005 04:36:39 GMT |
|
 |
Henrik Dah #13 / 25
|
 socket.Send and socket.Receive at the same time
Zane, Thank you very much! Henrik
Quote:
> >How do I make the client in such a way, that the server may > >succeed in sending to the client when it wants and the client may succeed in > >sending to the server when it wants? > I think the other people here are wrong. I seem to recall having done blocking > reads and writes on a single socket more than once in the past, although I > usually prefer to use asynchronous methods. I have some sample projects and > powerpoint slides at www.abderaware.com - look in the Free Stuff folder. You > can probably adapt code from one or more of the sample projects for whatever it > is you're trying to do. > -- > *--------={ Fine Art for .NET }=--------*
> *---------------------------------------* > Turn on, tune in, download.
|
Sun, 09 Jan 2005 04:38:56 GMT |
|
 |
Zane Thomas [.NET MV #14 / 25
|
 socket.Send and socket.Receive at the same time
On Tue, 23 Jul 2002 15:36:39 -0500, "Chad Myers" Quote:
>everyone seemed to agree
Sometimes it's better to be a rebel. ;-) -- *--------={ Fine Art for .NET }=--------*
*---------------------------------------* Turn on, tune in, download.
|
Sun, 09 Jan 2005 04:41:38 GMT |
|
 |
Henrik Dah #15 / 25
|
 socket.Send and socket.Receive at the same time
Zane, One more question. Let's assume I do it asynchronously using socket.BeginSend. Do you know, it I can invoke a number of socket.BeginSend invokations after each other without waiting for the previous one to finish or does it mix up the sequence of bytes actually sent. Best regards, Henrik
Quote:
> >How do I make the client in such a way, that the server may > >succeed in sending to the client when it wants and the client may succeed in > >sending to the server when it wants? > I think the other people here are wrong. I seem to recall having done blocking > reads and writes on a single socket more than once in the past, although I > usually prefer to use asynchronous methods. I have some sample projects and > powerpoint slides at www.abderaware.com - look in the Free Stuff folder. You > can probably adapt code from one or more of the sample projects for whatever it > is you're trying to do. > -- > *--------={ Fine Art for .NET }=--------*
> *---------------------------------------* > Turn on, tune in, download.
|
Sun, 09 Jan 2005 04:45:42 GMT |
|
|