1
Vb.net發送JPEG文件...有時是不完整
嗨
我一直在一個項目工作了幾個月,現在(2008年VB快遞)。有一個最後的問題 ,我不能解決。
我需要從'服務器'(偵聽器)發送圖像到客戶端。下面的代碼大部分時間工作,但有時圖像不完整。我相信這可能與tcp數據包大小不同有關,可能受限於網絡上的繁忙程度。我已經看到了將圖片拆分成塊並將其發送出去的代碼示例,但是我無法讓它們工作,也許是因爲我使用的是不同的vb版本。要發送的圖片是最小的20k。
任何工作代碼的例子將是美好的。我一直在試驗,並且在這個最後的障礙上連續幾周都沒有成功。
感謝您的期待。
客戶-----
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
監聽-----
'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