943,724 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 3347
  • VB.NET RSS
Jun 14th, 2009
0

[socket programming] send picture with socket programming

Expand Post »
Help me...
I want to sent picture with socket programming, from client to server.

client : get picture from OpenFileDialog, after get the picture , I send it to server.

server : after get the picture from client, the picture is saved in a destination folder.

I try my best, but all I can send is only 8kb picture.
if the picture larger than 8 kb, then at destination folder, only 8kb will be saved, and rest of the saved picture are blank...

here my current code:

Client :
VB.NET Syntax (Toggle Plain Text)
  1. Dim fileStream As FileStream = File.Open([filename], FileMode.Open, FileAccess.Read, FileShare.Read)
  2. Dim reader As BinaryReader = New BinaryReader(fileStream)
  3. Dim fileLength As Long = fileStream.Length
  4. Dim sendBytes(fileLength) As Byte
  5.  
  6. reader.Read(sendBytes, 0, fileLength)
  7. serverStream.Write(sendBytes, 0, fileLength)
  8. fileStream.Close()
  9. reader.Close()
Server:
VB.NET Syntax (Toggle Plain Text)
  1. Dim bgStream As NetworkStream = clientSocket.GetStream()
  2. Dim bgbytes(100024) As Byte
  3. bgStream.Read(bgbytes, 0, CInt(clientSocket.ReceiveBufferSize))
  4.  
  5. Dim gmb As New FileStream([filename], FileMode.Create, FileAccess.Write, FileShare.Write)
  6. Dim fileLength As Long = bgbytes.Length
  7. Dim writer As BinaryWriter = New BinaryWriter(gmb)
  8. writer.Write(bgbytes)
  9.  
  10. writer.Close()
  11. gmb.Close()



thanks before...
Last edited by Tekmaven; Jun 15th, 2009 at 1:39 am. Reason: Code Tags
Similar Threads
Reputation Points: 21
Solved Threads: 1
Light Poster
murid is offline Offline
48 posts
since May 2009
Jun 15th, 2009
0

Re: [socket programming] send picture with socket programming

You should read data from the socket in "chunks". Here's a snippet from asynchronous socket data transfer
VB.NET Syntax (Toggle Plain Text)
  1. Private PACKET_SIZE As UInt16 = 4096
  2. .
  3. .
  4. SyncLock Client.GetStream
  5. Reader = New BinaryReader(Client.GetStream)
  6. 'next we expect a pass-through byte
  7. Client.GetStream.Read(ReadByte, 0, 1)
  8. PassThroughByte = ReadByte(0)
  9. 'next expect length of data (Int32)
  10. NData = Reader.ReadInt32
  11. LenData = NData
  12. 'now comes the data, save it in a memory stream
  13. MStream = New MemoryStream
  14. While NData > 0
  15. RaiseEvent TransferProgress(Me, PassThroughByte, CSng(1.0 - NData / LenData))
  16. LData = Me.Client.GetStream.Read(ReadBuffer, 0, PACKET_SIZE)
  17. MStream.Write(ReadBuffer, 0, LData)
  18. NData -= LData
  19. End While
  20. 'Continue the asynchronous read from the NetworkStream
  21. Me.Client.GetStream.BeginRead(ReadByte, 0, 1, AddressOf ReceiveOneByte, Nothing)
  22. End SyncLock
and I suppose you are able to grab the idea i.e. how the chunks are read from the socket in the while loop.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 15th, 2009
0

Re: [socket programming] send picture with socket programming

Use BB code tags to present your source code.

Read this article.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jun 15th, 2009
0

Re: [socket programming] send picture with socket programming

Click to Expand / Collapse  Quote originally posted by Teme64 ...
You should read data from the socket in "chunks". Here's a snippet from asynchronous socket data transfer
VB.NET Syntax (Toggle Plain Text)
  1. Private PACKET_SIZE As UInt16 = 4096
  2. .
  3. .
  4. SyncLock Client.GetStream
  5. Reader = New BinaryReader(Client.GetStream)
  6. 'next we expect a pass-through byte
  7. Client.GetStream.Read(ReadByte, 0, 1)
  8. PassThroughByte = ReadByte(0)
  9. 'next expect length of data (Int32)
  10. NData = Reader.ReadInt32
  11. LenData = NData
  12. 'now comes the data, save it in a memory stream
  13. MStream = New MemoryStream
  14. While NData > 0
  15. RaiseEvent TransferProgress(Me, PassThroughByte, CSng(1.0 - NData / LenData))
  16. LData = Me.Client.GetStream.Read(ReadBuffer, 0, PACKET_SIZE)
  17. MStream.Write(ReadBuffer, 0, LData)
  18. NData -= LData
  19. End While
  20. 'Continue the asynchronous read from the NetworkStream
  21. Me.Client.GetStream.BeginRead(ReadByte, 0, 1, AddressOf ReceiveOneByte, Nothing)
  22. End SyncLock
and I suppose you are able to grab the idea i.e. how the chunks are read from the socket in the while loop.

this code, where should I put it ?? in the server part to recieve data ?? if so, then it means I send the picture from client without divide the data in to litte piece ??

correct me if I wrong... :p
Reputation Points: 21
Solved Threads: 1
Light Poster
murid is offline Offline
48 posts
since May 2009
Jun 15th, 2009
0

Re: [socket programming] send picture with socket programming

what class should I use for NData, LenData, and LData ?

thanks..
Reputation Points: 21
Solved Threads: 1
Light Poster
murid is offline Offline
48 posts
since May 2009
Jun 15th, 2009
0

Re: [socket programming] send picture with socket programming

Sorry for so unclear answer.

Here's the code with proper type definitions. And this code is receiving i.e. your server side code
VB.NET Syntax (Toggle Plain Text)
  1. Private PACKET_SIZE As UInt16 = 4096
  2. .
  3. .
  4. Dim Reader As BinaryReader
  5. Dim ReadBuffer(PACKET_SIZE - 1) As Byte
  6. Dim NData As Int32
  7. Dim MStream As MemoryStream
  8. Dim LData As Int32
  9.  
  10. Reader = New BinaryReader(Client.GetStream)
  11. ' Read Length of data (Int32)
  12. NData = Reader.ReadInt32
  13. ' Now comes the data, save it in a memory stream
  14. MStream = New MemoryStream
  15. While NData > 0
  16. LData = Me.Client.GetStream.Read(ReadBuffer, 0, PACKET_SIZE)
  17. MStream.Write(ReadBuffer, 0, LData)
  18. NData -= LData
  19. End While
And your client side code changes a bit
VB.NET Syntax (Toggle Plain Text)
  1. Dim fileStream As FileStream = File.Open([filename], FileMode.Open, FileAccess.Read, FileShare.Read)
  2. Dim reader As BinaryReader = New BinaryReader(fileStream)
  3. Dim fileLength As Int32 = CInt(fileStream.Length)
  4. Dim sendBytes(fileLength - 1) As Byte
  5.  
  6. reader.Read(sendBytes, 0, fileLength)
  7. ' Send length first
  8. serverStream.WriteInt32(fileLength)
  9. ' Now, send the data
  10. serverStream.Write(sendBytes, 0, fileLength)
since you have to send the length of the data first.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 15th, 2009
0

Re: [socket programming] send picture with socket programming

 Dim fileStream As FileStream = File.Open(backg, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim reader As BinaryReader = New BinaryReader(fileStream)
        Dim fileLength As Int32 = CInt(fileStream.Length)
        Dim sendBytes(fileLength - 1) As Byte

        reader.Read(sendBytes, 0, fileLength)
        ' Send length first
        serverStream.WriteInt32(fileLength)
        ' Now, send the data
        serverStream.Write(sendBytes, 0, fileLength)

at the client side, there is some error (the red one).
it's say that:
'WriteInt32' is not a member of 'System.Net.Sockets.NetworkStream'.

btw, i'm Using vb 2005. sorry not to tell U before.
Reputation Points: 21
Solved Threads: 1
Light Poster
murid is offline Offline
48 posts
since May 2009
Jun 15th, 2009
0

Re: [socket programming] send picture with socket programming

Quote ...
btw, i'm Using vb 2005. sorry not to tell U before.
I was assuming VB.2005.

Quote ...
at the client side, there is some error (the red one).
Oops!

Here's the modified client code. You can't send the whole file at once. You have to also send data in "chunks" (like you noticed in one of your messages). This is basically same code as yours. Instead of "serverStream" I've used a binary writer ("Writer") which writes to socket's data stream i.e. System.Net.Sockets.NetworkStream. You had declared serverStream As System.Net.Sockets.NetworkStream I guess
VB.NET Syntax (Toggle Plain Text)
  1. Private PACKET_SIZE As UInt16 = 4096
  2. .
  3. .
  4. Dim ByteArray() As Byte ' Data buffer
  5. Dim Fs As FileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read)
  6. Dim Reader As New BinaryReader(Fs)
  7.  
  8. Try
  9. Dim Writer As New BinaryWriter(Me.Client.GetStream) ' Get socket's stream
  10. 'send size of file
  11. Writer.Write(CInt(Fs.Length))
  12. 'Send the file data
  13. Do
  14. 'read data from file
  15. ByteArray = Reader.ReadBytes(PACKET_SIZE)
  16. 'write data to Network Stream
  17. Writer.Write(ByteArray)
  18. Loop While ByteArray.Length = PACKET_SIZE
  19. 'make sure all data is sent
  20. Writer.Flush()
  21. Writer.Close()
  22. Reader.Close()
  23. Catch ex As Exception
  24. ' Handle errors
  25. End Try
The advantage of using binary writer is that you have more overloaded Write methods. Actually the code needs only Write(value As Integer) method not found in NetworkStream class to send the length of the data.
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008
Jun 15th, 2009
0

Re: [socket programming] send picture with socket programming

First of all, I want to say thank you very much.
the code U gave me... IT WORKS !!!!

allow me to ask another question:
if I want to send an animated GIF format picture, how do I save the picture in deatination folder?
I already using "System.Drawing.Imaging.ImageFormat.gif", but the GIF picture no longer animated, it become like JPEG format.
another help please...
Reputation Points: 21
Solved Threads: 1
Light Poster
murid is offline Offline
48 posts
since May 2009
Jun 16th, 2009
0

Re: [socket programming] send picture with socket programming

Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!

Quote ...
allow me to ask another question:
Not related to sockets. Please, start a new thread for a new question.

That's not to say, I wouldn't answer the question. I would gladly help with that other question too. It's just that people do look for these Questions&Answers with DaniWeb's search or some other way. And if somebody looks for help in question "if I want to send an animated GIF format picture, how do I save the picture in deatination folder?", they hardly look for it in a thread with title "[socket programming]..." That's the reason why you should always start a new thread
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Teme64 is offline Offline
1,024 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Listview multiple select
Next Thread in VB.NET Forum Timeline: Closing file in VB.NET





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC