Code Monkey home page Code Monkey logo

Comments (6)

beetlex-io avatar beetlex-io commented on June 1, 2024

这个是.net 内部引发的错误,请问下你是怎样使用beetlex tcp client

from beetlex.

easyboot avatar easyboot commented on June 1, 2024

public class TCPCamera
{
private TcpClient client;

    private bool TaskIsRunning = true;
    static TCPCamera instance;
    public Action<string> ReceiveTCP;
    public Action<bool> TCPConnectStatus;
    public bool IsConnect = false;
    public static TCPCamera GetInstance()
    {
        if (instance == null)
        {
            instance = new TCPCamera();
        }
        return instance;
    }

    public void Connect()
    {
        Task.Run( () =>
        {
            RunClient();
        });
    }

    private  void RunClient()
    {
        string ClassName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
        string FuncName = System.Reflection.MethodBase.GetCurrentMethod().Name;
        string msg = "";

        string strmsg1 = $"0 { util.GetNow()} connectCamera ";
        App.Log.Info(strmsg1);

        string IP = Config.GetInstance().getConfig("CameraIP");
        int Port = Convert.ToInt32(Config.GetInstance().getConfig("CameraPort"));

        if (client == null)
        {
            client = SocketFactory.CreateClient<TcpClient>(IP, Port);
            client.TimeOut = 300000;
            client.Encoding = Encoding.GetEncoding(936);
        }

        while (TaskIsRunning)
        {
            try
            {                    
                var read = client.Receive();
                var str = read.ReadToEnd();

                msg = @$"
                            Class Name [{ClassName}]
                            Function   [{FuncName}]
                            RecvTime   [{util.GetNow()}]
                            Parameter
                            Message 接收Vision命令   [{str}]";
                App.Log.Info(msg);

                ReceiveTCP(str);
                TCPConnectStatus(client.IsConnected);
            }
            catch (Exception e)
            {
                TCPConnectStatus(false);
                msg = @$"
                            Class Name [{ClassName}]
                            Function   [{FuncName}]
                            RecvTime   [{util.GetNow()}]
                            Parameter
                            Message RunClient 视觉网络异常  : {e.Message}  HResult: {e.HResult} 
                         ";
                App.Log.Error(msg);
            }
        }
    }

    public void Send(string cmd)
    {
        try
        {
            TCPConnectStatus(client.IsConnected);
            if (client.IsConnected)
            {
                client.Stream.ToPipeStream().Write(cmd+"\r\n");
                client.Stream.Flush();

                string ClassName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
                string FuncName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                string msg = @$"
                                Class Name [{ClassName}]
                                Function   [{FuncName}]
                                RecvTime   [{util.GetNow()}]
                                Parameter
                                Message 发送命令到视觉  [{cmd}]";
                App.Log.Info(msg);
            }
            else
            {
                string ClassName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
                string FuncName = System.Reflection.MethodBase.GetCurrentMethod().Name;
                string msg = @$"
                                Class Name [{ClassName}]
                                Function   [{FuncName}]
                                RecvTime   [{util.GetNow()}]
                                Parameter
                                Message  视觉网络未连接,命令发送失败  [{cmd}]
                                ";
                App.Log.Warn(msg);
            }
        }
        catch (Exception ex)
        {
            string ClassName = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
            string FuncName = System.Reflection.MethodBase.GetCurrentMethod().Name;
            string msg = @$"
                                Class Name [{ClassName}]
                                Function   [{FuncName}]
                                RecvTime   [{util.GetNow()}]
                                Parameter
                                Message  视觉通讯出现异常  [{ex.Message}]";          
            App.Log.Error(msg);
        }
    }

    public void Dispose()
    {
        TaskIsRunning = false;
        client.DisConnect();
        client.Dispose();
        client = null;
    }
}

from beetlex.

beetlex-io avatar beetlex-io commented on June 1, 2024

pipestream不是线程安全的,Send方法不能多线程发数据。
看一下具体的socketException errorcode是多什么

from beetlex.

easyboot avatar easyboot commented on June 1, 2024

[ 一个封锁操作被对 WSACancelBlockingCall 的调用中断。 HResult: -2147467259]

from beetlex.

beetlex-io avatar beetlex-io commented on June 1, 2024

看能不能把异常转换成socketexception看对应的errorcode

from beetlex.

beetlex-io avatar beetlex-io commented on June 1, 2024

找到一个winsock描述,应该是说不要在gui线程中使用对应的同步阻塞
The WSACancelBlockingCall function has been removed in compliance with the Windows Sockets 2 specification, revision 2.2.0.

The function is not exported directly by WS2_32.DLL and Windows Sockets 2 applications should not use this function. Windows Sockets 1.1 applications that call this function are still supported through the WINSOCK.DLL and WSOCK32.DLL.

Blocking hooks are generally used to keep a single-threaded GUI application responsive during calls to blocking functions. Instead of using blocking hooks, an applications should use a separate thread (separate from the main GUI thread) for network activity.

from beetlex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.