一千萬個為什麽

搜索

通過tcp socket發送jpegs ...有時不完整

Vb.net

你好

I've been working on a project for months now (vb 2008 express). There is one final problem w你好ch 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 t你好s might be somet你好ng 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 t你好s final hurdle for weeks.

謝謝你的期待。

客戶 - - -

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

最佳答案

你得到你的形象的方式是錯誤的,這樣做:

Numbytes = ClientSocket.Receive(RecvBuffer)

你很有可能獲得部分數據,因為套接字實際上可以比它的緩沖區中的完整圖像少一些。無論如何嘗試接受它(我將在c#中給出樣本):

MemoryStream responseStream = new MemoryStream();
byte[] recvBuffer = new recvBuffer[4096];//just some size
int read = 0;//indicates how much data actually read from socket, can be smaller then buffer
do{
    read = ClientSocket.Receive( recvBuffer );//recieve data
    responseStream.write( recvBuffer, 0, read );//write it to resposne stream for later use
}
while( read != 0 );

這應該可行,但我會在你的情況下以其他方式做到這一點。這是客戶的代碼:

Socket s = new Socket( blablabla );
s.Connect( endPoint );
NetworkStream clientStream = new NetworkStream(s);
clientStream.Write( "Send picture" );//send a command to server

int pictureSize = clientStream.ReadInt();//here we expect to read following picture's size
byte[] pictureBuffer = clientStream.ReadBytes( pictureSize );

和服務器,監聽器部分:

Socket serverSocket = new Socket( blablabla );
serverSocket.Bind( blabla );
serverSocket.Listen( 100 );

while( true ){
    Socket clientSocket = serverSocket.Accept();
    Thread t = new Thread( new ParametrizedThreadStart( (object Obj) =>
    {
        Socket clientSocket = (Socket)Obj;
        NetworkStream clientStream = new NetworkStream( clientSocket );
        string command = clientStream.ReadString();
        if( command == "Send picture" ){
           //get ur picture here
           //get it's size
            clientStream.Write( pictureSize );
            clientStream.Write( pictureBytes );
        }
    } ) );
    t.start( clientSocket );
}

我希望這個偽代碼很清楚,隨便提問:)

轉載註明原文: 通過tcp socket發送jpegs ...有時不完整