如何使用C#实现IPP网关

本文关键字:IPP 网关 实现 何使用 | 更新日期: 2023-09-27 17:58:07

我需要在.Net中开发一个Internet打印协议网关,该网关将使用AirPrint客户端接收IOS发出的打印作业。网关将接收激发的文档,并将其释放到打印队列中。我可以使用苹果提供的SDK广播我的打印服务。然而,当我在端口上侦听以接收文档的网络流时,由于客户端不断发送流,我无法检测到接收到的流的末尾。我的猜测是,我们必须读取属性并做出相应的响应,但我不知道这些属性。以下是我目前使用的代码:

IPAddress ipAddress = IPAddress.Parse("10.0.0.13");
IPAddress tcpListener = new TcpListener(ipAddress, 631);
tcpListener.Start();
while (true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
byte[] bytes = new byte[2560];
NetworkStream stream = tcpClient.GetStream();
stream.Read(bytes, 0, bytes.Length);
string mstrMessage = Encoding.ASCII.GetString(bytesReceived, 0, bytesReceived.Length);
string Continue = "HTTP/1.1 100 Continue'r'n'r'nHTTP/1.1 200 OK'r'nCache-Control: no-cache'r'nDate: " + dateTime + "'r'nPragma: no-cache'r'nTransfer-Encoding: chunked'r'nContent-Type: application/ipp'r'n'r'nattributes-charset utf-8 attributes-natural-language en-us compression-supported none printer-is-accepting-jobs true document-format-supported application/pdf'r'n'r'n0'r'n";
bytesSent = Encoding.ASCII.GetBytes(mstrResponse);
stream.Write(bytesSent, 0, bytesSent.Length);
}

如何使用C#实现IPP网关

您应该检查stream.Read中的返回值。如果它不是零,则有来自TcpClient:的传入字节

var bytesLength = 0;
do
{
   bytesLength = stream.Read(bytes, 0, bytes.Length);
   if (bytesLength == 0) return;
}
while(bytesLength > 0);

您需要了解沟通的级别。您甚至还没有阅读或编写正确的IPP消息。100 continue与purley HTTP相关。

尽管AirPrint的Apples Spec还没有公开,但网上仍然有很多信息。简而言之:AirPrint基于IPP。至于支持的PDL PDF是一个不错的选择,但不是唯一的选择。iOS首先检查打印机的功能。您提供什么样的(虚拟)打印服务器取决于您。

(如果您有业务案例并需要远程开发人员,请不要犹豫与我们联系。)