Client Asynchronous Socket Communications RRS feed

  • Question

  • I have a client Asynchronous Socket communications I am trying to do to a specific Printer.   It allows TCP/IP communications on port 3028.

    I have successfully connected to the printer and sent a backup command which in response dumps the printers backup data.   All is good, however I am using a code example I found on MSDN under https://msdn.microsoft.com/en-us/library/bbx2eya8.aspx?f=255&MSPPError=-2147217396


    SharedSubReceiveCallback(ByValar AsIAsyncResult)

    ' Retrieve the state object and the client socket

    ' from the asynchronous state object.

    Dimstate AsStateObject= CType(ar.AsyncState, StateObject)

    Dimclient AsSocket= state.workSocket

    ' Read data from the remote device.

    DimbytesRead AsInteger= client.EndReceive(ar)

    Console.WriteLine("Ready to Read {0} bytes from server.", bytesRead)

    IfbytesRead > 0 Then

    ' There might be more data, so store the data received so far.


    Encoding.ASCII.GetString(state.buffer, 0, bytesRead))

    ' Get the rest of the data.  THIS IS WHERE IT HANGS

                client.BeginReceive(state.buffer, 0,

    StateObject.BufferSize, 0, NewAsyncCallback(AddressOfReceiveCallback), state)


    ' All the data has arrived; put it in response.

    Ifstate.sb.Length > 1 Then

                    response = state.sb.ToString()


    ' Signal that all bytes have been received.




    In the ReceicveCallBack, it is checking the incoming buffer to see if they is any data and then calls itself again.   When it calls itself it just hangs.   If I comment that out It works fine.  Can someone tell me what I am doing wrong?   Apparently it is a recursive call to itself to ensure the entire buffer has been handled and when the buffer length reaches 0 will complete and set the ReceiveDone.Set() to close out the call.

    For the next part of my project, when I actually start sending data to the printer, I will get a 1 byte acknowledgement code it received the data, then get a 1 byte code to tell me it has finished printing the data, and then I get a code for when a photocell was received.  At that time my program format the print data and sends the next print data to the printer and starts the cycle all over again.

    I have not found anything no how I can raise an event to tell when data is available in the socket buffer.

    Any help on this would be greatly appreciated.


    • Edited by rholmes Tuesday, July 7, 2015 4:18 PM
    Tuesday, July 7, 2015 4:18 PM

All replies