基于Socket 实现.net服务作为服务端进行信息接收

发布于 2022年 05月 19日 17:36

1、绑定本机的IP和端口实现信息接收

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("192.168.1.10"), 10001);//本机预使用的IP和端口
                LogHelper.Instance.AddErrorLogs("本机ip" + ipep);
                Socket newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                newsock.Bind(ipep);//绑定
                LogHelper.Instance.AddErrorLogs("绑定成功");
                newsock.Listen(10);//监听
                LogHelper.Instance.AddErrorLogs("开始监听");
                Thread thread = new Thread(DoWork);
                thread.Start(newsock);

2、开始工作

public void DoWork(object newsock)
        {
            while (true)
            {
                try
                {
                    Socket client = ((Socket)newsock).Accept();//当有可用的客户端连接尝试时执行,并返回一个新的socket,用于与客户端之间的通信
                    IPEndPoint clientip = (IPEndPoint)client.RemoteEndPoint;
                    LogHelper.Instance.AddErrorLogs("通讯ip" + clientip);
                    Thread th2 = new Thread(Rec);
                    th2.Start(client);
                }
                catch (Exception ex)
                {
                    ((Socket)newsock).Close();
                    ((Socket)newsock).Disconnect(true);
                    ((Socket)newsock).ReceiveTimeout = 60000;
                    ((Socket)newsock).Blocking = true;
                    LogHelper.Instance.AddErrorLogs("错误信息1" + ex.ToString());
                    downloaddRun = false;
                    break;
                }
            }
        }

3、进行消息的接收,并进行数据入库

//不断接收消息
        private void Rec(object client)
        {
            while (true)
            {
                try
                {
                    T_MON_QIXIANGJIANCEEntity t_mon_qx = new T_MON_QIXIANGJIANCEEntity();
                    byte[] data = new byte[1024];
                    int recv = ((Socket)client).Receive(data);
                    //说明客户端下线了
                    if (recv == 0)
                    {
                        break;
                    }
                    LogHelper.Instance.AddErrorLogs("recv=" + recv);
                    string strDatas = "";
                    string stringdata = Encoding.GetEncoding("GB2312").GetString(data, 0, recv);
            //stringdata就是接收到消息 strDatas
= stringdata; //下面是数据入库操作 if (strDatas.Trim().Substring(strDatas.Length - 2).ToUpper() == "GG" && strDatas.Trim().Substring(0, 1) == "#") { LogHelper.Instance.writeLogs("监测数据:" + strDatas, "data"); t_mon_qx = Deserialize(strDatas, "123", "XX气象站"); string strSql = DataCore.GetInsertSQL(t_mon_qx, mQxTVData, mQxTVData.ObjectName); int count = mDbHelper.ExecuteSql(strSql); LogHelper.Instance.writeLogs("sql:" + strSql, "sql"); strDatas = ""; } } catch (Exception ex) { ((Socket)client).Close(); ((Socket)client).Disconnect(true); ((Socket)client).ReceiveTimeout = 60000; ((Socket)client).Blocking = true; LogHelper.Instance.AddErrorLogs("错误信息2" + ex.ToString()); downloaddRun = false; break; } } }

 4、测试工具在https://download.csdn.net/download/duangufei/85415583

5、测试工具效果如图

 

推荐文章