Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. A problem while transferring data over the NetworkStream

A problem while transferring data over the NetworkStream

Scheduled Pinned Locked Moved C#
csharpsysadminhelpquestion
20 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SimpleData
    wrote on last edited by
    #1

    Hi, I am trying to send a big file over a TCP connection using NetworkStream and TCPClient. Both the client and the server is written in C#. There is no problem while transferring small files around the size of 300KB. My buffer size is 2KB and here is how the protocol works. -Server requests the file. -Client tells the server the size of the file, and then sends the first 2KB of it. -The server asks for another chunk of the file -like, send 2KB data, starting from 42649- -Client sends the chunk -Server asks for more, and client sends it... -When the end of file is reached, the client acknowledges the server that transfer is complete. Without looking at the code, since it is too long to be posted here, can you see any possible problems that may be causing this problem? I even tried sleeping the thread while receiving and processing the incoming chunks of data. Regards

    Richard Andrew x64R J L B 4 Replies Last reply
    0
    • S SimpleData

      Hi, I am trying to send a big file over a TCP connection using NetworkStream and TCPClient. Both the client and the server is written in C#. There is no problem while transferring small files around the size of 300KB. My buffer size is 2KB and here is how the protocol works. -Server requests the file. -Client tells the server the size of the file, and then sends the first 2KB of it. -The server asks for another chunk of the file -like, send 2KB data, starting from 42649- -Client sends the chunk -Server asks for more, and client sends it... -When the end of file is reached, the client acknowledges the server that transfer is complete. Without looking at the code, since it is too long to be posted here, can you see any possible problems that may be causing this problem? I even tried sleeping the thread while receiving and processing the incoming chunks of data. Regards

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      You haven't even said what the problem is.

      The difficult we do right away... ...the impossible takes slightly longer.

      S 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        You haven't even said what the problem is.

        The difficult we do right away... ...the impossible takes slightly longer.

        S Offline
        S Offline
        SimpleData
        wrote on last edited by
        #3

        Yeah, sorry about that. While I am trying to transfer the data, it is stuck at some point. The server doesn't make any other requests and hence, the client doesn't send any more data. I have no idea why nothing new happens.

        Richard Andrew x64R D 3 Replies Last reply
        0
        • S SimpleData

          Yeah, sorry about that. While I am trying to transfer the data, it is stuck at some point. The server doesn't make any other requests and hence, the client doesn't send any more data. I have no idea why nothing new happens.

          Richard Andrew x64R Offline
          Richard Andrew x64R Offline
          Richard Andrew x64
          wrote on last edited by
          #4

          Well, there is no way to tell why that happens just from the info you have provided.

          The difficult we do right away... ...the impossible takes slightly longer.

          1 Reply Last reply
          0
          • S SimpleData

            Yeah, sorry about that. While I am trying to transfer the data, it is stuck at some point. The server doesn't make any other requests and hence, the client doesn't send any more data. I have no idea why nothing new happens.

            Richard Andrew x64R Offline
            Richard Andrew x64R Offline
            Richard Andrew x64
            wrote on last edited by
            #5

            Run the server in the debugger, and be sure that the size of the file is communicated properly. Maybe the server thinks the file is smaller than it really is, and so it stops asking for more data.

            The difficult we do right away... ...the impossible takes slightly longer.

            S 1 Reply Last reply
            0
            • S SimpleData

              Yeah, sorry about that. While I am trying to transfer the data, it is stuck at some point. The server doesn't make any other requests and hence, the client doesn't send any more data. I have no idea why nothing new happens.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              You have no mechanism in your protocol to force either the client or server to force another request for the same data. If, after a timeout expires, the client doesn't get an ACK from the server, it should be able to try and reestablish communication, telling the server it's going to resend a block of data.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              S B 2 Replies Last reply
              0
              • D Dave Kreskowiak

                You have no mechanism in your protocol to force either the client or server to force another request for the same data. If, after a timeout expires, the client doesn't get an ACK from the server, it should be able to try and reestablish communication, telling the server it's going to resend a block of data.

                A guide to posting questions on CodeProject[^]
                Dave Kreskowiak

                S Offline
                S Offline
                SimpleData
                wrote on last edited by
                #7

                Thank you. Why haven't I thought of that. :) I will add that mechanism first thing tomorrow. By the way, even though this will probably solve my problem; there must still be a problem in my system since it is really unlikely for a package to be lost on a local TCP connection.

                1 Reply Last reply
                0
                • Richard Andrew x64R Richard Andrew x64

                  Run the server in the debugger, and be sure that the size of the file is communicated properly. Maybe the server thinks the file is smaller than it really is, and so it stops asking for more data.

                  The difficult we do right away... ...the impossible takes slightly longer.

                  S Offline
                  S Offline
                  SimpleData
                  wrote on last edited by
                  #8

                  I am aware that the information with which I've provided you is less than sufficient, but it is really hard for me to isolate the code that works on the transfer, and there is a chance that isolating it from the rest of the application will leave you with no problem to solve. The problem may as well be stemming from the structure of the rest of the application. I will check if there is a problem with the file size tomorrow, but I don't think that is the problem. When I occasionally place a breakpoint and break the program, it manages to transfer a little bit more data (around 300KB more).

                  1 Reply Last reply
                  0
                  • S SimpleData

                    Hi, I am trying to send a big file over a TCP connection using NetworkStream and TCPClient. Both the client and the server is written in C#. There is no problem while transferring small files around the size of 300KB. My buffer size is 2KB and here is how the protocol works. -Server requests the file. -Client tells the server the size of the file, and then sends the first 2KB of it. -The server asks for another chunk of the file -like, send 2KB data, starting from 42649- -Client sends the chunk -Server asks for more, and client sends it... -When the end of file is reached, the client acknowledges the server that transfer is complete. Without looking at the code, since it is too long to be posted here, can you see any possible problems that may be causing this problem? I even tried sleeping the thread while receiving and processing the incoming chunks of data. Regards

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    Some possibilities - Ignored exceptions on server - Buffer doesn't match size (read/write). Either end. - Something is pausing the server. Seems that the problem is deterministic so you can add logging and print exactly what the sizes are that you read and write each time.

                    1 Reply Last reply
                    0
                    • S SimpleData

                      Hi, I am trying to send a big file over a TCP connection using NetworkStream and TCPClient. Both the client and the server is written in C#. There is no problem while transferring small files around the size of 300KB. My buffer size is 2KB and here is how the protocol works. -Server requests the file. -Client tells the server the size of the file, and then sends the first 2KB of it. -The server asks for another chunk of the file -like, send 2KB data, starting from 42649- -Client sends the chunk -Server asks for more, and client sends it... -When the end of file is reached, the client acknowledges the server that transfer is complete. Without looking at the code, since it is too long to be posted here, can you see any possible problems that may be causing this problem? I even tried sleeping the thread while receiving and processing the incoming chunks of data. Regards

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      what you should do IMO is this: 1. improve observability: provide logging on both sides, with actual times, and relevant data, such as offset and length. 2. create a synthetic test file, so the receiver can predict and hence verify what should and is received. Here is one useful algorithm: for each block, start with the offset (yes, I mean stuff the offset in the data, I know you already send it as metadata), then add incrementing data modulo some number that isn't a power of 2 (for bytes, 255 works just fine). The net result is (1) each block is different (the offset!), and (2) yet very predictable. You'll see immediately if and when what gets received is completely wacked. 3. As you have packets already in your yet extremely simple protocol, add a checksum to the protocol, so the receiver can verify things are correct. And send an ACK (when OK) or a RESEND-REQUEST (when checksum fails or something goes wrong). 4. Refinement: don't wait for the ACK before you send the next packet; a strictly sequential process would slow things down needlessly (sender calculates checksum, sends data, waits for ACK, receiver gets data, checks checksum, sends ACK/RESEND, resulting in two periods the line isn't active at all). Instead, allow for at least 1, better a few, packets in progress, i.e. only wait for ACK for packet 1 after having send packet 1+N where N is 1 upto a few. :)

                      Luc Pattyn [My Articles] Nil Volentibus Arduum

                      The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                      Please use <PRE> tags for code snippets, they improve readability.
                      CP Vanity has been updated to V2.3

                      1 Reply Last reply
                      0
                      • S SimpleData

                        Hi, I am trying to send a big file over a TCP connection using NetworkStream and TCPClient. Both the client and the server is written in C#. There is no problem while transferring small files around the size of 300KB. My buffer size is 2KB and here is how the protocol works. -Server requests the file. -Client tells the server the size of the file, and then sends the first 2KB of it. -The server asks for another chunk of the file -like, send 2KB data, starting from 42649- -Client sends the chunk -Server asks for more, and client sends it... -When the end of file is reached, the client acknowledges the server that transfer is complete. Without looking at the code, since it is too long to be posted here, can you see any possible problems that may be causing this problem? I even tried sleeping the thread while receiving and processing the incoming chunks of data. Regards

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #11

                        Since it works for small files, and appears to be deterministic (does it always fail at the same point? or is it just more likely that larger files will fail?), it must be to do with filling an array, running out of space in a TCP buffer or something similar. If it is not so simple (i.e. it fails more on larger files, but it is not deterministic) then it is probably to do with the handshake to request more data. It is impossible to go into any more detail than that with the information available. I can only echo the advice to sprinkle your code liberally with logging statements to find out what is actually happening. Why are you doing that, anyway? TCP guarantees the ordering and transmission so you can just send the whole file on the sender end. What you are doing adds latency (2 way ping) each chunk.

                        S 1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          You have no mechanism in your protocol to force either the client or server to force another request for the same data. If, after a timeout expires, the client doesn't get an ACK from the server, it should be able to try and reestablish communication, telling the server it's going to resend a block of data.

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak

                          B Offline
                          B Offline
                          BobJanova
                          wrote on last edited by
                          #12

                          TCP guarantees that the sent packets will arrive, unless the connection drops entirely (which the OP would have noticed), so I doubt that this is the problem.

                          S D 2 Replies Last reply
                          0
                          • B BobJanova

                            TCP guarantees that the sent packets will arrive, unless the connection drops entirely (which the OP would have noticed), so I doubt that this is the problem.

                            S Offline
                            S Offline
                            SimpleData
                            wrote on last edited by
                            #13

                            The connection is still intact, it doesn't drop. The only problem is that the server stops requesting chunks.

                            1 Reply Last reply
                            0
                            • B BobJanova

                              Since it works for small files, and appears to be deterministic (does it always fail at the same point? or is it just more likely that larger files will fail?), it must be to do with filling an array, running out of space in a TCP buffer or something similar. If it is not so simple (i.e. it fails more on larger files, but it is not deterministic) then it is probably to do with the handshake to request more data. It is impossible to go into any more detail than that with the information available. I can only echo the advice to sprinkle your code liberally with logging statements to find out what is actually happening. Why are you doing that, anyway? TCP guarantees the ordering and transmission so you can just send the whole file on the sender end. What you are doing adds latency (2 way ping) each chunk.

                              S Offline
                              S Offline
                              SimpleData
                              wrote on last edited by
                              #14

                              I can't say that it is deterministic since if I pause the program at certain intervals, it can complete the transmission. If I don't pause the program at all, it stops after transferring 200-300KB. I am aware that what I am doing is just slowing down the transfer rate, but I am new to this are of programming and currently trying to develop this project to increase my experience. I will now add a timeout system to the server and request the rest of the data if nothing is incoming.

                              J 1 Reply Last reply
                              0
                              • B BobJanova

                                TCP guarantees that the sent packets will arrive, unless the connection drops entirely (which the OP would have noticed), so I doubt that this is the problem.

                                D Offline
                                D Offline
                                Dave Kreskowiak
                                wrote on last edited by
                                #15

                                I know. It works great too, on paper. In actual practice, it's not 100% accurate.

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak

                                1 Reply Last reply
                                0
                                • S SimpleData

                                  I can't say that it is deterministic since if I pause the program at certain intervals, it can complete the transmission. If I don't pause the program at all, it stops after transferring 200-300KB. I am aware that what I am doing is just slowing down the transfer rate, but I am new to this are of programming and currently trying to develop this project to increase my experience. I will now add a timeout system to the server and request the rest of the data if nothing is incoming.

                                  J Offline
                                  J Offline
                                  jschell
                                  wrote on last edited by
                                  #16

                                  SimpleData wrote:

                                  I can't say that it is deterministic since if I pause the program at certain intervals, it can complete the transmission. If I don't pause the program at all, it stops after transferring 200-300KB.

                                  As another thought - a firewall. It is throttling your throughput. When you pause it you are throttling yourself so it doesn't get involved. The same behavior could be due to some oddity in your server code though. Such as attempting to be clever by using threads for reading and writing.

                                  S 1 Reply Last reply
                                  0
                                  • J jschell

                                    SimpleData wrote:

                                    I can't say that it is deterministic since if I pause the program at certain intervals, it can complete the transmission. If I don't pause the program at all, it stops after transferring 200-300KB.

                                    As another thought - a firewall. It is throttling your throughput. When you pause it you are throttling yourself so it doesn't get involved. The same behavior could be due to some oddity in your server code though. Such as attempting to be clever by using threads for reading and writing.

                                    S Offline
                                    S Offline
                                    SimpleData
                                    wrote on last edited by
                                    #17

                                    Can you elaborate on "attempting to be clever by using threads for reading and writing"? Because it sounds like something I might have done. :)

                                    J 1 Reply Last reply
                                    0
                                    • S SimpleData

                                      Can you elaborate on "attempting to be clever by using threads for reading and writing"? Because it sounds like something I might have done. :)

                                      J Offline
                                      J Offline
                                      jschell
                                      wrote on last edited by
                                      #18

                                      To use two threads something needs to control access to the shared data. If you mess that control up then you can end up blocking or taking a large amount of time.

                                      S 1 Reply Last reply
                                      0
                                      • J jschell

                                        To use two threads something needs to control access to the shared data. If you mess that control up then you can end up blocking or taking a large amount of time.

                                        S Offline
                                        S Offline
                                        SimpleData
                                        wrote on last edited by
                                        #19

                                        Wouldn't using the "lock" directive be enough?

                                        J 1 Reply Last reply
                                        0
                                        • S SimpleData

                                          Wouldn't using the "lock" directive be enough?

                                          J Offline
                                          J Offline
                                          jschell
                                          wrote on last edited by
                                          #20

                                          No idea what you are referring to. If you are asking if you can block using lock then yes.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups