Sending jpegs by tcp socket...sometimes incomplete.
Posted
by Guy
on Stack Overflow
See other posts from Stack Overflow
or by Guy
Published on 2010-04-17T13:12:37Z
Indexed on
2010/04/24
20:23 UTC
Read the original article
Hit count: 314
Vb.net
Hi
I've been working on a project for months now (vb 2008 express). There is one final problem which I can't solve.
I need to send images to a client from a 'server'(listener). The code below works most of the time but sometimes the image is incomplete. I believe this might be something to do with the tcp packet sizes varying, maybe limited by how busy it is out there on the net. I have seen examples of code that splits the image into chunks and sends them out, but I can't get them to work maybe because I'm using a different vb version. The pictures to be sent are small 20k max.
Any working code examples would be wonderful. I have been experimenting and failing with this final hurdle for weeks.
Thanks in anticipation.
Client-----
Sub GetPic()
'------- Connect to Server
ClientSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, _ ProtocolType.Tcp)
ClientSocket.Connect(Epoint)
'------- Send Picture Request
Dim Bytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes("Send Picture")
ClientSocket.Send(Bytes, Bytes.Length, SocketFlags.None)
'------- Receive Response
Dim RecvBuffer(20000) As Byte
Dim Numbytes As Integer
Numbytes = ClientSocket.Receive(RecvBuffer)
Dim Darray(Numbytes) As Byte
Buffer.BlockCopy(RecvBuffer, 0, Darray, 0, Numbytes)
'------- Close Connection
ClientSocket.Shutdown(SocketShutdown.Both)
ClientSocket.Close()
'-------
Dim MStrm = New MemoryStream(Darray)
Picture = Image.FromStream(MStrm)
End Sub
Listener-----
'Threaded from a listener
Sub ClientThread(ByVal Client As TcpClient)
Dim MStrm As New MemoryStream
Dim Rbuffer(1024) As Byte
Dim Tbyte As Byte()
Dim NStrm As NetworkStream = Client.GetStream()
Dim I As Integer = NStrm.Read(Rbuffer, 0, Rbuffer.Length)
Dim Incoming As String = System.Text.Encoding.ASCII.GetString(Rbuffer, 0, I)
If Incoming = "Send Picture" then
Picture Save(MStrm, Picture.RawFormat)
Tbyte = MStrm.ToArray
NStrm.Write(Tbyte, 0, Tbyte.Length)
End if
Client.Close()
End Sub
© Stack Overflow or respective owner