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
                Writer.Write(CInt(filestream.Length))
                'Send the file data
                Do
                    'read data from file
                    ByteArray = Reader.ReadBytes(PACKET_SIZE)
                    'write data to Network Stream
                    Writer.Write(ByteArray)
                Loop While ByteArray.Length = PACKET_SIZE
                'make sure all data is sent
                Writer.Flush()
                z = z + 3
                Writer.Close()
                Reader.Close()
            Next
            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
                    Next
                end if

many thanks before...

Recommended Answers

All 5 Replies

you shouldnt close the writer (which is your socket.getstream) inside the loop. that closes the connection

if so, then when I should close the connection ??

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

thanks. try i'll that

now i can send many picture...
thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.