socket.BeginReceive()使打印机无响应

时间:2011-05-11 05:39:02

标签: c# sockets printing

我一直在开发一个应用程序,可以在到达时自动在特定网络打印机上打印PDF文件并监控@PJL USTATUS JOB消息。但是,每当我开始我的应用程序并且代码命中socket.BeginReceive()时,打印机就无法从任何工作站接收任何打印作业,这也意味着永远不会调用回调。但是,如果我关闭我的应用程序或仅关闭套接字,打印机将再次开始工作。

我错过了什么吗?像阻塞/非阻塞或同步或异步套接字一样?

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

我知道我可以进行投票,但我无法收到未经请求的打印机消息。

提前致谢!

更新 我感觉打印机上只允许一个套接字连接。由于socket.BeginReceive()只能在连接套接字时才能工作,因此套接字已被使用,因此无法发送任何其他请求。也许还有另一种处理未经请求的打印机回读的方法吗?

2 个答案:

答案 0 :(得分:0)

Send报告所有字节都已发送(检查它的返回值)?我无法发现任何会阻止另一端收到您的消息的错误。您是否100%确定您发送的字符串是否正确?

答案 1 :(得分:0)

您好我在使用打印机时发现了与您相同的问题

首先:确保打印机处于打开状态" Bi-Directional"模式并尝试此代码:

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

它会很好用。

最好的问候