C# .NET print to default Printer - c#

I need to update this program to print to whichever printer the user has selected as their default printer. Currently I'm using the IP address of the printer but it will be different depending on the pc.
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.NoDelay = true;
PrinterSettings settings = new PrinterSettings();
IPAddress ip = IPAddress.Parse("ipaddress");
IPEndPoint ipep = new IPEndPoint(ip, 9100);
clientSocket.Connect(ipep);
try {
clientSocket.Send(output.ToArray());
} catch (Exception) {}

Related

epson ip printer insert cut-line after printing

I have below code to print a simple txt file on a epson ip ticket printer. How can I add a paper cut line / paper cut?
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.NoDelay = true;
IPAddress ip = IPAddress.Parse("000.000.000.000");
IPEndPoint ipep = new IPEndPoint(ip, 9100);
clientSocket.Connect(ipep);
byte[] fileBytes = File.ReadAllBytes("C:/xxx/xxx.txt");
clientSocket.Send(fileBytes);
clientSocket.Close();

UDP Multicast not receiving data

I'm trying to write a simple client to receive multicast data. I've tried many different iterations and none of them work. The environment that I am using is .netcore 2.2.2 and in a linux environment. I can see that the data is being sent to the proper interface via tcpdump but my client always gets stuck at the receive. I also have a program that was written in Java by a previous developer which properly receives data from interfaces below so I can confirm that the route does indeed work.
Attempt #1
mcastSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram,
ProtocolType.Udp);
IPAddress localIPAddr = IPAddress.Parse("10.51.254.2");
EndPoint localEP = (EndPoint)new IPEndPoint(localIPAddr, mcastPort);
mcastSocket.Bind(localEP);
mcastOption = new MulticastOption(mcastAddress, localIPAddr);
mcastSocket.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.AddMembership,
mcastOption);
EndPoint remoteEp = new IPEndPoint(mcastAddress, mcastPort);
// I have tried using (localIPAddr, mcastPort), (IPAddress.Any, 0),
// (localIPAddr, 0), (IPAddress.Any, mcastPort)
byte[] arr = new byte[4096];
Console.WriteLine("Socket setup");
var receivedBytes = mcastSocket.ReceiveFrom(arr, ref remoteEp );
Console.WriteLine("Got data");
Attempt#2
UdpClient client = new UdpClient();
client.ExclusiveAddressUse = false;
IPEndPoint localEp = new IPEndPoint(IPAddress.Parse("10.51.254.2"), 14382);
client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
client.ExclusiveAddressUse = false;
client.Client.Bind(localEp);
client.JoinMulticastGroup(IPAddress.Parse("224.0.31.130"), IPAddress.Parse("10.51.254.2"));
Console.WriteLine("Listening this will never quit so you will need to ctrl-c it");
IPEndPoint remoteEp = new IPEndPoint(IPAddress.Parse("224.0.31.130"), 14382);
EndPoint remoteEndPoint = (EndPoint) new IPEndPoint(IPAddress.Any, 0);
while (true)
{
Byte[] data = client.Receive(ref remoteEndPoint);
Console.WriteLine("got data");
}
Any help would be greatly appreciated.
FIXED using the following code
try
{
mcastSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress localIPAddr = IPAddress.Parse("10.51.254.2");
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, mcastPort);
mcastSocket.Bind(localEP);
MulticastOption option = new MulticastOption(mcastAddress, localIPAddr);
mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, option);
EndPoint remoteEp = new IPEndPoint(localIPAddr, mcastPort);
byte[] arr = new byte[4096];
while (true)
{
var receivedBytes = mcastSocket.ReceiveFrom(arr, ref remoteEp);
Console.WriteLine($"Got data {receivedBytes}");
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}

How can I add SSL encryption to available TCP/IP socket

