Results 1 to 5 of 5

Thread: File Transfer

  1. #1

    Thread Starter
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    File Transfer

    I made a multi-user chat program in VB.Net using the WinSock 6.0 control.

    I now need to be able to transfer pictures from client to server and view them on the server. I do this by converting the Image to a Base64String, then send the string over the WinSock TCP connection.

    However, a single packet cannot nearly handle the entire picture. Data is cut off after about 4337 characters, and the entire picture is usually at least 250,000 characters.

    I made this function that sends file text over winsock, packet by packet.
    Code:
    Public Sub SendFileText(ByVal text As String, Optional ByVal interval As Integer = 3000)
            Dim cPack As String = ""
            If text.Length < interval Then
                Sock.SendData("SS|" & text)
            Else
                Do Until text = ""
                    cPack = text.Substring(0, interval)
                    Sock.SendData("SS|" & cPack)
                    text = text.Substring(interval)
                Loop
            End If
    End Sub
    I was hoping this would send the file text in parts, so i could then append them on the Server side and rebuild a file.
    However, this code behaves the same way as sending the entire file at once does.

    I've also noticed that when you try to send multiple packets quickly over a winsock connection that they append to each other, which could explain why this approach isnt working.

    Is there any way i can transfer a picture from client to server?

    Thanks,
    Philly0494

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: File Transfer

    It is important to z that you aren't working directly with packets when using TCP/IP.
    You should think of the connection as a pipeline between two hosts through which data streams in both directions, forget about packets and all that. Because when you send data from your application, the TCP and IP protocols will create packets and segments as they see fit, and one "send call" does not equal to one sent packet... nor does one "receive call" receive one packet...it will return everything that has arrived since the last time it checked for data...which explains the "packet appending" thing you describe.

    So does this Winsock component not provide a method for sending byte data? Only strings? This is one of the downsides to using the Winsock component in .NET applications, you can not adjust it to your needs, but must figure out dodgy work-arounds because of it. If you have the possibility to do so, I would strongly advice you to use the native .NET classes TcpClient/TcpListener. However if not, we'll just have to figure out a dodgy workaround

    So you're saying that if you try to send the entire picture (as a string), and then convert it back to bytes on the receiving end, it will not contain all the bytes?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    Re: File Transfer

    Quote Originally Posted by Atheist View Post
    So you're saying that if you try to send the entire picture (as a string), and then convert it back to bytes on the receiving end, it will not contain all the bytes?
    no, i have done checks on the client/server side to make sure that the following is true

    1. Picture file on client is 268kb
    2. Picture String on client is 283,000 characters
    ~Client Sends String to Server
    3. String data received by server is 4337 characters (about 5kb)

    and i know the conversion from image-string and vice versa isnt the problem, ive used that many times before.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: File Transfer

    With a TcpClient this would be very simple:
    vb.net Code:
    1. myTcpClient.GetStream().Write(IO.File.ReadAllBytes("file path here"))
    to send the contents of an image file or, if you already have an Image object:
    vb.net Code:
    1. myImage.Save(myTcpClient.GetStream(), ImageFormat.Jpeg)
    If you're going to use .NET then you should use .NET. If you're going to cling to the VB6 way of doing things then you may as well stick with VB6.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member Philly0494's Avatar
    Join Date
    Apr 2008
    Posts
    485

    Re: File Transfer

    Quote Originally Posted by jmcilhinney View Post
    With a TcpClient this would be very simple:
    vb.net Code:
    1. myTcpClient.GetStream().Write(IO.File.ReadAllBytes("file path here"))
    to send the contents of an image file or, if you already have an Image object:
    vb.net Code:
    1. myImage.Save(myTcpClient.GetStream(), ImageFormat.Jpeg)
    If you're going to use .NET then you should use .NET. If you're going to cling to the VB6 way of doing things then you may as well stick with VB6.
    okay well whether i use vb6 or vb.net will not affect my problem.. besides i still want to use the power of the .NET framework for many other features, and I am used to building protocols with WinSock.

    and i've already designed my program to be multi-user with the server running on just 1 port.. it would require an entire revamp to switch to the Socket class

    besides it sounds like a cop-out to me

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width