多线程套接字连接挂起c#.套接字超时/挂起 [英] Multithreaded socket connection hangs c#. Socket timeout/hangs

查看:109
本文介绍了多线程套接字连接挂起c#.套接字超时/挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过套接字连接将打印指令发送到两台打印机.该脚本可以正常运行,然后挂起并报告:

I'm attempting to send print instructions to two printers over a socket connection. The script works fine then hangs and reports back:

System.IO.IOException:无法将数据写入传输 连接:不允许发送或接收数据的请求,因为 插座已经朝那个方向被关闭了, 先前的关机呼叫

System.IO.IOException: Unable to write data to the transport connection: A request to send or receive data was disallowed because the socket had already been shut down in that direction with a previous shutdown call

System.Net.Sockets.SocketException(0x80004005):连接尝试失败,因为被连接方未正确响应 一段时间后,或由于以下原因建立的连接失败 连接的主机无法响应

System.Net.Sockets.SocketException (0x80004005): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

该问题是断断续续的,我无法通过附加的调试器进行重现.

The issue is intermittent and I haven't been able to reproduce with a debugger attached.

任何人都可以发现可能导致此现象的原因吗?我在线程安全方面遇到了问题,我认为这应该归咎于此.

Can anyone spot what could be causing this behavior? I've had issue with thread safety which I believe could be to blame.

致歉的代码量.由于这种可能性仅限于线程处理,因此我已尽可能地包含了范围.

Apologies for the amount of code. Due to the possibility of this being down to threading I've included as much scope as I can.

// main entry point
class HomeController{

    List<string> lsLabelResults = new List<string>("label result");
    PrinterBench pbBench        = new PrinterBench("192.168.2.20","192.168.2.21");

    void Process(){

        oPrintController = new PrintController(this);

        if(GetLabel()){
            // should always come out of the big printer (runs in background)
            oPrintController.PrintBySocketThreaded(lsLabelResults, pbBench.PostageLabelIP);
            // should always come out of the small printer
            oPrintController.PrintWarningLabel();
        }
    }
}

class PrintController{

    HomeController oHC;

    private static Dictionary<string, Socket> lSocks = new Dictionary<string, Socket>();

    private BackgroundWorker _backgroundWorker;
    static readonly object locker = new object();
    double dProgress;
    bool bPrintSuccess = true;


    public PrintController(HomeController oArg_HC)
    {
        oHC = oArg_HC;
    }


    public bool InitSocks()
    {
        // Ensure the IP's / endpoints of users printers are assigned
        if (!lSocks.ContainsKey(oHC.pbBench.PostageLabelIP))
        {
            lSocks.Add(oHC.pbBench.PostageLabelIP, null);
        }
        if (!lSocks.ContainsKey(oHC.pbBench.SmallLabelIP))
        {
            lSocks.Add(oHC.pbBench.SmallLabelIP, null);
        }

        // attempt to create a connection to each socket
        try
        {
            foreach (string sKey in lSocks.Keys.ToList())
            {
                if (lSocks[sKey] == null || !lSocks[sKey].Connected)
                {
                    IPEndPoint ep = new IPEndPoint(IPAddress.Parse(sKey), 9100);
                    lSocks[sKey] = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                    try
                    {

                        //lSocks[sKey].Connect(ep);
                        var result = lSocks[sKey].BeginConnect(ep, null, null);

                        bool success = result.AsyncWaitHandle.WaitOne(2000, true);
                        // dont need success
                        if (lSocks[sKey].Connected)
                        {
                            lSocks[sKey].EndConnect(result);
                        }
                        else
                        {
                            lSocks[sKey].Close();
                            throw new SocketException(10060); // Connection timed out.
                        }

                    }
                    catch(SocketException se)
                    {
                        if(se.ErrorCode == 10060)
                        {
                            oHC.WriteLog("Unable to init connection to printer. Is it plugged in?", Color.Red);
                        }
                        else
                        {
                            oHC.WriteLog("Unable to init connection to printer. Error: " + se.ErrorCode.ToString(), Color.Red);
                        }
                    }
                    catch (Exception e)
                    {
                        oHC.WriteLog("Unable to init connection to printer. Error: " + e.ToString(), Color.Red);
                    }
                }
            }
        }catch (Exception e)
        {
            oHC.WriteLog(e.ToString(), true);
            return false;
        }

        return true;
    }

    public bool PrintBySocketThreaded(List<string> lsToPrint, string sIP)
    {
        // open both the sockets
        InitSocks();

        bBatchPrintSuccess = false;
        _backgroundWorker = new BackgroundWorker();

        _backgroundWorker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
        _backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
        _backgroundWorker.WorkerReportsProgress = true;
        _backgroundWorker.WorkerSupportsCancellation = true;

        object[] parameters = new object[] { lsToPrint, sIP, lSocks };

        _backgroundWorker.RunWorkerAsync(parameters);
        return true;
    }


    // On worker thread, send to print!
    public void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        object[] parameters = e.Argument as object[];

        double dProgressChunks = (100 / ((List<string>)parameters[0]).Count);
        int iPos = 1;

        Dictionary<string, Socket> dctSocks = (Dictionary<string, Socket>)parameters[2];

        foreach (string sLabel in (List<string>)parameters[0] )
        {
            bool bPrinted = false;

            // thread lock print by socket to ensure its not accessed twice
            lock (locker)
            {
                // get relevant socket from main thread
                bPrinted = PrintBySocket(sLabel, (string)parameters[1], dctSocks[(string)parameters[1]]);
            }

            iPos++;
        }

