試圖寫網絡流套接字,但得到以下錯誤:的TcpClient:無連接可以作出,因爲目標機器積極地拒絕它
No connection could be made because the target machine actively refused it
應用具有打印作業的併發名單,所以當我們有東西要打印,檢查TCP客戶端是否連接,如果沒有連接,獲取流,寫入流和刷新。
示例代碼如下:
/// <summary> Print via TCP </summary>
private void PrintViaTcp(object a)
{
Debug.WriteLine($"Printing via TCP...");
var labelData = a as LabelData;
// Send data to the label printer
SendToLabelPrinter(labelData.Commands, labelData.PrinterAddress, labelData.Port);
Debug.WriteLine(" -> Printed labelId : {0}", labelData.LabelId);
}
/// <summary> Send the native command to the label printer </summary>
public void SendToLabelPrinter(string commands, string address, int? port)
{
//Connect to tcp client if a connection does not exist
if (!TcpClient.Connected) TcpClient.Connect(address, port ?? 9100);
//Get the network stream
var stream = TcpClient.GetStream();
// Send command strings to printer
//stream.Write(Encoding.ASCII.GetBytes(commands), 0, commands.Length);
stream.Write(Encoding.GetEncoding(850).GetBytes(commands), 0, commands.Length);
//Flush the buffer on the network stream
stream.Flush();
}
這適用於小型打印作業< 100次印刷,但是當我去了100,誤差是隨機發生的失蹤打印。
這似乎是一個併發問題。您應該檢查打印機服務器可以處理多少個併發作業 – yorodm