using System.Net.Sockets;
using System.Net;
using System.Runtime.InteropServices;
namespace fsd
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
private static bool SendBytesToPrinter(string printerName, IntPtr intptrBytes, Int32 count)
{
Int32 error = 0, written = 0;
IntPtr intptrPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false;
di.pDocName = "Test Document";
di.pDataType = "RAW";
if (OpenPrinter(printerName.Normalize(), out intptrPrinter, IntPtr.Zero))
{
if (StartDocPrinter(intptrPrinter, 1, di))
{
if (StartPagePrinter(intptrPrinter))
{
bSuccess = WritePrinter(intptrPrinter, intptrBytes, count, out written);
EndPagePrinter(intptrPrinter);
}
EndDocPrinter(intptrPrinter);
}
ClosePrinter(intptrPrinter);
}
if (bSuccess == false)
{
error = Marshal.GetLastWin32Error();
}
return bSuccess;
}
Socket _socket;
Dictionary<string, Socket> dicSocket = new Dictionary<string, Socket>();
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.58"), 9178);
try
{
_socket.Bind(endpoint);
}
catch (Exception ee)
{
MessageBox.Show("连接失败");
}
_socket.Listen(10);
while (true)
{
Socket sockClient = _socket.Accept();
while (true)
{
byte[] Receivedata = new byte[1024];
int length = -1;
try
{
length = sockClient.Receive(Receivedata);
}
catch
{
break;
}
if (length == 0)
{
sockClient.Close();
break;
}
else
{
string data = BitConverter.ToString(Receivedata);
data = data.Replace("-", "");
int Data_head = data.IndexOf("52454D4F5445315449");
int End_data = data.IndexOf("4A450100001B000000");
if (data.Contains("804420FB00144008020820820040411101"))
{
data = data.Replace("804420FB00144008020820820040411101", "5A4420FB001440080345A33C0040411108");
}
string printer_name = "Printer Name";
byte[] outdata = Enumerable.Range(0, data.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(data.Substring(x, 2), 16))
.ToArray();
IntPtr pUnmanagedBytes = Marshal.AllocCoTaskMem(outdata.Length);
Marshal.Copy(outdata, 0, pUnmanagedBytes, outdata.Length);
if (pUnmanagedBytes != 0)
{
SendBytesToPrinter(printer_name, pUnmanagedBytes, outdata.Length);
}
}
}
}
}
else
{
_socket.Close();
}
}
}
}