Sending jpg files client to server with Stream

Posted on 2008-02-01
Medium Priority
Last Modified: 2013-11-26
Ok its a simple question im sure for someone but i have not used much .net and just got back into programming.  I have a simple client to server program made up and am able to send a text file across easy.  Im wondering what to change in my code to send a jpeg file.

Iv tried it using just this code and the jpeg is distorted although u can see some of the screen.
Im just looking for a quick manipulation of my code here sort this,  I dont want to have to change much if possible.    I am thinking its probably a binary read i need or  a full byte for byte read.

Im using Visual Studio 2005.

'Here is the Client Code
Private Sub SendInfoFile()
        Dim fileBuffer As Byte()
        Dim fileStream As Stream
        Dim fback As String
        fback = "C:\text1.txt"
        fileStream = File.OpenRead(fback) 'open file to read
        ReDim fileBuffer(fileStream.Length) ' set size of file
        fileStream.Read(fileBuffer, 0, fileStream.Length) ' read the file in
        Dim clientSocket As New TcpClient(IPAddy, 667) 'send back on port 667 to Ip address of client in Global IPaddy
        Dim netWorkStream As NetworkStream
        netWorkStream = clientSocket.GetStream()
        netWorkStream.Write(fileBuffer, 0, fileStream.Length)
    End Sub
'And here is the code for the Server to Catch and write the file.
Public Sub handlerThread()
        Dim handlerSocket As Socket
        handlerSocket = alSockets(alSockets.Count - 1)
        Dim networkStream As NetworkStream = New NetworkStream(handlerSocket)
        Dim blockSize As Int16 = 1024
        Dim thisRead As Int16
        Dim dataByte(blockSize) As Byte
        SyncLock Me
            Dim fileStream As Stream
            fileStream = File.OpenWrite("C:\copiedtext.txt")
            While (True)
                thisRead = networkStream.Read(dataByte, 0, blockSize)
                If thisRead = 0 Then
                    Exit While
                End If
                fileStream.Write(dataByte, 0, dataByte.Length)
            End While
        End SyncLock
        lbxConnections.Items.Add("File Written")
        handlerSocket = Nothing
    End Sub

Open in new window

Question by:TDC_LuCiFeR
  • 2
  • 2

Author Comment

ID: 20802779
I need a solution fast as possible please!
ill give 500 more points!!!!
LVL 12

Accepted Solution

vb_jonas earned 2000 total points
ID: 20804804
                fileStream.Write(dataByte, 0, dataByte.Length)
                fileStream.Write(dataByte, 0, thisRead)

because the last read might not be 1024 bytes.

Author Closing Comment

ID: 31427296
Thank you very much, i see what you mean exactly.  Iv been tormenting myself with it,  ty very much,  my .jps were coming through but a bit distorted!  Here is points also if u leave a comment in the section.

LVL 12

Expert Comment

ID: 20805032

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Loops Section Overview

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question