Question how to send file with progressbar via TcpClient ?

elmagek

Member
Joined
Oct 5, 2021
Messages
8
Programming Experience
1-3
hi
can any one help me with how to send file with progressbar via TcpClient
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
There's nothing built into the TcpClient class for that. You call GetStream and then you write to the Stream. It's up to you to read the file in chunks and write those chunks to the Stream, performing the progress calculation yourself as you go and displaying the result.
 

elmagek

Member
Joined
Oct 5, 2021
Messages
8
Programming Experience
1-3
how to do that with this code
edit this code with progressbar please
vb.net:
 Dim ns As NetworkStream
                 ns = cli.GetStream
                 Dim bytes() As Byte = New Byte(cli.ReceiveBufferSize - 1) {}
                Dim i As Integer = 0
                 Do While cli.GetStream.DataAvailable = True
                    bytes(i) = cli.GetStream.ReadByte()
                    i += 1
                    'msg = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                    'Form3.ProgressBar1.Value = cli.Available / i * 100
                Loop
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
That code is receiving data from a TcpClient, not sending data to one. You said that you want to send data. Which is it? You need to be clear in what question you're actually asking.
 

elmagek

Member
Joined
Oct 5, 2021
Messages
8
Programming Experience
1-3
That code is receiving data from a TcpClient, not sending data to one. You said that you want to send data. Which is it? You need to be clear in what question you're actually asking.
i am sorry
but i mean i will send by client to server
server and client work on my pc for test
so i want send file by client
and receved it by server and progressbar will be in server form
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
I'm at my desk, doing the work that I get paid for. If you expect someone to be at your beck and call then you probably ought to think about paying someone, rather than using public forums where people volunteer their time. I will address individual issues if and when I have the time and the inclination.
 

elmagek

Member
Joined
Oct 5, 2021
Messages
8
Programming Experience
1-3
I'm at my desk, doing the work that I get paid for. If you expect someone to be at your beck and call then you probably ought to think about paying someone, rather than using public forums where people volunteer their time. I will address individual issues if and when I have the time and the inclination.
Thank you, but I'm a student, I'm still learning, and I can't pay anyone
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
I wasn't really suggesting that you should pay someone. My point was that you're not paying anyone here so you should probably be a little less demanding of people who are volunteering their time to help a stranger.

Anyway, particularly given that you're a student, my main concern is that you learn as much as possible and the best way to do that is to do as much as possible yourself. That doesn't mean that I won't help but I'm not going to just write your code for you. I will help you understand the principles involved so that you can write as much code as possible for yourself.

As for the question, in order to be able to report progress, you need to be able to calculate that progress first. You can't magically calculate how much of a file you have downloaded and how much is yet to be downloaded if you don't know how big the file is in the first place. Are you transmitting that information first? If not then there's nothing to be done, because you can't calculate a proportion of an unknown number.
 

elmagek

Member
Joined
Oct 5, 2021
Messages
8
Programming Experience
1-3
Ican send file from client before file data
Or what best way for that and sorry for my language it's bad because my main language is arabic
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
It's really up to you to decide. Everything that the server receives is just going to be bytes so it is up to you to determine an appropriate protocol so that the server knows how to interpret that data. For instance, you might start every communication with a number that represents the type of the communication. The number 1 might represent a file upload, so when the server reads the number 1, it knows that it needs to interpret the subsequent data in an appropriate manner for an uploaded file. You might want to follow that with a number representing the size of the file. You will presumably need to communicate the file type somehow as well. You can then send the file contents and the server will know exactly how many bytes to read, so it will be able to calculate the progress as it goes.

Hopefully you're getting a feel for the fact that software development is more than just writing code. You need to spend some time designing the software before you implement it, working out the logic before you write the code to perform it. Think about the sorts of steps you would need to perform in a manual process, because the application will have to perform mostly the same steps.
 

elmagek

Member
Joined
Oct 5, 2021
Messages
8
Programming Experience
1-3
i got source code but send file with error bytes
if file have 40 mb after sent is 237 mb
if you can fix this
 

Attachments

  • File Transfer (update).zip
    50.5 KB · Views: 0
Top Bottom