Is NetworkStream.Write() thread safe? 
Author Message
 Is NetworkStream.Write() thread safe?

Hi, Happy new year!

  When I am in implementing a P2P file upload & download
the TCP upload always fails:

C: Client;
S: Server;

Upload flow:

C: NetworkStream.Send( connect with the file name)
S: Create file and waiting for uploading
for(file size is large than 4k bytes)
{
  C: NetworkStream.Write(4k bytes buffer of the file content)
  S: NetworkStream.Read()// read the data from the network stream and write
it to the loacal file;

Quote:
}

C: close the TcpClient;

    When this code runs, the following Exception is always be thrown:
System.NullRefereceException;

    And I wonder to know whether the Write() method of NetworkStream
is thread safe or not?
    Because if it calls the following code between every Write() method
calling,
Thread.Sleep(500); The file can be upload successfully,

    Any suggestion is appreciated!

Loggy!



Sun, 19 Jun 2005 14:27:07 GMT  
 Is NetworkStream.Write() thread safe?

Quote:

> Hi, Happy new year!

>   When I am in implementing a P2P file upload & download
> the TCP upload always fails:

>     And I wonder to know whether the Write() method of NetworkStream
> is thread safe or not?
>     Because if it calls the following code between every Write() method
> calling,
> Thread.Sleep(500); The file can be upload successfully,

Loggy -

    I am not sure I understand why you think this would be a thread
issue. I am assuming that the client and server code are running in
two separate programs on two separate machines, accessing two separate
files. This would not present a thread issue.

    By placing the Sleep() call between the Write() and Read()
methods, you may be masking the real problem. When you Write() 4k
worth of data, there is no guarantee that the Read() method will
receive all 4k at once. It can easily return after reading just a few
bytes, leaving the rest for the next Read() call.

    When you make the Read() wait awhile with the Sleep() call, that
may be long enough for all 4k to be available in the buffer for the
single Read() to scoop it all up at once. But again, that is still not
100% guaranteed.

    If you send a 4k block of data, make sure the receiver received
the entire 4k block of data before continuing with the next block.
Hope this makes sense and helps.

Rich Blum
Author of "C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176



Mon, 20 Jun 2005 12:49:33 GMT  
 Is NetworkStream.Write() thread safe?



Quote:
> > Hi, Happy new year!

> >   When I am in implementing a P2P file upload & download
> > the TCP upload always fails:

> >     And I wonder to know whether the Write() method of NetworkStream
> > is thread safe or not?
> >     Because if it calls the following code between every Write() method
> > calling,
> > Thread.Sleep(500); The file can be upload successfully,

> Loggy -

>     I am not sure I understand why you think this would be a thread
> issue. I am assuming that the client and server code are running in
> two separate programs on two separate machines, accessing two separate
> files. This would not present a thread issue.

>     By placing the Sleep() call between the Write() and Read()
> methods, you may be masking the real problem. When you Write() 4k
> worth of data, there is no guarantee that the Read() method will
> receive all 4k at once. It can easily return after reading just a few
> bytes, leaving the rest for the next Read() call.

>     When you make the Read() wait awhile with the Sleep() call, that
> may be long enough for all 4k to be available in the buffer for the
> single Read() to scoop it all up at once. But again, that is still not
> 100% guaranteed.

>     If you send a 4k block of data, make sure the receiver received
> the entire 4k block of data before continuing with the next block.
> Hope this makes sense and helps.

> Rich Blum
> Author of "C# Network Programming" (Sybex)
> http://www.sybex.com/sybexbooks.nsf/Booklist/4176

  I want to know is there any other method that can guarantee the data
transmission
 between the server and client?

Thank you!

Loggy



Mon, 20 Jun 2005 16:20:17 GMT  
 Is NetworkStream.Write() thread safe?
You should program it asynchronously instead of all this sleeping stuff. You
may evt. purchase the new relevant book on network programming from
Microsoft Press, then you can just solve your problem and proceed to the
next tasks.

Best regards,

Henrik Dahl


Quote:





> > > Hi, Happy new year!

> > >   When I am in implementing a P2P file upload & download
> > > the TCP upload always fails:

> > >     And I wonder to know whether the Write() method of NetworkStream
> > > is thread safe or not?
> > >     Because if it calls the following code between every Write()
method
> > > calling,
> > > Thread.Sleep(500); The file can be upload successfully,

> > Loggy -

> >     I am not sure I understand why you think this would be a thread
> > issue. I am assuming that the client and server code are running in
> > two separate programs on two separate machines, accessing two separate
> > files. This would not present a thread issue.

