public static ArrayList GetTicket(string host, int port, byte[] pack, TextBox txt) { sbTmp = new StringBuilder(); #if DEBUG WriteLog("客户端开始连接!", txt); #endif try { // int port = 2908; // string host = "119.10.114.212"; // string host = "58.62.144.227"; // int port = 2907; // string host = "10.12.23.216"; // int port = 2907; IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port); //把ip和端口转化为IPEndPoint实例 Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //创建一个Socket #if DEBUG WriteLog("开始连接服务器IP:" + ip + ",端口" + port + "...", txt); #endif // c.ReceiveTimeout = 100; // c.SendTimeout = 1000; // c.Connect(ipe, c.Connect(ipe);//连接到服务器 #if DEBUG WriteLog("完成连接服务器...", txt); //string sendStr = str; WriteLog("发送取票内容为:" + pack, txt); WriteLog("Hex输出为:" + GetHexOutput(pack), txt); #endif c.Send(pack, pack.Length, 0);//发送测试信息 string recvStr = ""; byte[] recvBytes = new byte[1024]; int bytes; #if DEBUG WriteLog("开始从服务器接收内容...", txt); #endif int readTime = 0; byte[] heads1 = new byte[5]; byte[] heads2 = new byte[22]; bool beginAccept = true; int acceptByte = 0; while (beginAccept) { acceptByte = c.Receive(heads1, heads1.Length, SocketFlags.None); #if DEBUG WriteLog("尝试接收5字节,实际接收结果为:" + acceptByte.ToString(), txt); #endif if (acceptByte == 5) { break; } } while (beginAccept) { acceptByte = c.Receive(heads2, heads2.Length, SocketFlags.None); #if DEBUG WriteLog("尝试接收22字节,实际接收结果为:" + acceptByte.ToString(), txt); #endif if (acceptByte == 22) { break; } } byte[] lens = new byte[4]; Array.Copy(heads2, 1, lens, 0, lens.Length); int len = HiPiaoProtocol.GetDataPackageLen(lens); len -= 22; #if DEBUG byte[] dataLength = BitConverter.GetBytes(1484); WriteLog("changdu:" + GetHexOutput(dataLength), txt); WriteLog("Hex输出为:" + GetHexOutput(heads1), txt); WriteLog("获取报文包头并解析的长度为" + len, txt); WriteLog("Hex输出为:" + GetHexOutput(heads2), txt); #endif /* * byte[] allpackage = new byte[len]; * while (beginAccept) * { * * acceptByte=c.Receive(allpackage); #if DEBUG * WriteLog("尝试接收" + len.ToString()+ "字节,实际接收结果为:" + acceptByte.ToString(), txt); #endif * if (acceptByte>0) * { * * break; * } * * } */ byte[] allpackage = new byte[len]; byte[] bufferTmp = new byte[1024]; int needAcceptLen = len < 1024?len:1024; int acceptLen = 0; // bool needAccept=true; while (needAcceptLen > 0) { acceptByte = c.Receive(allpackage, acceptLen, needAcceptLen, SocketFlags.None); #if DEBUG WriteLog("尝试接收" + needAcceptLen.ToString() + "字节,实际接收结果为:" + acceptByte.ToString(), txt); #endif acceptLen += acceptByte; if (acceptLen < len) { needAcceptLen = len - acceptLen < 1024 ? len - acceptLen : 1024; } else { needAcceptLen = 0; break; } } // acceptByte = c.Receive(allpackage); #if DEBUG // WriteLog("尝试接收2字节,实际接收结果为:" + acceptByte.ToString(), txt); #endif #if DEBUG WriteLog("开始断开服务器连接...", txt); #endif c.Close(); #if DEBUG WriteLog("完成断开服务器连接", txt); #endif // List<TicketPrintObject> tickets = new List<TicketPrintObject>(); ArrayList tickets = new ArrayList(); string tmpstr = Encoding.GetEncoding("GBK").GetString(allpackage, 0, len); #if DEBUG if (tmpstr.StartsWith("取票失败:")) { WriteLog("最终取票结果的字符串为:" + tmpstr, txt); } WriteAllString(sbTmp.ToString()); sbTmp = new StringBuilder(); // string tmpstr7ttt = ReadAllString(); #endif if (tmpstr.StartsWith("取票失败:")) { TicketPrintObject tmpTicket1 = new TicketPrintObject(); tmpTicket1.IsPrinted = true; tickets.Add(tmpTicket1); return(tickets); } string[] tmpstr1 = tmpstr.Split('\r'); string[] tmpstr2 = tmpstr1[1].Split('\n'); GetTicketPrintObject[] ticketsObjects = new GetTicketPrintObject[tmpstr2.Length]; GetTicketPrintObject tmpTicket = null; for (int i = 0; i < tmpstr2.Length; i++) { string[] tmpstr3 = tmpstr2[i].Split('\t'); #if DEBUG if (tmpstr3.Length == 7) { WriteLog("票据唯一标识:" + tmpstr3[0], txt); WriteLog("正副券标识:" + tmpstr3[1], txt); WriteLog("打印X坐标:" + tmpstr3[2], txt); WriteLog("打印Y坐标:" + tmpstr3[3], txt); WriteLog("字体大小:" + tmpstr3[4], txt); WriteLog("是否加粗:" + tmpstr3[5], txt); WriteLog("打印项内容:" + tmpstr3[6], txt); } #endif if (tmpstr3.Length != 7) { break; } else { tmpTicket = new GetTicketPrintObject(); tmpTicket.Id = tmpstr3[0]; tmpTicket.Id2 = tmpstr3[1]; tmpTicket.X = Convert.ToInt32(tmpstr3[2]); tmpTicket.Y = Convert.ToInt32(tmpstr3[3]); tmpTicket.FontWeight = Convert.ToInt32(tmpstr3[4]); tmpTicket.IsBold = tmpstr3[5] == "1"; tmpTicket.Content = tmpstr3[6]; ticketsObjects[i] = tmpTicket; } } #if DEBUG WriteLog("从服务器接收内容长度为:" + allpackage + " 内容为:" + tmpstr, txt); WriteLog("Hex输出为:" + GetHexOutput(allpackage), txt); #endif // for() /* * * while (readTime<3) * { * // bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息 * //c.Receive( * * bytes = c.Receive(recvBytes); * * * byte[] data = new byte[bytes]; * Array.Copy(recvBytes, data, data.Length); * * recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes); #if DEBUG * Console.WriteLine("从服务器第"+readTime.ToString()+"次接收内容长度为:" + bytes + " 内容为:" + recvStr); * Console.WriteLine("Hex输出为:" + GetHexOutput(data)); #endif * // Console.WriteLine("发送取票获取结果内容为:{0}", recvStr);//显示服务器返回信息 * * readTime++; * } * * */ // return recvStr; //int count = tmpstr.Split("出票时间".ToCharArray()).Length-1; string strReplaced = tmpstr.Replace("出票时间", ""); int count = (tmpstr.Length - strReplaced.Length) / "出票时间".Length; TicketPrintObject ticketPrintObject = null; SellProductPrinter sellProduct = null; int startindex = 0; string tmpid1 = string.Empty; int num = 16; int sellcount = 0; int ticketcount = 0; for (int i = 0; i < count; i++) { //if (tmpstr2[startindex].IndexOf("出票时间")!=-1) if (tmpid1.Length > 0 && ticketsObjects[startindex].Content.StartsWith("名称")) { //startindex +=sellcount*8; sellProduct = new SellProductPrinter(); sellProduct.Id = ticketsObjects[startindex + 0].Id; sellProduct.Id2 = ticketsObjects[startindex + 0].Id2; sellProduct.ProductName = ticketsObjects[startindex + 0].Content.Substring(3); sellProduct.Price = ticketsObjects[startindex + 1].Content.Substring(3); sellProduct.PlayDate = ticketsObjects[startindex + 2].Content.Substring(4); sellProduct.PlayTime = ticketsObjects[startindex + 3].Content.Substring(5); sellProduct.Content = ticketsObjects[startindex + 4].Content.Substring(3); sellProduct.Cinema = ticketsObjects[startindex + 5].Content; sellProduct.HipiaoCard = ticketsObjects[startindex + 6].Content.Substring(4); sellProduct.PrintTime = ticketsObjects[startindex + 7].Content.Substring(5); tickets.Add(sellProduct); sellcount++; startindex += 8; continue; } else { tmpid1 = ticketsObjects[startindex + 0].Id; // startindex += ticketcount * 16; ticketPrintObject = new TicketPrintObject(); ticketPrintObject.Cinema = ticketsObjects[startindex + 0].Content; ticketPrintObject.UniqueId = tmpid1; ticketPrintObject.IsPrinted = false; ticketPrintObject.MovieName = ticketsObjects[startindex + 1].Content.Substring(3); ticketPrintObject.TicketId = ticketsObjects[startindex + 2].Content.Substring(3); // string ttt = ticketsObjects[startindex + 3].Content.Substring(4); ticketPrintObject.Price = Convert.ToInt32(Convert.ToDouble(ticketsObjects[startindex + 3].Content.Substring(4))); ticketPrintObject.PlayTime = ticketsObjects[startindex + 4].Content; ticketPrintObject.RoomName = ticketsObjects[startindex + 6].Content.Substring(3); ticketPrintObject.Seat = ticketsObjects[startindex + 7].Content.Substring(3); ticketPrintObject.PlayDate = ticketsObjects[startindex + 8].Content.Substring(3); if (ticketsObjects[startindex + 14].Content.StartsWith("出票时间")) { ticketPrintObject.PrintTime = ticketsObjects[startindex + 14].Content.Substring(5); } else if (ticketsObjects[startindex + 14].Content.StartsWith("手续费")) { ticketPrintObject.MiddleFee = ticketsObjects[startindex + 14].Content.Substring(5); } if (ticketsObjects[startindex + 15].Content.StartsWith("出票时间")) { ticketPrintObject.PrintTime = ticketsObjects[startindex + 15].Content.Substring(5); } else if (ticketsObjects[startindex + 15].Content.StartsWith("手续费")) { ticketPrintObject.MiddleFee = ticketsObjects[startindex + 15].Content.Substring(5); } ticketPrintObject.ChangCi = ticketsObjects[startindex + 13].Content; ticketPrintObject.SeatId = string.Empty; tickets.Add(ticketPrintObject); ticketcount++; startindex += 16; continue; } } #if DEBUG WriteLog("获取票的个数为:" + tickets.Count.ToString(), txt); WriteAllString(sbTmp.ToString()); #endif return(tickets); } catch (ArgumentNullException e) { WriteLog(string.Format("ArgumentNullException: {0}", e), txt); sbTmp.Append("\r\n"); sbTmp.Append(e.ToString()); WriteAllString(sbTmp.ToString()); return(null); } catch (SocketException e) { WriteLog(string.Format("SocketException: {0}", e), txt); sbTmp.Append("\r\n"); sbTmp.Append(e.ToString()); WriteAllString(sbTmp.ToString()); throw e; // return null; } #if DEBUG WriteLog("客户端连接完成!", txt); WriteAllString(sbTmp.ToString()); #endif return(null); }
public static void PrintSellProduct(SellProductPrinter ticketTmp) { SystemConfig config = FT.Commons.Cache.StaticCacheManager.GetConfig<SystemConfig>(); if (config.PrinterType == "Windows") { PrintWindowsTemplate(ticketTmp, "SellProductTemplate-Windows.txt"); } else { PrintTemplate(ticketTmp, "SellProduct.txt"); } }
public static ArrayList GetTicket(string host, int port, byte[] pack, TextBox txt) { sbTmp = new StringBuilder(); #if DEBUG WriteLog("客户端开始连接!",txt); #endif try { // int port = 2908; // string host = "119.10.114.212"; // string host = "58.62.144.227"; // int port = 2907; // string host = "10.12.23.216"; // int port = 2907; IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例 Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket #if DEBUG WriteLog("开始连接服务器IP:" + ip + ",端口" + port + "...",txt); #endif // c.ReceiveTimeout = 100; // c.SendTimeout = 1000; // c.Connect(ipe, c.Connect(ipe);//连接到服务器 #if DEBUG WriteLog("完成连接服务器...",txt); //string sendStr = str; WriteLog("发送取票内容为:" + pack,txt); WriteLog("Hex输出为:"+GetHexOutput(pack),txt); #endif c.Send(pack, pack.Length, 0);//发送测试信息 string recvStr = ""; byte[] recvBytes = new byte[1024]; int bytes; #if DEBUG WriteLog("开始从服务器接收内容...",txt); #endif int readTime=0; byte[] heads1=new byte[5]; byte[] heads2 = new byte[22]; bool beginAccept = true; int acceptByte = 0; while (beginAccept) { acceptByte=c.Receive(heads1, heads1.Length, SocketFlags.None); #if DEBUG WriteLog("尝试接收5字节,实际接收结果为:" + acceptByte.ToString(), txt); #endif if (acceptByte == 5) { break; } } while (beginAccept) { acceptByte = c.Receive(heads2, heads2.Length, SocketFlags.None); #if DEBUG WriteLog("尝试接收22字节,实际接收结果为:"+acceptByte.ToString(),txt); #endif if (acceptByte == 22) { break; } } byte[] lens=new byte[4]; Array.Copy(heads2,1, lens,0, lens.Length); int len = HiPiaoProtocol.GetDataPackageLen(lens); len -= 22; #if DEBUG byte[] dataLength = BitConverter.GetBytes(1484); WriteLog("changdu:" + GetHexOutput(dataLength),txt); WriteLog("Hex输出为:" + GetHexOutput(heads1),txt); WriteLog("获取报文包头并解析的长度为" + len ,txt); WriteLog("Hex输出为:" + GetHexOutput(heads2),txt); #endif /* byte[] allpackage = new byte[len]; while (beginAccept) { acceptByte=c.Receive(allpackage); #if DEBUG WriteLog("尝试接收" + len.ToString()+ "字节,实际接收结果为:" + acceptByte.ToString(), txt); #endif if (acceptByte>0) { break; } } */ byte[] allpackage = new byte[len]; byte[] bufferTmp=new byte[1024]; int needAcceptLen = len<1024?len:1024; int acceptLen = 0; // bool needAccept=true; while (needAcceptLen>0) { acceptByte = c.Receive(allpackage,acceptLen,needAcceptLen,SocketFlags.None); #if DEBUG WriteLog("尝试接收" + needAcceptLen.ToString() + "字节,实际接收结果为:" + acceptByte.ToString(), txt); #endif acceptLen += acceptByte; if (acceptLen < len) { needAcceptLen = len - acceptLen < 1024 ? len - acceptLen : 1024; } else { needAcceptLen = 0; break; } } // acceptByte = c.Receive(allpackage); #if DEBUG // WriteLog("尝试接收2字节,实际接收结果为:" + acceptByte.ToString(), txt); #endif #if DEBUG WriteLog("开始断开服务器连接...", txt); #endif c.Close(); #if DEBUG WriteLog("完成断开服务器连接", txt); #endif // List<TicketPrintObject> tickets = new List<TicketPrintObject>(); ArrayList tickets = new ArrayList(); string tmpstr = Encoding.GetEncoding("GBK").GetString(allpackage, 0, len); #if DEBUG if (tmpstr.StartsWith("取票失败:")) { WriteLog("最终取票结果的字符串为:"+tmpstr, txt); } WriteAllString(sbTmp.ToString()); sbTmp = new StringBuilder(); // string tmpstr7ttt = ReadAllString(); #endif if (tmpstr.StartsWith("取票失败:")) { TicketPrintObject tmpTicket1 = new TicketPrintObject(); tmpTicket1.IsPrinted = true; tickets.Add(tmpTicket1); return tickets; } string[] tmpstr1 = tmpstr.Split('\r'); string[] tmpstr2 = tmpstr1[1].Split('\n'); GetTicketPrintObject[] ticketsObjects=new GetTicketPrintObject[tmpstr2.Length]; GetTicketPrintObject tmpTicket = null; for (int i = 0; i < tmpstr2.Length; i++) { string[] tmpstr3 = tmpstr2[i].Split('\t'); #if DEBUG if (tmpstr3.Length == 7) { WriteLog("票据唯一标识:" + tmpstr3[0], txt); WriteLog("正副券标识:" + tmpstr3[1], txt); WriteLog("打印X坐标:" + tmpstr3[2], txt); WriteLog("打印Y坐标:" + tmpstr3[3], txt); WriteLog("字体大小:" + tmpstr3[4], txt); WriteLog("是否加粗:" + tmpstr3[5], txt); WriteLog("打印项内容:" + tmpstr3[6], txt); } #endif if (tmpstr3.Length != 7) { break; } else { tmpTicket = new GetTicketPrintObject(); tmpTicket.Id = tmpstr3[0]; tmpTicket.Id2 = tmpstr3[1]; tmpTicket.X = Convert.ToInt32(tmpstr3[2]); tmpTicket.Y = Convert.ToInt32(tmpstr3[3]); tmpTicket.FontWeight = Convert.ToInt32(tmpstr3[4]); tmpTicket.IsBold = tmpstr3[5] == "1"; tmpTicket.Content=tmpstr3[6]; ticketsObjects[i] = tmpTicket; } } #if DEBUG WriteLog("从服务器接收内容长度为:" + allpackage + " 内容为:" + tmpstr,txt); WriteLog("Hex输出为:" + GetHexOutput(allpackage),txt); #endif // for() /* while (readTime<3) { // bytes = c.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息 //c.Receive( bytes = c.Receive(recvBytes); byte[] data = new byte[bytes]; Array.Copy(recvBytes, data, data.Length); recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes); #if DEBUG Console.WriteLine("从服务器第"+readTime.ToString()+"次接收内容长度为:" + bytes + " 内容为:" + recvStr); Console.WriteLine("Hex输出为:" + GetHexOutput(data)); #endif // Console.WriteLine("发送取票获取结果内容为:{0}", recvStr);//显示服务器返回信息 readTime++; } * * */ // return recvStr; //int count = tmpstr.Split("出票时间".ToCharArray()).Length-1; string strReplaced = tmpstr.Replace("出票时间", ""); int count= (tmpstr.Length - strReplaced.Length) / "出票时间".Length; TicketPrintObject ticketPrintObject = null; SellProductPrinter sellProduct = null; int startindex = 0; string tmpid1=string.Empty; int num = 16; int sellcount = 0; int ticketcount = 0; for (int i = 0; i < count; i++) { //if (tmpstr2[startindex].IndexOf("出票时间")!=-1) if (tmpid1.Length > 0 && ticketsObjects[startindex].Content.StartsWith("名称")) { //startindex +=sellcount*8; sellProduct = new SellProductPrinter(); sellProduct.Id = ticketsObjects[startindex + 0].Id; sellProduct.Id2 = ticketsObjects[startindex + 0].Id2; sellProduct.ProductName = ticketsObjects[startindex + 0].Content.Substring(3); sellProduct.Price = ticketsObjects[startindex + 1].Content.Substring(3); sellProduct.PlayDate = ticketsObjects[startindex + 2].Content.Substring(4); sellProduct.PlayTime = ticketsObjects[startindex + 3].Content.Substring(5); sellProduct.Content = ticketsObjects[startindex + 4].Content.Substring(3); sellProduct.Cinema = ticketsObjects[startindex + 5].Content; sellProduct.HipiaoCard = ticketsObjects[startindex + 6].Content.Substring(4); sellProduct.PrintTime = ticketsObjects[startindex + 7].Content.Substring(5); tickets.Add(sellProduct); sellcount++; startindex += 8; continue; } else { tmpid1 = ticketsObjects[startindex + 0].Id; // startindex += ticketcount * 16; ticketPrintObject = new TicketPrintObject(); ticketPrintObject.Cinema = ticketsObjects[startindex + 0].Content; ticketPrintObject.UniqueId = tmpid1; ticketPrintObject.IsPrinted = false; ticketPrintObject.MovieName = ticketsObjects[startindex + 1].Content.Substring(3); ticketPrintObject.TicketId = ticketsObjects[startindex + 2].Content.Substring(3); // string ttt = ticketsObjects[startindex + 3].Content.Substring(4); ticketPrintObject.Price = Convert.ToInt32(Convert.ToDouble(ticketsObjects[startindex + 3].Content.Substring(4))); ticketPrintObject.PlayTime = ticketsObjects[startindex + 4].Content; ticketPrintObject.RoomName = ticketsObjects[startindex + 6].Content.Substring(3); ticketPrintObject.Seat = ticketsObjects[startindex + 7].Content.Substring(3); ticketPrintObject.PlayDate = ticketsObjects[startindex + 8].Content.Substring(3); if (ticketsObjects[startindex + 14].Content.StartsWith("出票时间")) { ticketPrintObject.PrintTime = ticketsObjects[startindex + 14].Content.Substring(5); } else if (ticketsObjects[startindex + 14].Content.StartsWith("手续费")) { ticketPrintObject.MiddleFee = ticketsObjects[startindex + 14].Content.Substring(5); } if (ticketsObjects[startindex + 15].Content.StartsWith("出票时间")) { ticketPrintObject.PrintTime = ticketsObjects[startindex + 15].Content.Substring(5); } else if (ticketsObjects[startindex + 15].Content.StartsWith("手续费")) { ticketPrintObject.MiddleFee = ticketsObjects[startindex + 15].Content.Substring(5); } ticketPrintObject.ChangCi = ticketsObjects[startindex + 13].Content; ticketPrintObject.SeatId = string.Empty; tickets.Add(ticketPrintObject); ticketcount++; startindex += 16; continue; } } #if DEBUG WriteLog("获取票的个数为:"+tickets.Count.ToString(), txt); WriteAllString(sbTmp.ToString()); #endif return tickets; } catch (ArgumentNullException e) { WriteLog(string.Format("ArgumentNullException: {0}", e),txt); sbTmp.Append("\r\n"); sbTmp.Append(e.ToString()); WriteAllString(sbTmp.ToString()); return null; } catch (SocketException e) { WriteLog(string.Format("SocketException: {0}", e),txt); sbTmp.Append("\r\n"); sbTmp.Append(e.ToString()); WriteAllString(sbTmp.ToString()); throw e; // return null; } #if DEBUG WriteLog("客户端连接完成!",txt); WriteAllString(sbTmp.ToString()); #endif return null; }