views:

180

answers:

1

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
A: 

the way you're getting your image is quite wrong, doing like this:

Numbytes = ClientSocket.Receive(RecvBuffer)

you have a quite chance of getting part of data, cause socket can actually have a smaller amount then full image in it's buffer. Anyways try recieve it like that (i'll give sample in 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 );

This should do work, but I'd do it a little other way in your case. Here's client's code:

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 );

And server, listener part:

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 );
}

I hope this pseudo code is clear, feel free to ask questions :)

hoodoos