I am going to upgrade a system which used TCP connection without SSL. I want to add SSL encryption to the current connection without changing much in code. This is what there was previously in the code.
IPAddress ipAddress = IPAddress.Parse(IpAddress);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, Port);
// Create a TCP/IP socket.
var client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(remoteEP,new AsyncCallback(ConnectCallback), client);
What I tried is this,
IPAddress ipAddress = IPAddress.Parse(IpAddress);
IPEndPoint remoteEP = new IPEndPoint(ipAddress, Port);
// Create a TCP/IP socket.
var client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
while (!client.Connected) { }
NetworkStream stream = new NetworkStream(client);
SslStream ssl = new SslStream(stream, false, ValidateServerCertificate);
But this is not doing what I need. At least ValidateServerCertificate delegate is not called.
What's wrong with my code?
IMPORTANT
I cannot remove this socket named client during the change because it will affect lot of other codes.

Sending and Receiving UDP packets

The following code sends a packet on port 15000:
int port = 15000;
UdpClient udp = new UdpClient();
//udp.EnableBroadcast = true; //This was suggested in a now deleted answer
IPEndPoint groupEP = new IPEndPoint(IPAddress.Broadcast, port);
string str4 = "I want to receive this!";
byte[] sendBytes4 = Encoding.ASCII.GetBytes(str4);
udp.Send(sendBytes4, sendBytes4.Length, groupEP);
udp.Close();
However, it's kind of useless if I can't then receive it on another computer. All I need is to send a command to another computer on the LAN, and for it to receive it and do something.
Without using a Pcap library, is there any way I can accomplish this? The computer my program is communicating with is Windows XP 32-bit, and the sending computer is Windows 7 64-bit, if it makes a difference. I've looked into various net send commands, but I can't figure them out.
I also have access to the computer (the XP one)'s local IP, by being able to physically type 'ipconfig' on it.
EDIT: Here's the Receive function I'm using, copied from somewhere:
public void ReceiveBroadcast(int port)
{
Debug.WriteLine("Trying to receive...");
UdpClient client = null;
try
{
client = new UdpClient(port);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
IPEndPoint server = new IPEndPoint(IPAddress.Broadcast, port);
byte[] packet = client.Receive(ref server);
Debug.WriteLine(Encoding.ASCII.GetString(packet));
}
I'm calling ReceiveBroadcast(15000) but there's no output at all.
Here is the simple version of Server and Client to send/receive UDP packets
Server
IPEndPoint ServerEndPoint= new IPEndPoint(IPAddress.Any,9050);
Socket WinSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
WinSocket.Bind(ServerEndPoint);
Console.Write("Waiting for client");
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0)
EndPoint Remote = (EndPoint)(sender);
int recv = WinSocket.ReceiveFrom(data, ref Remote);
Console.WriteLine("Message received from {0}:", Remote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
Client
IPEndPoint RemoteEndPoint= new IPEndPoint(
IPAddress.Parse("ServerHostName"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
string welcome = "Hello, are you there?";
data = Encoding.ASCII.GetBytes(welcome);
server.SendTo(data, data.Length, SocketFlags.None, RemoteEndPoint);

Winforms C# app using sockets works under winXp, but throws error under Windows 7

Here's the properties and the method that connects.
protected Socket _socketConnection =
new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
private string _host = "";
private string _hostIpAddress = "";
private int _port = 0;
public void Connect()
{
// don't allow two connections
if (_socketConnection.Connected)
return;
// get the ip address from the hostname
IPHostEntry ipHostEntry = Dns.GetHostByName(_host);
_hostIpAddress = ipHostEntry.AddressList[0].ToString();
// create the socket endpoint
IPAddress ipAddress = IPAddress.Parse(_hostIpAddress);
IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, _port);
// connect
try
{
_socketConnection.Connect(ipEndPoint);
if (OnConnect != null)
OnConnect();
}
catch
{
throw;
}
}
When I run the app under Windows 7 I get the following error:
An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call.
I've seen messages that talk about setting a particular option on the socket, but this is an app that has been working for years and is only happening when this app is installed on Windows 7.
Is there a compatibility flag to tweak or something?
Thanks!
Perhaps on Win7 you get a IPv6 as the _hostIpAddress. Try using something like this when instantiating the socket:
if(Socket.OSSupportsIPv6 && _hostIpAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
// newer OS
_socketConnection = new Socket(
AddressFamily.InterNetworkV6,
SocketType.Stream,
ProtocolType.Tcp);
_socketConnection.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);
} else {
// older OS
_socketConnection = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
}

Categories