Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# socket
Dear All:
 
Code:
 
While(tcpClient.Available >0)
{
  // todo: read contents
    Thread.Sleep(0);
}
 
The problem is
 
when call Thread.Sleep(0).
 
the tcpClient will be disposed.
 
the tcpClient.Available will throw exception.
 

Thanks.
Posted 20-Aug-12 6:57am
Sam_lin313
Edited 20-Aug-12 7:17am
Wes Aday94.3K
v2
Comments
Wes Aday at 20-Aug-12 12:01pm
   
That makes no sense at all. Calling Sleep will not dispose your connection for you.
Sergey Alexandrovich Kryukov at 20-Aug-12 12:31pm
   
Basically, you are right, but it might indirectly manifest already existing problem in a wrong code, if some race condition is present. Please see the explanation in my answer.
--SA
ridoy at 20-Aug-12 12:24pm
   
Question isn't clear..what you need to do here?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The problem, if any, is somewhere else. Yes, I'm 100% sure.
 
I little clarification: Thread.Sleep(0) is not equivalent to "no operation" or the code where this line is missing. To best of my knowledge, it takes some time for the method call, it cause switching the calling thread off and preemption by some other thread (perhaps one-time), and the actual delay is some random delay more then zero. It could not possibly disrupt operation of your thread.
 
However, it can indirectly manifest some problem with some probability, if you whole code design is wrong due to race condition you might embed in your design. I prefer more descriptive term "incorrect dependency on the time of execution". Please see:
http://en.wikipedia.org/wiki/Race_condition[^].
 
To start the investigation, you should learn about threading and parallel design from this point of view. You did not provide us enough information to help you.
 
—SA
  Permalink  
v2
Comments
@amitgajjar at 21-Aug-12 1:03am
   
5+ thanks for Thread.Sleep(0) explanation.
Sergey Alexandrovich Kryukov at 21-Aug-12 14:19pm
   
Thank you, Amit.
--SAq
Kuthuparakkal at 21-Aug-12 1:32am
   
great solution... so much to learn from you SA
Sergey Alexandrovich Kryukov at 21-Aug-12 14:19pm
   
Thank you very much for your nice words.
--SA
Sam_lin at 21-Aug-12 11:55am
   
i know what you mean, when call Thread.Sleep(0),There is a chance for opration system to switch another thread. That's said, another thread will close the socket.
 
i will check the whole code. Thanks.
 
But i want to make sure that "Thread.Sleep(0)" dosen't dispose any resource,right?
 
Thanks.
Sergey Alexandrovich Kryukov at 21-Aug-12 14:30pm
   
Right, it cannot change anything in resource or whatever else, can only affect timing. If this call effects any operation logically, it means there is a race condition. Imagine that one thread adds 3 to some integer object, and another one multiplies the same object by 4. Even though if everything is correctly synchronized, the final result depends on which thread comes first, that is, the result is unpredictable. This is the race condition. It's a good idea to work with every socket or a network stream in one thread, but one thread can and usually should handle just one socket.
 
It does not mean you cannot share sockets between threads at all. On server end, the typical schema is: one thread is listening for new connections, and one more (or few threads) is working with network streams through the sockets, so the socket collection should be interlocked (using .NET lock, or something).
 
About closing sockets: I developed the practice to close sockets only on exceptions (+ on exit). Say, when a client terminates, the server side gets exception while trying to send data to the remote socket. Here is why: unpredicted closing of connections are unavoidable in principle (if a client computer looses power accidentally, you will get the same exception), so you can use unified closing mechanism for all connections, without the need for "graceful" disconnection. However, if it is essential to distinguish between graceful and accidental losses of connection, add some graceful mechanism in application-level protocol...
 
--SA
Sergey Alexandrovich Kryukov at 21-Aug-12 14:31pm
   
Anyway, if you agree it makes sense, please accept the answer formally (green button) -- thanks.
--SA
Sam_lin at 21-Aug-12 22:50pm
   
Thanks,SA
Sergey Alexandrovich Kryukov at 22-Aug-12 0:49am
   
You are very welcome.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 21 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100