C# .net TCPClient 网络监听

套接字简介:套接字最早是Unix的,window是借鉴过来的。TCP/IP协议族提供三种套接字:流式、数据报式、原始套接字。其中原始套接字允许对底层协议直接访问,一般用于检验新协议或者新设备问题,很少使用。

套接字编程原理:延续文件作用思想,打开-读写-关闭的模式。

C/S编程模式如下:

Ø  服务器端:

打开通信通道,告诉本地机器,愿意在该通道上接受客户请求——监听,等待客户请求——接受请求,创建专用链接进行读写——处理完毕,关闭专用链接——关闭通信通道(当然其中监听到关闭专用链接可以重复循环)

Ø  客户端:打开通信通道,连接服务器——数据交互——关闭信道。

Socket通信方式:

Ø  同步:客户端在发送请求之后必须等到服务器回应之后才可以发送下一条请求。串行运行

Ø  异步:客户端请求之后,不必等到服务器回应之后就可以发送下一条请求。并行运行

套接字模式:

Ø  阻塞:执行此套接字调用时,所有调用函数只有在得到返回结果之后才会返回。在调用结果返回之前,当前进程会被挂起。即此套接字一直被阻塞在网络调用上。

Ø  非阻塞:执行此套接字调用时,调用函数即使得不到得到返回结果也会返回。

套接字工作步骤:

Ø  服务器监听:监听时服务器端套接字并不定位具体客户端套接字,而是处于等待链接的状态,实时监控网络状态

Ø  客户端链接:客户端发出链接请求,要连接的目标是服务器端的套接字。为此客户端套接字必须描述服务器端套接字的服务器地址与端口号。

Ø  链接确认:是指服务器端套接字监听到客户端套接字的链接请求时,它响应客户端链接请求,建立一个新的线程,把服务器端套接字的描述发送给客户端,一旦客户端确认此描述,则链接建立好。而服务器端的套接字继续处于监听状态,继续接受其他客户端套接字请求。

TCP/IP网络中,IP网络交互分类两大类:面向连接的交互与面向无连接的交互。

Socket构造函数:public socket(AddressFamily 寻址类型, SocketType 套接字类型, ProtocolType 协议类型)。但需要注意的是套接字类型与协议类型并不是可以随便组合。

SocketType

ProtocolType

描述

Stream

Tcp

面向连接

Dgram

Udp

面向无连接

Raw

Icmp

网际消息控制

Raw

Raw

基础传输协议

Socket类的公共属性:

属性名

描述

AddressFamily

获取Socket的地址族

Available

获取已经从网络接收且可供读取的数据量

Blocking

获取或设置一个值,只是socket是否处于阻塞模式

Connected

获取一个值,指示当前连接状态

Handle

获取socket的操作系统句柄

LocalEndPoint

获取本地终端EndPoint

RemoteEndPoint

获取远程终端EndPoint

ProtocolType

获取协议类型

SocketType

获取SocketType类型

Socket常用方法:

Bind(EndPoint)

服务器端套接字需要绑定到特定的终端,客户端也可以先绑定再请求连接

Listen(int)

监听端口,其中parameters表示最大监听数

Accept()

接受客户端链接,并返回一个新的链接,用于处理同客户端的通信问题

 

Send()

发送数据

Send(byte[])

简单发送数据

Send(byte[],SocketFlag)

使用指定的SocketFlag发送数据

Send(byte[], int, SocketFlag)

使用指定的SocketFlag发送指定长度数据

Send(byte[], int, int, SocketFlag)

使用指定的SocketFlag,将指定字节数的数据发送到已连接的socket(从指定偏移量开始)

Receive()

接受数据

Receive(byte[])

简单接受数据

Receive (byte[],SocketFlag)

使用指定的SocketFlag接受数据

Receive (byte[], int, SocketFlag)

使用指定的SocketFlag接受指定长度数据

Receive (byte[], int, int, SocketFlag)

使用指定的SocketFlag,从绑定的套接字接收指定字节数的数据,并存到指定偏移量位置的缓冲区

 

Connect(EndPoint)

连接远程服务器

ShutDown(SocketShutDown)

禁用套接字,其中SocketShutDown为枚举,Send禁止发送,Receive为禁止接受,Both为两者都禁止

Close()

关闭套接字,释放资源

异步通信方法:

BeginAccept(AsynscCallBack,object)

开始一个一步操作接受一个连接尝试。参数:一个委托。一个对象。对象包含此请求的状态信息。其中回调方法中必须使用EndAccept方法。应用程序调用BegineAccept方法后,系统会使用单独的线程执行指定的回调方法并在EndAccept上一直处于阻塞状态,直至监测到挂起的链接。EndAccept会返回新的socket对象。供你来同远程主机数据交互。不能使用返回的这个socket接受队列中的任何附加连接。调用BeginAccept当希望原始线程阻塞的时候,请调用WaitHandle.WaitOne方法。当需要原始线程继续执行时请在回调方法中使用ManualResetEventset方法

BeginConnect(EndPoint, AsyncCallBack, Object)

