搜索内容

有一个问题?

如果您有任何疑问,可以在下面询问或输入您要寻找的!

一个打印的小功能,运行在Receive异常提示:你的主机中的软件中止了一个已建立的连接。

技术聚客 未结 1 985
stefan&Napo
stefan&Napo 2021-04-08 11:05
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Web.Script.Serialization; namespace Print { class Program { static JavaScriptSerializer js = new JavaScriptSerializer(); static void Main(string[] args) { try { //打印服务的Ip地址 string ip = "127.0.0.1"; //参数 APIParam param = new APIParam(); //这里调用了 WebService/MesATEApi.asmx 获取打印数据 string str = new PrintService.MesATEApiSoapClient().EQLabelPrinting(param.sn, param.printType, param.stationName, param.resourceName, param.empNo); APIData data = js.Deserialize<APIData>(str); if (data.Result != "OK") { Console.WriteLine(data.Msg); return; } //开始打印 using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { client.Connect(new IPEndPoint(IPAddress.Parse(ip), 9000)); //告诉打印服务要打印 byte[] comm1 = Encoding.UTF8.GetBytes("CSharp-Socket-Print"); client.Send(comm1, comm1.Length, 0); //发送打印数据 byte[] comm2 = Encoding.UTF8.GetBytes(js.Serialize(data)); client.Send(comm2, comm2.Length, 0);      //接收返回消息 byte[] receivebyte = new byte[2048]; int length = client.Receive(receivebyte, receivebyte.Length, 0); receivebyte = receivebyte.Skip(0).Take(length).ToArray(); string result = Encoding.UTF8.GetString(receivebyte); WS_Message msg = js.Deserialize<WS_Message>(result); if (msg.Success) { Console.WriteLine("打印成功"); } else { Console.WriteLine(msg.Error); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } } /// <summary> /// API调用的参数 /// </summary> public class APIParam { public string sn = "PB210402000001"; public string printType = "1"; public string stationName = "test1"; public string resourceName = "成品B2拉"; public string empNo = "admin"; } public class APIData { /// <summary> /// 打印Id:GUID /// </summary> public string DataId { get; set; } /// <summary> /// 域名或者IP (URL的协议部分+端口部分):http;//sktmes004 /// </summary> public string Domain { get; set; } /// <summary> /// /8.5.4/Handler/PrintUpdate.ashx /// </summary> public string Url { get; set; } /// <summary> /// 文档模板Id labelDocumentId /// </summary> public string TempId { get; set; } /// <summary> /// 打印机名称 /// </summary> public string PrintName { get; set; } /// <summary> /// 打印类型//0:默认,1:adobe ,2:zpl /// </summary> public int Type { get; set; } /// <summary> /// 版本 /// </summary> public string Version { get; set; } /// <summary> /// 返回结果:OK 或者NG /// </summary> public string Result { get; set; } /// <summary> /// 返回错误消息 /// </summary> public string Msg { get; set; } /// <summary> /// 打印内容(比对内容) /// </summary> public List<ComparDataInfo> ComparData { get; set; } } public class ComparDataInfo { /// <summary> /// 客户SN /// </summary> public string IMEI { get; set; } /// <summary> /// MAC /// </summary> public string MAC { get; set; } /// <summary> /// 预留字段1 /// </summary> public string Deafult1 { get; set; } /// <summary> /// 预留字段2 /// </summary> public string Deafult2 { get; set; } /// <summary> /// 预留字段3 /// </summary> public string Deafult3 { get; set; } } public class WS_Message { public bool Success { get; set; } public string Error { get; set; } public object Result { get; set; } } }
1条回答
  • 2021-04-08 11:28

    补充图片

    0 讨论(0)
提交回复