我在打印到我们的条码打印机时遇到了一些问题。代码运行良好,但我无法立即打印另一个代码,但需要等待几秒钟。我的代码有什么问题吗?
提前感谢任何人。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace PrintToZebra
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("start printing to: 10.160.2.254:9100");
string text = "^XA" +
"^FO335,22,^CI0^A0,14,14^FR^FDConta^FS" +
"^FO368,22,^CI0^A0,14,14^FR^FDins^FS" +
"^PQ1" +
"^XZ";
printToIP("10.160.2.254", 9100, text);
}
public static void printToIP(string ipAddress, int printerPort, string content)
{
try
{
EndPoint ep = new IPEndPoint(IPAddress.Parse(ipAddress), printerPort);
//EndPoint ep = new IPEndPoint(parseIP(ipAddress),printerPort);
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
sock.Connect(ep);
NetworkStream ns = new NetworkStream(sock);
byte[] toSend = Encoding.ASCII.GetBytes(content);
ns.Write(toSend, 0, toSend.Length);
sock.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
}
}