开发者

socket.BeginReceive() renders printer unresponsive

I've been developing an application that automatically prints PDF files on specific network printers upon arrival and monitoring @PJL USTATUS JOB messages. However, whenever I begin my application and the code hits socket.BeginReceive(), the printer is unable to receive a开发者_高级运维ny 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.

Am I missing anything? Like blocking/non-blocking or synchronous or asynchronous sockets?

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

I know I could do polling, but i won't be able to receive unsolicited printer messages.

Thanks in advance!

UPDATE: I'm getting the feeling that only one socket connection is allowed on the printers. Since socket.BeginReceive() 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?


Do Send report that all bytes have been sent (check it's return value)? I can't spot any errors that would prevent the other end from receiving your message. Are you 100% sure that the string you are sending is correct?


Hi I found same problem as yours when using printer

Firstly : Be sure the printer is on "Bi-Directional" mode and try this code:

  private void SendCommand(byte[] data, string ip, int port)
    {
        var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.NoDelay = true;

        var ipAddress = IPAddress.Parse(ip);
        IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);

        socket.Connect(remoteEP);
        socket.Send(data);
        socket.Close();
    }

and it will work good.

Best Regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