| | |
[socket programming] send picture with socket programming
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: May 2009
Posts: 45
Reputation:
Solved Threads: 1
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 :
Server:
thanks before...
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)
Dim fileStream As FileStream = File.Open([filename], FileMode.Open, FileAccess.Read, FileShare.Read) Dim reader As BinaryReader = New BinaryReader(fileStream) Dim fileLength As Long = fileStream.Length Dim sendBytes(fileLength) As Byte reader.Read(sendBytes, 0, fileLength) serverStream.Write(sendBytes, 0, fileLength) fileStream.Close() reader.Close()
VB.NET Syntax (Toggle Plain Text)
Dim bgStream As NetworkStream = clientSocket.GetStream() Dim bgbytes(100024) As Byte bgStream.Read(bgbytes, 0, CInt(clientSocket.ReceiveBufferSize)) Dim gmb As New FileStream([filename], FileMode.Create, FileAccess.Write, FileShare.Write) Dim fileLength As Long = bgbytes.Length Dim writer As BinaryWriter = New BinaryWriter(gmb) writer.Write(bgbytes) writer.Close() gmb.Close()
thanks before...
Last edited by Tekmaven; Jun 15th, 2009 at 1:39 am. Reason: Code Tags
You should read data from the socket in "chunks". Here's a snippet from asynchronous socket data transfer
and I suppose you are able to grab the idea i.e. how the chunks are read from the socket in the while loop.
VB.NET Syntax (Toggle Plain Text)
Private PACKET_SIZE As UInt16 = 4096 . . SyncLock Client.GetStream Reader = New BinaryReader(Client.GetStream) 'next we expect a pass-through byte Client.GetStream.Read(ReadByte, 0, 1) PassThroughByte = ReadByte(0) 'next expect length of data (Int32) NData = Reader.ReadInt32 LenData = NData 'now comes the data, save it in a memory stream MStream = New MemoryStream While NData > 0 RaiseEvent TransferProgress(Me, PassThroughByte, CSng(1.0 - NData / LenData)) LData = Me.Client.GetStream.Read(ReadBuffer, 0, PACKET_SIZE) MStream.Write(ReadBuffer, 0, LData) NData -= LData End While 'Continue the asynchronous read from the NetworkStream Me.Client.GetStream.BeginRead(ReadByte, 0, 1, AddressOf ReceiveOneByte, Nothing) End SyncLock
Teme64 @ Windows Developer Blog
•
•
Join Date: May 2009
Posts: 45
Reputation:
Solved Threads: 1
•
•
•
•
You should read data from the socket in "chunks". Here's a snippet from asynchronous socket data transfer
and I suppose you are able to grab the idea i.e. how the chunks are read from the socket in the while loop.VB.NET Syntax (Toggle Plain Text)
Private PACKET_SIZE As UInt16 = 4096 . . SyncLock Client.GetStream Reader = New BinaryReader(Client.GetStream) 'next we expect a pass-through byte Client.GetStream.Read(ReadByte, 0, 1) PassThroughByte = ReadByte(0) 'next expect length of data (Int32) NData = Reader.ReadInt32 LenData = NData 'now comes the data, save it in a memory stream MStream = New MemoryStream While NData > 0 RaiseEvent TransferProgress(Me, PassThroughByte, CSng(1.0 - NData / LenData)) LData = Me.Client.GetStream.Read(ReadBuffer, 0, PACKET_SIZE) MStream.Write(ReadBuffer, 0, LData) NData -= LData End While 'Continue the asynchronous read from the NetworkStream Me.Client.GetStream.BeginRead(ReadByte, 0, 1, AddressOf ReceiveOneByte, Nothing) End SyncLock
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
Sorry for so unclear answer.
Here's the code with proper type definitions. And this code is receiving i.e. your server side code
And your client side code changes a bit since you have to send the length of the data first.
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)
Private 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(Client.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 = Me.Client.GetStream.Read(ReadBuffer, 0, PACKET_SIZE) MStream.Write(ReadBuffer, 0, LData) NData -= LData End While
VB.NET Syntax (Toggle Plain Text)
Dim fileStream As FileStream = File.Open([filename], 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)
Teme64 @ Windows Developer Blog
•
•
Join Date: May 2009
Posts: 45
Reputation:
Solved Threads: 1
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.
•
•
•
•
btw, i'm Using vb 2005. sorry not to tell U before.
•
•
•
•
at the client side, there is some error (the red one).
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)
Private PACKET_SIZE As UInt16 = 4096 . . Dim ByteArray() As Byte ' Data buffer Dim Fs As FileStream = New FileStream(FilePath, FileMode.Open, FileAccess.Read) Dim Reader As New BinaryReader(Fs) Try Dim Writer As New BinaryWriter(Me.Client.GetStream) ' Get socket's stream 'send size of file Writer.Write(CInt(Fs.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() Writer.Close() Reader.Close() Catch ex As Exception ' Handle errors End Try
Write(value As Integer) method not found in NetworkStream class to send the length of the data. Teme64 @ Windows Developer Blog
•
•
Join Date: May 2009
Posts: 45
Reputation:
Solved Threads: 1
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...
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...
Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!
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
•
•
•
•
allow me to ask another 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
Teme64 @ Windows Developer Blog
![]() |
Similar Threads
- Need help regarding socket programming (C)
- TCP/IP socket programming (Domains and DNS)
- Socket Programming in java (Java)
- Socket programming (Java)
- Help on socket programming (C++)
- Socket Programming (C++)
- Socket programming in IPv6? (C++)
- Socket Programming (ASP.NET)
- How to send a double value in socket programming (C)
Other Threads in the VB.NET Forum
- Previous Thread: Listview multiple select
- Next Thread: Closing file in VB.NET
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account arithmetic array arrays basic bing button buttons c# center check checkbox code combobox convert crystalreport data database datagrid datagridview date dissertation dissertations dropdownlist excel fade file-dialog filter ftp generatetags google gridview hardcopy images inline input insert intel internet listview mobile monitor ms net networking objects output panel passingparameters picturebox picturebox1 port position print printing problem project read remove save searchbox searchvb.net select serial server shutdown soap sorting survey table tcp temperature text textbox timer timespan toolbox trim update user validation vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vbnet view visual visualbasic visualbasic.net visualstudio2008 web winforms wpf year






