Is NetworkStream.Write() thread safe?
Author |
Message |
Logg #1 / 7
|
 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 |
|
 |
Rich Bl #2 / 7
|
 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 |
|
 |
Logg #3 / 7
|
 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 |
|
 |
Henrik Dah #4 / 7
|
 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 |
|
 |
Rich Bl #5 / 7
|
 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 |
|
 |
loggy yy #6 / 7
|
 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 |
|
 |
Henrik Dah #7 / 7
|
 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 |
|
|
|