回调方法中必须使用EndConnect()方法。Object中存储了连接的详细信息。

BeginSend(byte[], SocketFlag, AsyncCallBack, Object)

 

BegineReceive(byte[], SocketFlag, AsyncCallBack, Object)

 

BegineDisconnect(bool, AsyncCallBack, Object)

 




//C# 网络监听 TCPClient 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Runtime.Serialization.Json;
namespace TCPTest
{
    public partial class TcpClient1 : Form
    {
        public int threadSendToServerErrorCount = 0;
        private static Thread threadSendToServer;
        private int gloryh = 0;//程序与设备的会话句柄
        public TcpClient1()
        {
            InitializeComponent();
            // threadSendToServer=  new Thread(new ThreadStart(ClientSendMessageToServer));

        }
        byte[] GetConMessage(MemoryStream ms)
        {
            string content = Encoding.UTF8.GetString(ms.ToArray());
            string message = string.Format("[len={0}]{1}", content.Length, content);//添加消息结束标记
            return Encoding.UTF8.GetBytes(message);
        }
        public string GetIpAddress(string input)
        {
            return input.Split(':')[0];
        }
        public class ConnectionDataMessage
        {
            /// <summary>
            /// ip地址
            /// </summary>
            public string Address { get; set; }
            /// <summary>
            /// 登录用户
            /// </summary>
            public string SignInName { get; set; }
            /// <summary>
            /// 设备号
            /// </summary>
            public string DeviceCode { get; set; }
            /// <summary>
            /// 网点
            /// </summary>
            public int BranchID { get; set; }
            /// <summary>
            /// 时间
            /// </summary>
            public DateTime NowTime { get; set; }
            /// <summary>
            /// 0:正常 1:终端机正常  2: 终端机异常
            /// </summary>
            public int ConnStatus { get; set; }

        }
        private void btnLink_Click(object sender, EventArgs e)
        {
            while(true){
            TcpClient client = new TcpClient((IPAddress.Parse("192.168.0.197")).ToString(), 60001);
            DataContractJsonSerializer dss = new DataContractJsonSerializer(typeof(ConnectionDataMessage));
            MemoryStream ms = new MemoryStream();
            ConnectionDataMessage dataMessage = new ConnectionDataMessage()
            {
                Address = GetIpAddress(client.Client.LocalEndPoint.ToString()),
                SignInName = "az",
                BranchID = 12,
                DeviceCode = "A31",
                NowTime = System.DateTime.Now,
                ConnStatus = 0
            };
            dss.WriteObject(ms, dataMessage);
            byte[] data = GetConMessage(ms);
            NetworkStream stream = client.GetStream();

            stream.Write(data, 0, data.Length);
            ms.Close();
            stream.Close();
            client.Close();
            Thread.Sleep(1000);
        }
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {

        }
    }
}

// 监听 server端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using Model;
using System.Threading;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text.RegularExpressions;
using TCRBLL;

namespace TCRNeteorkService
{
    public partial class ServerNetworker : Form
    {
        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ServerNetworker));//日志记录对象   
        TcpListener server = null;//服务端监听对象
        private System.Windows.Forms.Timer timer;//时间戳,遍历客户端连接状态
        private DeviceStatusBLL deviceStatusBLL = new DeviceStatusBLL();//业务操作

        private Dictionary<string, Model.ConnectionDataMessage> dictConnects = new Dictionary<string, ConnectionDataMessage>();//客户端连接集

合

        public ServerNetworker()
        {
            InitializeComponent();

            timer = new System.Windows.Forms.Timer();
            timer.Interval = 500;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Start();

            //获取服务器IP地址
            IPAddress hostIP = Dns.GetHostAddresses(Dns.GetHostName()).ToList().First(d => d.AddressFamily == AddressFamily.InterNetwork);
            server = new TcpListener(hostIP, Common.GloryConstant.GetServerListenPort());
            server.Start(Common.GloryConstant.GetBacklog());

            this.richTextBoxOfMessage.Text +=string.Format("({0})开启网络终端监听...\r\n",server.LocalEndpoint);
            ThreadPool.QueueUserWorkItem(new WaitCallback(AcceptWaitCallback), server);//线程池后台接受客户端连接

        }

        /// <summary>
        /// 获取IP地址, 连接集合主键,lv图片主键
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string GetIpAddress(string input)
        {
            return input.Split(':')[0];
        }

        string pattern = @"(?<=^\[len=)(\d+)(?=\])";//数据包正则,获取数据包长度

