I have a Question about printing files via a Socket Connection.
Here is m Simple Code so send a file via socket to a IP Printer ->
Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
clientSocket.NoDelay = true;
IPAddress ip = IPAddress.Parse("192.168.192.6");
IPEndPoint ipep = new IPEndPoint(ip, 9100);
clientSocket.Connect(ipep);
byte[] fileBytes = File.ReadAllBytes("test.txt");
clientSocket.Send(fileBytes);
clientSocket.Close();
The Problem:
We need to print invoices on a different paper. This paper is on the second printer tray.
Example:
IP1 need to print on tray 1
IP2 need to print on tray 3
Is there a option so send a file via socket connection to print a file on the second printer tray?
Related
I am trying to develop an video chat application via UDP C#. I have an picture box that shows the webcam of the user continuously and sending to a specific port. But I don't know how to receive the Image
I am using a byte to image / image to byte converter class (which normally works fine) but when I put this code in a timer sequence I get this error
'A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself'
here is my code :
private void timer1_Tick(object sender, EventArgs e)
{
Image bitmap = pictureBox1.Image;
byte[] sndcam = imageToByteArray(bitmap);
string Ip = "127.0.0.1";
int port = 1111;
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(Ip), port);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
client.SendTo(sndcam, ep);
}
I've seen few questions like this on Stackoverflow but I cloudn't find a satisfying answer
Should I use RTP instead ? If so , How ?
Thanks in advance
I have a socat TCP listener running on Linux (SLES 12). Any client that connects will pass a string to the socket and socat will execute a script which does some work with based on the string. The script echos some output which is passed back to client.
socat TCP-LISTEN:9996,fork EXEC:/home/abhishek/hello.sh
Below is the hello.sh script.
#!/bin/bash
read str
echo "[Hello] $str" | tee -a test.txt
When I run ncat client this works as expected. ncat is able to get the data back and output it.
echo abhishek | ncat 192.168.1.12 9996
[Hello] abhishek
Now I want to connect to socat through a .NET client written in C#. Below is the raw socket based code I have come up with.
IPAddress address = IPAddress.Parse("192.168.1.12");
IPEndPoint ipe = new IPEndPoint(address, 9996);
Socket client = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipe);
byte[] msg = Encoding.ASCII.GetBytes("abhishek");
// Send the data through the socket.
int bytesSent = client.Send(msg);
Console.WriteLine("Sent {0} bytes.", bytesSent);
byte[] bytes = new byte[128];
int bytesRec = client.Receive(bytes); ;
Console.WriteLine("Response text = {0}", Encoding.ASCII.GetString(bytes, 0, bytesRec));
client.Shutdown(SocketShutdown.Both);
client.Close();
I get the line "Sent 8 bytes" but after that client hangs on Receive. The client data is received by hello.sh becaused test.txt contains new entry. When I kill the client (Ctrl+C) socat prints
2018/02/03 10:12:03 socat[21656] E write(4, 0xe530f0, 17): Broken pipe
How should I read the reply from socat in C#?
Thanks
I was able to use TcpClient to establish communication with socat and get the output from script. Below is the main part of code I implemented.
TcpClient client = new TcpClient("192.168.1.12", 9999);
NetworkStream stream = client.GetStream();
StreamWriter writer = new StreamWriter(stream) { AutoFlush = true };
StreamReader reader = new StreamReader(stream);
//Send message to socat.
writer.WriteLine(message);
//Receive reply from socat.
string reply = reader.ReadLine();
stream.close();
client.close();
I am trying to write/read to sslstream on 443 port.
I can able write data to that sslstream but while reading from that sslstream, it is waiting to read, forever.
Sample code:
var add= "IPGoesHere";
var port = 443;
var remoteIpAdd= IPaddress.
Parse("IPGoesHere");
var socket = new Socket(remoteIpAdd.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(new IPEndPoint(remoteIpAdd,port));
var netstream = new NetworkStream(
socket);
var sslstream = new SslStream(
netstream, false, callback1,
callback2);
sslstream.AuthenticateAsClient
(add,null, SslProtocols.Tls12, false);
var bytes = Encoding.Ascii.GetBytes("something");
sslstream.write( bytes,0,bytes.Length);
sslstream.Flush();
sslstream.Read(new byte[9],0,9); //Here it is waiting indefinitely..
Callback1 is a RemoteCertificateValidationCallback method and I am returning true here.
Callback2 is a Local certificate selection callback method and returning null here.
I am thinking like, how it can not able to read when it can able to write.
Could someone share some input on this issue.
PS: I typed entire code in mobile app. Regret for alignment.
I have been able to connect using TcpClient by the ip address and everything works.
I was told I need to change it to connect to the printer on the network by name, because the ip address will change. My question is, how would I go about doing this?
This is how I am doing it right now
string zplString = "zlp print instructions"
string printImage= "zlp instructions for png"
using (var client = new TcpClient())
{
client.Connect(ipAddress, 0);
using (var writer = new StreamWriter(client.GetStream()))
{
//Write ZPL String to connection
writer.Write(zplString);
writer.Flush();
writer.Write(printImage);
writer.Flush();
return;
}//Close streamwriter
}//Close Connection
}
Solved it by getting the ip address dynamically with IPHostEntry and using it in the TcpClient connection
IPHostEntry IPHost = Dns.GetHostByName(printerName);
string[] aliases = IPHost.Aliases;
I need to print on network Zebra printer. From some reasons, I cannot use winspool printing ( http://support.microsoft.com/kb/154078 ), I have to print print directly through sockets on IP and port. Here is my print method:
System.Net.Sockets.TcpClient zebraClient = new System.Net.Sockets.TcpClient();
try
{
zebraClient.SendTimeout = 5000;
zebraClient.Connect(IP, port);
}
catch (Exception ex)
{
Utils.ShowError(ex);
}
if (zebraClient.Connected)
{
NetworkStream nStream;
nStream = zebraClient.GetStream();
StreamWriter wStream;
using (nStream)
{
wStream = new StreamWriter(nStream);
using (wStream)
{
wStream.Write(content);
wStream.Flush();
}
}
zebraClient.Close();
}
Problem is, that from time to time "No connection could be created, because target computer actively refused it" exception occurs. I have no idea why is that happening (maybe full printer buffer - and if so, how can I check it in both languages?). So I ask if anybody have had this problem and how can I fix it?
Try port 9100. And make sure you can see the printers IP on the network.
Heres my VB code for this.
Private Sub sendData(ByVal zpl As String)
Dim ns As System.Net.Sockets.NetworkStream = Nothing
Dim socket As System.Net.Sockets.Socket = Nothing
Dim printerIP As Net.IPEndPoint = Nothing
Dim toSend As Byte()
Try
If printerIP Is Nothing Then
'set the IP address
printerIP = New Net.IPEndPoint(IPAddress.Parse(IP_ADDRESS), 9100)
End If
'Create a TCP socket
socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
'Connect to the printer based on the IP address
socket.Connect(printerIP)
'create a new network stream based on the socket connection
ns = New NetworkStream(socket)
'convert the zpl command to a byte array
toSend = System.Text.Encoding.ASCII.GetBytes(zpl)
'send the zpl byte array over the networkstream to the connected printer
ns.Write(toSend, 0, toSend.Length)
Catch ex As Exception
MessageBox.Show(ex.Message, "Cable Printer", MessageBoxButtons.OKCancel, MessageBoxIcon.Error)
Finally
'close the networkstream and then the socket
If Not ns Is Nothing Then
ns.Close()
End If
If Not socket Is Nothing Then
socket.Close()
End If
End Try
End Sub
AND
Private Function createString() As String
Dim command As String
command = "^XA"
command += "^LH20,25"
If rdoSmall.Checked = True Then
command += "^FO1,30^A0,N,25,25^FD"
ElseIf rdoNormal.Checked = True Then
command += "^FO1,30^A0,N,35,35^FD"
Else
command += "^FO1,30^A0,N,50,50^FD"
End If
command += txtInput.Text
command += "^FS"
command += "^XZ"
Return command
End Function
This is just for printing out text to a s4m printer.
I'm not sure if this would apply to you, but I ran into a similar problem using asp classic. I needed to print directly to a zebra printer without changing the default printer, so what I did as a solution was create a java executable that uses sockets to connect to the zebra printer. Once I had the java executable able to send a string of Zpl to the zebra printer through a stream on the open sockets I created a batch file to run my java executable. Since the executable needed a string from my asp page, I added a user input variable to the batch file. I placed these 2 files(the java jar and .bat file) on a shared drive and using ActiveX on the asp page, I was able to send raw bytes in the form of a string directly to my zebra printer. If you have any questions don't hesitate to ask. Below is a link that will help with implementing a way to socket print to a zebra printer through java.
https://km.zebra.com/kb/index?page=content&id=SO7149&actp=RSS
This worked for me:
using System.IO;
using System.Net;
using System.Net.Sockets;
.
.
.
private void btnPrint_Click(object sender, EventArgs e) {
try {
string ipAddress = txtIPAddr.Text.ToString(); ; //ie: 10.0.0.91
int port = int.Parse(txtPort.Text.ToString()); //ie: 9100
System.Net.Sockets.TcpClient client = new System.Net.Sockets.TcpClient();
client.Connect(ipAddress, port);
StreamReader reader = new StreamReader(txtFilename.Text.ToString()); //ie: C:\\Apps\\test.txt
StreamWriter writer = new StreamWriter(client.GetStream());
string testFile = reader.ReadToEnd();
reader.Close();
writer.Write(testFile);
writer.WriteLine("Hello World!");
writer.Flush();
writer.Close();
client.Close();
}
catch (Exception ex) {
MessageBox.Show(ex.Message, "Error");
}
}