> >     By placing the Sleep() call between the Write() and Read()
> > methods, you may be masking the real problem. When you Write() 4k
> > worth of data, there is no guarantee that the Read() method will
> > receive all 4k at once. It can easily return after reading just a few
> > bytes, leaving the rest for the next Read() call.

> >     When you make the Read() wait awhile with the Sleep() call, that
> > may be long enough for all 4k to be available in the buffer for the
> > single Read() to scoop it all up at once. But again, that is still not
> > 100% guaranteed.

> >     If you send a 4k block of data, make sure the receiver received
> > the entire 4k block of data before continuing with the next block.
> > Hope this makes sense and helps.

> > Rich Blum
> > Author of "C# Network Programming" (Sybex)
> > http://www.sybex.com/sybexbooks.nsf/Booklist/4176

>   I want to know is there any other method that can guarantee the data
> transmission
>  between the server and client?

> Thank you!

> Loggy



Tue, 21 Jun 2005 02:44:09 GMT  
 Is NetworkStream.Write() thread safe?

Quote:

> You should program it asynchronously instead of all this sleeping stuff. You
> may evt. purchase the new relevant book on network programming from
> Microsoft Press, then you can just solve your problem and proceed to the
> next tasks.

Loggy -

    TCP will guarantee (barring any catastrophic network problems)
that your data will arrive at the other end error-free and in the
proper order. It is your program's job to ensure that the same number
of bytes you send are received at the other end.

    Whether you use synchronous or asynchronous sockets, this usually
involves checking the data for either boundary markers or size. For a
mass data transfer, usually its easier to just get the size of the
data being sent, send that value to the receiver first, then allow the
receiver to loop through the Receive() method until it's gathered all
the data.

    Henrik is correct. If you are writing a serious network
application, I would recommend buying some form of network programming
book, either mine, the Wrox "Professional .NET Network Programming"
book, the Microsoft Press "Windows Network Programming" book, or the
infamous Richard Stevens' "Unix Network Programming" classic. A little
bit of TCP theory goes a long way. Good luck in your network
programming.

Rich Blum
Author of "C# Network Programming" (Sybex)
http://www.sybex.com/sybexbooks.nsf/Booklist/4176



Tue, 21 Jun 2005 20:17:08 GMT  
 Is NetworkStream.Write() thread safe?
Thans for all of your suggestions!

loggy!

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Tue, 21 Jun 2005 20:36:12 GMT  
 Is NetworkStream.Write() thread safe?
Hello,

... and if something which supports transfer of structures, including some
boundary checking should be done, ISO 8824 and ISO 8825 is the way to go of
course.

Henrik Dahl



Quote:
> > You should program it asynchronously instead of all this sleeping stuff.
You
> > may evt. purchase the new relevant book on network programming from
> > Microsoft Press, then you can just solve your problem and proceed to the
> > next tasks.

> Loggy -

>     TCP will guarantee (barring any catastrophic network problems)
> that your data will arrive at the other end error-free and in the
> proper order. It is your program's job to ensure that the same number
> of bytes you send are received at the other end.

>     Whether you use synchronous or asynchronous sockets, this usually
> involves checking the data for either boundary markers or size. For a
> mass data transfer, usually its easier to just get the size of the
> data being sent, send that value to the receiver first, then allow the
> receiver to loop through the Receive() method until it's gathered all
> the data.

>     Henrik is correct. If you are writing a serious network
> application, I would recommend buying some form of network programming
> book, either mine, the Wrox "Professional .NET Network Programming"
> book, the Microsoft Press "Windows Network Programming" book, or the
> infamous Richard Stevens' "Unix Network Programming" classic. A little
> bit of TCP theory goes a long way. Good luck in your network
> programming.

> Rich Blum
> Author of "C# Network Programming" (Sybex)
> http://www.sybex.com/sybexbooks.nsf/Booklist/4176



Tue, 21 Jun 2005 20:55:15 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. NetworkStream.Write writes "sometimes"

2. Writing thread safe collections in VC++6

3. ANN: sigslot - C++ Portable, Thread-Safe, Type-Safe Signal/Slot Library

4. Writing to browser using NetworkStream

5. Using a non-thread-safe library with threads?

6. Thread functions are thread-safe?

7. how do i create thread safe worker thread

8. NetworkStream thread safety?

9. Proper/safe way to write to a logfile

10. Why OleDbCommand objects are not thread safe?

11. Thread-safe DataTable.Rows.Add

12. Is ADO really thread-safe?

 

 
Powered by phpBB® Forum Software