        /// <summary>
        /// 挂起连接
        /// </summary>
        /// <param name="o"></param>
        void AcceptWaitCallback(object o)
        {
            TcpListener listener = o as TcpListener;
            try
            {
                while (true)
                {
                    TcpClient client = listener.AcceptTcpClient();//接受连接
                    byte[] bytes = new byte[1024];
                    StringBuilder sbuilder = new StringBuilder();
                    string output;
                    NetworkStream stream = client.GetStream();
                    int i;
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        sbuilder.Append(System.Text.Encoding.UTF8.GetString(bytes, 0, i));
                    }

                    string data = sbuilder.ToString();
                    int len;
                    if (Regex.IsMatch(data, pattern))
                    {
                        Match m = Regex.Match(data, pattern);
                        // 获取消息字符串实际应有的长度
                        len = Convert.ToInt32(m.Groups[0].Value);
                        // 获取需要进行截取的位置
                        int startIndex = data.IndexOf(']') + 1;
                        // 获取从此位置开始后所有字符的长度
                        output = data.Substring(startIndex);
                        if (output.Length == len)
                        {
                            AnalyticalData(client, output);
                        }
                        else if (output.Length > len)
                        {
                            output = output.Substring(0, len);
                            AnalyticalData(client, output);
                        }
                        stream.Close();
                    }
                    client.Close();
                }
            }

            catch (SocketException e)
            {
                MessageBox.Show(e.ToString());
                logger.Error(e);
            }
            finally
            {
                server.Stop();
            }
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="clientSocket"></param>
        /// <param name="output"></param>
        private void AnalyticalData(TcpClient clientSocket, string output)
        {
            byte[] dataOutput = Encoding.UTF8.GetBytes(output);
            using (MemoryStream ms = new MemoryStream(dataOutput))
            {
                DataContractJsonSerializer dss = new DataContractJsonSerializer(typeof(Model.ConnectionDataMessage));
                Model.ConnectionDataMessage dataMessage = dss.ReadObject(ms) as Model.ConnectionDataMessage;// 反序列化将消息包转换成

ConnectionDataMessage对象
                string keyPoint = GetIpAddress(clientSocket.Client.RemoteEndPoint.ToString());//获取远程连接节点

                if (dictConnects.Keys.SingleOrDefault(s => s == keyPoint) == null)
                {
                    string keyForImage = GetIpAddress(clientSocket.Client.RemoteEndPoint.ToString());
                    this.listViewOfTerminal.Invoke(new Action<string>((k) =>
                    {
                        if (listViewOfTerminal.Items.ContainsKey(k))
                        {
                            this.listViewOfTerminal.Items[k].ImageIndex = 0;//如果该设备之前就已经连接过,就更改设备连接的状态,否则新建一个连

接状态
                        }
                        else
                        {
                            listViewOfTerminalBind(GetIpAddress(k));
                        }
                    }), keyForImage);

                    dictConnects.Add(keyPoint, dataMessage);//将新的终端连接加载到集合中
                    richTextBoxOfMessageInvoke(string.Format("{0},用户{1}登录终端机({2})-[设备{3}连接状态:{4}]", 
                        dataMessage.NowTime,
                        dataMessage.SignInName,
                        dataMessage.Address, 
                        dataMessage.DeviceCode,
                        dataMessage.ConnStatus==0?"正常":"异常"));
                }
                else
                {
                    dictConnects[keyPoint] = dataMessage;//更新终端连接状态
                }

                deviceStatusBLL.UpdateDeviceConnStatus(
                      dataMessage.ConnStatus == 0 ? 0 : 1,
                      dataMessage.DeviceCode);//更新数据库 终端连接状态 上线
            }
        }

        //判断下线用户
        void timer_Tick(object sender, EventArgs e)
        {
            if (dictConnects.Count > 0)
            {
                DateTime dt = System.DateTime.Now;
                var dataMessage = dictConnects.Values.ToList().Where(w =>
                     (dt - w.NowTime).TotalSeconds > 10d
                  );
                foreach (var item in dataMessage)
                {
                    deviceStatusBLL.UpdateDeviceConnStatus(2, item.DeviceCode);//更新数据库 终端连接状态 下线
                    this.listViewOfTerminal.Invoke(new Action(() => this.listViewOfTerminal.Items[item.Address].ImageIndex = 1));
                    richTextBoxOfMessageInvoke(string.Format("{0},用户{1}({2})-[设备{3}]下线",
                        item.NowTime,
                        item.SignInName,
                        item.Address,
                        item.DeviceCode));
                    dictConnects.Remove(item.Address);
                }

            }
        }

        /// <summary>
        /// 绑定连接上来的终端
        /// </summary>
        /// <param name="keyPoint"></param>
        void listViewOfTerminalBind(string keyPoint)
        {
            ListViewItem lvi = new ListViewItem(keyPoint, 0);
            lvi.Name = keyPoint;
            this.listViewOfTerminal.Invoke(new Action(() => this.listViewOfTerminal.Items.Add(lvi)));

        }

        /// <summary>
        /// 显示上线的终端信息
        /// </summary>
        /// <param name="message"></param>
        void richTextBoxOfMessageInvoke(string message)
        {
            this.richTextBoxOfMessage.Invoke(new Action<string>((m) =>
            {
                this.richTextBoxOfMessage.Text += string.Format("{0}\r\n", m);
            }), message);

        }

        //显示窗体
        private void notifyOfTCRNetwork_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.ShowDialog();
        }

        private void ServerNetworker_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();//隐藏窗体
            e.Cancel = true; //撤销退出
        }

    }
}
  • 3
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 15
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值