narsil/Tanshu.Accounts.Print/PrinterSocket.cs

51 lines
1.5 KiB
C#
Raw Normal View History

using System;
using System.Net.Sockets;
namespace Tanshu.Accounts.Print
{
internal class PrinterSocket : PlatformPrinter
{
private string location;
// Cut Code for the printer Alt-29, Alt-86, Alt-49
internal PrinterSocket(string location)
{
this.location = location;
}
internal override bool Print(string documentName, string printingText)
{
try
{
Uri uri = new Uri(location);
TcpClient client = new TcpClient(uri.Host, uri.Port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes(printingText);
// Get a client stream for reading and writing.
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
Console.WriteLine("Sent: {0}", printingText);
// Close everything.
stream.Close();
client.Close();
return true;
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException: {0}", e);
return false;
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
return false;
}
}
}
}