服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#基于Socket套接字的网络通信封装

C#基于Socket套接字的网络通信封装

2022-12-09 12:56uiuan00 C#

这篇文章主要为大家详细介绍了C#基于Socket套接字的网络通信封装本文实例为大家分享了Java实现图片旋转的具体代码,供大家参考,具体内容如下

本文为大家分享了C#基于Socket套接字的网络通信封装代码,供大家参考,具体内容如下

摘要

之所以要进行Socket套接字通信库封装,主要是直接使用套接字进行网络通信编程相对复杂,特别对于初学者而言。实际上微软从.net 2.0开始已经提供了TCP、UDP通信高级封装类如下:

TcpListener
TcpClient
UdpClient

微软从.net 4.0开始提供基于Task任务的异步通信接口。而直接使用socket封装库,很多socket本身的细节没办法自行控制,本文目就是提供一种socket的封装供参考。文中展示部分封装了TCP通信库,UDP封装也可触类旁通:

CusTcpListener
CusTcpClient

TCP服务端

TCP服务端封装了服务端本地绑定、监听、接受客户端连接,并提供了网络数据流的接口。完整代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
public class CusTcpListener
    {
        private IPEndPoint mServerSocketEndPoint;
        private Socket mServerSocket;
        private bool isActive;
 
        public Socket Server
        {
            get { return this.mServerSocket; }
        }
        protected bool Active
        {
            get { return this.isActive; }
        }
        public EndPoint LocalEndpoint
        {
            get
            {
                if (!this.isActive)
                {
                    return this.mServerSocketEndPoint;
                }
                return this.mServerSocket.LocalEndPoint;
            }
        }
        public NetworkStream DataStream
        {
            get
            {
                NetworkStream networkStream = null;
                if (this.Server.Connected)
                {
                    networkStream = new NetworkStream(this.Server, true);
                }
                return networkStream;
            }
        }
 
        public CusTcpListener(IPEndPoint localEP)
        {
            this.mServerSocketEndPoint = localEP;
            this.mServerSocket = new Socket(this.mServerSocketEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        }
 
        public CusTcpListener(string localaddr, int port)
        {
            if (localaddr == null)
            {
                throw new ArgumentNullException("localaddr");
            }
            this.mServerSocketEndPoint = new IPEndPoint(IPAddress.Parse(localaddr), port);
            this.mServerSocket = new Socket(this.mServerSocketEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        }
 
        public CusTcpListener(int port)
        {
            this.mServerSocketEndPoint = new IPEndPoint(IPAddress.Any, port);
            this.mServerSocket = new Socket(this.mServerSocketEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        }
 
        public void Start()
        {
            this.Start(int.MaxValue);
        }
        /// <summary>
        /// 开始服务器监听
        /// </summary>
        /// <param name="backlog">同时等待连接的最大个数(半连接队列个数限制)</param>
        public void Start(int backlog)
        {
            if (backlog > int.MaxValue || backlog < 0)
            {
                throw new ArgumentOutOfRangeException("backlog");
            }
            if (this.mServerSocket == null)
            {
                throw new NullReferenceException("套接字为空");
            }
            this.mServerSocket.Bind(this.mServerSocketEndPoint);
            this.mServerSocket.Listen(backlog);
            this.isActive = true;
        }
        public void Stop()
        {
            if (this.mServerSocket != null)
            {
                this.mServerSocket.Close();
                this.mServerSocket = null;
            }
            this.isActive = false;
            this.mServerSocket = new Socket(this.mServerSocketEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        }
 
        public Socket AcceptSocket()
        {
            Socket socket = this.mServerSocket.Accept();
            return socket;
        }
 
        public CusTcpClient AcceptTcpClient()
        {
            CusTcpClient tcpClient = new CusTcpClient(this.mServerSocket.Accept());
            return tcpClient;
        }
    }

TCP客户端

TCP客户端封装了客户端本地绑定、连接服务器,并提供了网络数据流的接口。完整代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
public class CusTcpClient : IDisposable
    {
        public Socket Client { get; set; }
        protected bool Active { get; set; }
        public IPEndPoint ClientSocketEndPoint { get; set; }
        public bool IsConnected { get { return this.Client.Connected; } }
        public NetworkStream DataStream
        {
            get
            {
                NetworkStream networkStream = null;
                if (this.Client.Connected)
                {
                    networkStream = new NetworkStream(this.Client, true);
                }
                return networkStream;
            }
        }
 
        public CusTcpClient(IPEndPoint localEP)
        {
            if (localEP == null)
            {
                throw new ArgumentNullException("localEP");
            }
            this.Client = new Socket(localEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            this.Active = false;
            this.Client.Bind(localEP);
            this.ClientSocketEndPoint = localEP;
        }
 
        public CusTcpClient(string localaddr, int port)
        {
            if (localaddr == null)
            {
                throw new ArgumentNullException("localaddr");
            }
            IPEndPoint localEP = new IPEndPoint(IPAddress.Parse(localaddr), port);
            this.Client = new Socket(localEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            this.Active = false;
            this.Client.Bind(localEP);
            this.ClientSocketEndPoint = localEP;
        }
        internal CusTcpClient(Socket acceptedSocket)
        {
            this.Client = acceptedSocket;
            this.Active = true;
            this.ClientSocketEndPoint = (IPEndPoint)this.Client.LocalEndPoint;
        }
 
        public void Connect(string address, int port)
        {
            if (address == null)
            {
                throw new ArgumentNullException("address");
            }
            IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(address), port);
            this.Connect(remoteEP);
        }
 
        public void Connect(IPEndPoint remoteEP)
        {
            if (remoteEP == null)
            {
                throw new ArgumentNullException("remoteEP");
            }
            this.Client.Connect(remoteEP);
            this.Active = true;
        }
 
        public void Close()
        {
            this.Dispose(true);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                IDisposable dataStream = this.DataStream;
                if (dataStream != null)
                {
                    dataStream.Dispose();
                }
                else
                {
                    Socket client = this.Client;
                    if (client != null)
                    {
                        client.Close();
                        this.Client = null;
                    }
                }
                GC.SuppressFinalize(this);
            }
        }
 
        public void Dispose()
        {
            this.Dispose(true);
        }
    }

通信实验

控制台程序试验,服务端程序:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Program
    {
        static void Main(string[] args)
        {
            Thread listenerThread = new Thread(ListenerClientConnection);
            listenerThread.IsBackground = true;
            listenerThread.Start();
 
            Console.ReadKey();
        }
 
        private static void ListenerClientConnection()
        {
            CusTcpListener tcpListener = new CusTcpListener("127.0.0.1", 5100);
            tcpListener.Start();
            Console.WriteLine("等待客户端连接……");
            while (true)
            {
                CusTcpClient tcpClient = tcpListener.AcceptTcpClient();
 
                Console.WriteLine("客户端接入,ip={0} port={1}",
                    tcpClient.ClientSocketEndPoint.Address, tcpClient.ClientSocketEndPoint.Port);
                Thread thread = new Thread(DataHandleProcess);
                thread.IsBackground = true;
                thread.Start(tcpClient);
            }
        }
 
        private static void DataHandleProcess(object obj)
        {
            CusTcpClient tcpClient = (CusTcpClient)obj;
            StreamReader streamReader = new StreamReader(tcpClient.DataStream, Encoding.Default);
            Console.WriteLine("等待客户端输入:");
            while (true)
            {
                try
                {
                    string receStr = streamReader.ReadLine();
                    Console.WriteLine(receStr);
                }
                catch (Exception)
                {
                    Console.WriteLine("断开连接");
                    break;
                }
                Thread.Sleep(5);
            }
        }
    }

客户端程序:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Program
    {
        static void Main(string[] args)
        {
            Thread listenerThread = new Thread(UserProcess);
            listenerThread.IsBackground = true;
            listenerThread.Start();
 
            Console.ReadKey();
        }
 
        private static void UserProcess()
        {
            Console.WriteLine("连接服务器");
            CusTcpClient tcpClient = new CusTcpClient("127.0.0.1", 5080);
            tcpClient.Connect("127.0.0.1", 5100);
 
            Console.WriteLine("开始和服务器通信");
            StreamWriter sw = new StreamWriter(tcpClient.DataStream, Encoding.Default);
            sw.AutoFlush = true;
            while (true)
            {
                for (int i = 0; i < 10; i++)
                {
                    string str = string.Format("第{0}次,内容:{1}", i, "测试通信");
                    Console.WriteLine("发送数据:{0}", str);
                    sw.WriteLine(str);
                }
                break;
            }
        }
    }

通信成功:

C#基于Socket套接字的网络通信封装

通过本次封装演示可实现基于Socket的通信库封装,目的就是使用Socket通信库让应用开发人员在进行网络通讯编程时无需关心底层通讯机制,而只关心应用层的开发,让开发变得更简洁。当然UDP封装类似,可自行设计。当然本文只是一种示例,实际使用可使用.net自带封装库或自定义封装。

补充:目前有很多优秀的开源Socket框架,比如SuperSocketFastSocket等。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/uiuan00/article/details/39580787

延伸 · 阅读

精彩推荐
  • C#轻松学习C#的装箱与拆箱

    轻松学习C#的装箱与拆箱

    轻松学习C#的装箱与拆箱,在之前的文章简单的提到了轻松学习C#的装箱与拆箱,本文带着大家更加详细的介绍轻松学习C#的装箱与拆箱,感兴趣的小伙伴们...

    丿木呈广予口贝5832021-11-03
  • C#c# 基于Titanium爬取微信公众号历史文章列表

    c# 基于Titanium爬取微信公众号历史文章列表

    这篇文章主要介绍了c# 基于Titanium爬取微信公众号历史文章列表,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...

    leestar546542022-11-07
  • C#详解C#中的System.Timers.Timer定时器的使用和定时自动清理内存应用

    详解C#中的System.Timers.Timer定时器的使用和定时自动清理内存应用

    这篇文章主要介绍了详解C#中的System.Timers.Timer定时器的使用和定时自动清理内存应用,需要的朋友可以参考下...

    Jugg书生11022022-01-12
  • C#c#删除指定文件夹中今天之前的文件

    c#删除指定文件夹中今天之前的文件

    本文主要介绍了c#删除指定文件夹中今天之前文件的方法,具有很好的参考价值,下面跟着小编一起来看下吧...

    冷战10682021-12-27
  • C#C#利用VS中插件打包并发布winfrom程序

    C#利用VS中插件打包并发布winfrom程序

    这篇文章主要为大家详细介绍了C#利用VS中插件打包并发布winfrom程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一...

    饮墨3492022-02-28
  • C#C#中DataTable的创建与遍历实现

    C#中DataTable的创建与遍历实现

    这篇文章主要介绍了C#中DataTable的创建与遍历实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    pan_junbiao9662022-11-02
  • C#C#实现窗体抖动的两种方法

    C#实现窗体抖动的两种方法

    这篇文章主要为大家详细介绍了C#实现窗体抖动的两种方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    英莱特——Dream11862022-10-18
  • C#C#泛型类创建与使用的方法

    C#泛型类创建与使用的方法

    这篇文章主要为大家详细介绍了C#泛型类创建与使用的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    cnc11942022-01-17