954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

[HELP]sending many picture using socket programming

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...

murid
Light Poster
48 posts since May 2009
Reputation Points: 21
Solved Threads: 1
 

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

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

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

murid
Light Poster
48 posts since May 2009
Reputation Points: 21
Solved Threads: 1
 

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

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

thanks. try i'll that

murid
Light Poster
48 posts since May 2009
Reputation Points: 21
Solved Threads: 1
 

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

murid
Light Poster
48 posts since May 2009
Reputation Points: 21
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You