i'm try to send many picture using socket programming in VB 2005 from client to server. but, after i run the program, only the first picture that reach destination server. and there is an error saying that my socket are no longer connected after the first picture are sent.

client side : ---> sender
at(0) -----> number of file that I want to send
at(3) n at(6) -----> file name address that I want to send

i = 1
        Dim x As Integer
        Dim z As Integer = 3
        If at(3) <> Nothing And at(6) <> Nothing Then
            x = at(0) + 1
            For var = i To x
                Dim PACKET_SIZE As UInt16 = 4096
                Dim ByteArray() As Byte ' Data buffer
                Dim filestream As FileStream = New FileStream(at(z), FileMode.Open, FileAccess.Read)
                Dim Reader As New BinaryReader(filestream)
                Dim Writer As New BinaryWriter(clientSocke.GetStream) ' Get socket's stream
                'send size of file
                'Send the file data
                    'read data from file
                    ByteArray = Reader.ReadBytes(PACKET_SIZE)
                    'write data to Network Stream
                Loop While ByteArray.Length = PACKET_SIZE
                'make sure all data is sent
                z = z + 3
            end if

Server side : ------> receiver
arrayteks(0) -----> number of file that I want to receive
arrayteks(3) n arrayteks(6) -----> file name address where I want save the received file

Dim x As Integer
         Dim z As Integer = 3
         If arrayTeks(3) <> Nothing And arrayTeks(6) <> Nothing Then
                    x = arrayTeks(0) + 1
                    For var = i To x
                        Dim PACKET_SIZE As UInt16 = 4096
                        Dim Reader As BinaryReader
                        Dim ReadBuffer(PACKET_SIZE - 1) As Byte
                        Dim NData As Int32
                        Dim MStream As MemoryStream
                        Dim LData As Int32

                        Reader = New BinaryReader(clientSocket.GetStream)
                        ' Read Length of data (Int32)
                        NData = Reader.ReadInt32
                        ' Now comes the data, save it in a memory stream
                        MStream = New MemoryStream
                        While NData > 0
                            LData = clientSocket.GetStream.Read(ReadBuffer, 0, PACKET_SIZE)
                            MStream.Write(ReadBuffer, 0, LData)
                            NData -= LData
                        End While

                        SaveFile(arrayTeks(z), MStream.ToArray, False) 'Then
                        z = z + 3
                end if

many thanks before...

7 Years
Discussion Span
Last Post by murid

declare the reader and writer before the for loop, assigne the values inside the loop and close them after the loop

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.