        while (!((BackgroundWorker)sender).CancellationPending)
        {
            ((BackgroundWorker)sender).CancelAsync();
            ((BackgroundWorker)sender).Dispose();
            //Thread.Sleep(500);
        }
        return;
    }


    // Back on the 'UI' thread so we can update the progress bar (have access to main thread data)!
    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null) MessageBox.Show(e.Error.Message);
        if (bPrintSuccess) oHC.WriteLog("Printing Complete");

        bBatchPrintSuccess = true;

        ((BackgroundWorker)sender).CancelAsync();
        ((BackgroundWorker)sender).Dispose();
    }

    /// sends to printer via socket
    public bool PrintBySocket(string sArg_ToPrint, string sIP, Socket sock = null)
    {
        Socket sTmpSock = sock;

        if (sTmpSock == null)
        { 
            InitSocks();

            if (!lSocks.ContainsKey(sIP))
            {
                throw new Exception("Sock not init");
            }
            else
            {
                sTmpSock = lSocks[sIP];
            }
        }

        try
        {

            if(!sTmpSock.Connected || !sTmpSock.IsBound)
            {
                InitSocks();

                if (!sTmpSock.Connected)
                {
                    oHC.WriteLog("Unable to init connection to printer. Is it plugged in?", Color.Red);
                }
            }

            using (NetworkStream ns = new NetworkStream(sTmpSock))
            {
                byte[] toSend = null;

                // convert string to byte stream, or use byte stream
                if (byToPrint == null)
                {
                    toSend = Encoding.ASCII.GetBytes(sEOL + sArg_ToPrint);
                }
                else
                {
                    toSend = byToPrint;
                }

                ns.BeginWrite(toSend, 0, toSend.Length, OnWriteComplete, null);
                ns.Flush();
            }

            return true;

        }
        catch (Exception e)
        {
            oHC.WriteLog("Print by socket: " + e.ToString(), true);
            DisposeSocks();
        }
    }


    public bool PrintWarningLabel()
    {
        string sOut = sEOL + "N" + sEOL;
        sOut += "A0,150,0,4,3,3,N,\"WARNING MESSAGE TO PRINT\"" + sEOL;
        sOut += sEOL;

        if (PrintBySocket(sOut, oHC.pbBench.SmallLabelIP))
        {
            oHC.WriteLog("WARNING LABEL PRINTED");
            return true;
        }
        return false;
    }
}

推荐答案

似乎您正在连接到TCP服务器并发送一些数据.在现代的.NET中,您不需要显式的多线程处理,因此async-await大大简化了事情.这就是我可能会这样做的方式.

It appears you’re connecting to TCP servers and sending some data. In modern .NET you don’t need explicit multithreading for that, async-await simplified things a lot. Here’s how I would probably do that.

static class NetworkPrinter
{
    const ushort tcpPort = 31337;
    const string sEOL = "\r\n";

    /// <summary>Send a single job to the printer.</summary>
    static async Task sendLabel( Stream stream, string what, CancellationToken ct )
    {
        byte[] toSend = Encoding.ASCII.GetBytes( sEOL + what );
        await stream.WriteAsync( toSend, 0, toSend.Length, ct );
        await stream.FlushAsync();
    }

    /// <summary>Connect to a network printer, send a batch of jobs reporting progress, disconnect.</summary>
    public static async Task printLabels( string ip, string[] labels, Action<double> progress, CancellationToken ct )
    {
        IPAddress address = IPAddress.Parse( ip );
        double progressMul = 1.0 / labels.Length;
        using( var tc = new TcpClient() )
        {
            await tc.ConnectAsync( address, tcpPort );
            Stream stream = tc.GetStream();
            for( int i = 0; i < labels.Length; )
            {
                ct.ThrowIfCancellationRequested();
                await sendLabel( stream, labels[ i ], ct );
                i++;
                progress( i * progressMul );
            }
        }
    }

    /// <summary>Send multiple batches to multiple printers, return true of all of them were good.</summary>
    public static async Task<bool> printBatches( LabelBatch[] batches )
    {
        await Task.WhenAll( batches.Select( a => a.print( CancellationToken.None ) ) );
        return batches.All( a => a.completed );
    }
}

/// <summary>A batch of labels to be printed by a single printer.</summary>
/// <remarks>Once printed, includes some status info.</remarks>
class LabelBatch
{
    readonly string ip;
    readonly string[] labels;
    public bool completed { get; private set; } = false;
    public Exception exception { get; private set; } = null;

    public LabelBatch( string ip, IEnumerable<string> labels )
    {
        this.ip = ip;
        this.labels = labels.ToArray();
    }

    /// <summary>Print all labels, ignoring the progress. This method doesn't throw, returns false if failed.</summary>
    public async Task<bool> print( CancellationToken ct )
    {
        completed = false;
        exception = null;
        try
        {
            await NetworkPrinter.printLabels( ip, labels, d => { }, ct );
            completed = true;
        }
        catch( Exception ex )
        {
            exception = ex;
        }
        return completed;
    }
}

如果您没有用于报告进度的同步上下文(例如,您正在编写控制台应用程序),则可能仍需要手动进行线程同步.您可以从多个打印机获取并行调用的进度回调.进度报告应该很快,静态对象上的单个lock()可以完成.如果您正在构建GUI应用程序并从GUI线程调用printBatches,则不需要这样做,尽管网络请求仍将并行运行,但sync.context会将所有内容序列化为GUI线程.

You might still need manual thread synchronization if you don’t have a synchronization context (e.g. you’re writing a console app) to report progress. You can get progress callbacks called in parallel from multiple printers. Progress reporting should be fast, a single lock() on a static object will do. Not required if you’re building a GUI app and calling printBatches from the GUI thread, the sync.context will serialize everything to the GUI thread, despite network requests will still run in parallel.

这篇关于多线程套接字连接挂起c#.套接字超时/挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