Note that there are some explanatory texts on larger screens.

plurals
  1. POsocket.BeginReceive() renders printer unresponsive
    primarykey
    data
    text
    <p>I've been developing an application that automatically prints PDF files on specific network printers upon arrival and monitoring <code>@PJL USTATUS JOB</code> messages. However, whenever I begin my application and the code hits <code>socket.BeginReceive()</code>, the printer is unable to receive any print jobs from any workstation which also means that the callback will never be called. However, if I close my application or just the socket, the printer begins working again.</p> <p>Am I missing anything? Like blocking/non-blocking or synchronous or asynchronous sockets?</p> <pre><code>public class SocketData { public Socket socket; public byte[] dataBuffer = new byte[1024]; } public void Start() { // start the thread that checks the printer status thread = new Thread(new ThreadStart(ConnectAndListen)); thread.Priority = ThreadPriority.Lowest; thread.Start(); // start the file listener listener = new FileSystemWatcher(this.WatchPath); listener.Created += new FileSystemEventHandler(listener_Created); listener.EnableRaisingEvents = true; } public void ConnectAndListen() { ipEndPoint = new IPEndPoint(Dns.GetHostEntry(this.IPAddress).AddressList[0], 9100); socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(ipEndPoint); //socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS DEVICE = ON\r\n\x1B%-12345X\r\n")); //socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS JOB = ON\r\n\x1B%-12345X\r\n")); socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS PAGE = ON\r\n\x1B%-12345X\r\n")); //socket.Send(Encoding.ASCII.GetBytes("\x1B%-12345X@PJL USTATUS TIMED = 0\r\n\x1B%-12345X\r\n")); ListenForPrinterReadBack(socket); } public void ListenForPrinterReadBack(Socket socket) { if (DataRecievedCallBack == null) { DataRecievedCallBack = new AsyncCallback(OnDataReceived); } SocketData socketData = new SocketData(); socketData.socket = socket; m_asynResult = socket.BeginReceive(socketData.dataBuffer, 0, socketData.dataBuffer.Length, SocketFlags.None, DataRecievedCallBack, socketData); } public void OnDataReceived(IAsyncResult asyn) { SocketData socketData = (SocketData)asyn.AsyncState; int result = socketData.socket.EndReceive(asyn); char[] chars = new char[result + 1]; Decoder d = Encoding.UTF8.GetDecoder(); //creating object for decoding int charLen = d.GetChars(socketData.dataBuffer, 0, result, chars, 0); String recievedData = new String(chars); this.Status = recievedData; ListenForPrinterReadBack(socketData.socket); } </code></pre> <p>I know I could do polling, but i won't be able to receive unsolicited printer messages.</p> <p>Thanks in advance!</p> <p><strong>UPDATE:</strong> I'm getting the feeling that only one socket connection is allowed on the printers. Since <code>socket.BeginReceive()</code> can only work if the socket is connected, then any other requests cannot be sent since the socket is already being used. Maybe there's another way to handle unsolicited printer readbacks?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload