{"id":69005,"date":"2026-01-31T00:48:47","date_gmt":"2026-01-30T16:48:47","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/69005.html"},"modified":"2026-01-31T00:48:47","modified_gmt":"2026-01-30T16:48:47","slug":"c-tcp%e6%95%b0%e6%8d%ae%e5%a4%84%e7%90%86%e7%bb%88%e6%9e%81%e6%8c%87%e5%8d%97%ef%bc%9a%e4%bb%8e100ms%e5%88%b01ms%e7%9a%84%e6%80%a7%e8%83%bd%e9%a3%9e%e8%b7%83%ef%bc%8c%e5%bf%83%e8%b7%b3-%e7%b2%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/69005.html","title":{"rendered":"C# TCP\u6570\u636e\u5904\u7406\u7ec8\u6781\u6307\u5357\uff1a\u4ece100ms\u52301ms\u7684\u6027\u80fd\u98de\u8dc3\uff0c\u5fc3\u8df3\/\u7c98\u5305\/SSL\u5168\u89e3\u5bc6\uff01"},"content":{"rendered":"<p>\u4e00\u3001\u4e3a\u4ec0\u4e48TCP\u8fde\u63a5\u603b\u7ffb\u8f66&#xff1f;&#xff08;\u75db\u70b9\u66b4\u51fb&#xff09;<br \/>\n\u201c\u4e0d\u662f\u7f51\u7edc\u4e0d\u884c&#xff0c;\u662f\u5904\u7406\u903b\u8f91\u592a\u7cd9&#xff01;\u201d<\/p>\n<p>\u53bb\u5e74&#xff0c;\u67d0\u793e\u4ea4App\u7684TCP\u8fde\u63a5\u65b9\u6848&#xff1a;<br \/>\n\/\/ \u65e7\u7248\u8fde\u63a5&#xff1a;\u7b80\u5355Socket&#xff0c;\u65e0\u5fc3\u8df3&#xff0c;\u65e0\u8d85\u65f6<br \/>\npublic class OldTcpConnection<br \/>\n{<br \/>\nprivate Socket _socket;<\/p>\n<p>public void Connect(string host, int port)<br \/>\n{<br \/>\n    _socket &#061; new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);<br \/>\n    _socket.Connect(host, port); \/\/ 1. \u65e0\u8d85\u65f6\u8bbe\u7f6e<br \/>\n}<\/p>\n<p>public void Send(byte[] data)<br \/>\n{<br \/>\n    _socket.Send(data); \/\/ 2. \u65e0\u7c98\u5305\u5904\u7406<br \/>\n}<\/p>\n<p>}<\/p>\n<p>\u7ed3\u679c&#xff1a;<\/p>\n<ul>\n<li>\u8fde\u63a5\u65ad\u5f00\u738735%&#xff08;10\u4e07\u8fde\u63a5\u4e2d3.5\u4e07\u7a81\u7136\u65ad\u5f00&#xff09;<\/li>\n<li>\u6570\u636e\u7c98\u5305\u738725%&#xff08;1000\u6761\u6570\u636e\u4e2d250\u6761\u4e71\u7801&#xff09;<\/li>\n<li>\u6700\u540e\u53d1\u73b0&#xff1a;\u6211\u5fd8\u4e86\u505a\u5fc3\u8df3&#043;\u8d85\u65f6&#043;\u7c98\u5305\u5904\u7406&#xff01;<br \/>\n&#x1f4a1; \u8840\u6cea\u6559\u8bad&#xff1a;TCP\u4e0d\u662f&#034;\u80fd\u8fde\u5c31\u884c&#034;&#xff0c;\u662f&#034;\u7a33\u5b9a&#043;\u9ad8\u6548&#034;&#xff01;<\/li>\n<\/ul>\n<p>\u4e8c\u30015\u4e2a\u5fc5\u987b\u7528\u7684TCP\u5904\u7406\u6838\u5fc3\u7ef4\u5ea6&#xff08;\u751f\u4ea7\u73af\u5883\u5b9e\u6d4b&#xff09;<br \/>\n\u201c\u7ef4\u5ea6\u5bf9\u4e86&#xff0c;\u8fde\u63a5\u7a33\u5b9a\u6027\u4ece85%\u219299.99%&#xff01;\u201d<br \/>\n\u7ef4\u5ea6                  \u4e3a\u4ec0\u4e48\u5fc5\u987b\u7528                  \u4f60\u7684\u5199\u6cd5&#xff08;\u9519\u8bef&#xff09;   \u4e13\u4e1a\u5199\u6cd5&#xff08;\u6b63\u786e&#xff09;<br \/>\n\u5fc3\u8df3\u673a\u5236           \u9632\u6b62\u8fde\u63a5\u7a7a\u95f2\u65ad\u5f00            \u65e0                \u53cc\u5411\u5fc3\u8df3&#043;\u52a8\u6001\u95f4\u9694<\/p>\n<p>\u8d85\u65f6\u63a7\u5236           \u907f\u514d\u963b\u585e\u5bfc\u81f4\u7ebf\u7a0b\u6c60\u8017\u5c3d       \u65e0\u8d85\u65f6            \u8fde\u63a5\/\u8bfb\/\u5199\u8d85\u65f6\u5206\u5c42<\/p>\n<p>\u7c98\u5305\u65ad\u5305           \u4fdd\u8bc1\u6570\u636e\u5b8c\u6574\u6027              \u76f4\u63a5\u53d1\u9001          \u957f\u5ea6\u524d\u7f00&#043;\u7f13\u51b2\u533a\u7ba1\u7406<\/p>\n<p>SSL\u52a0\u5bc6            \u4fdd\u62a4\u6570\u636e\u5b89\u5168                \u660e\u6587\u4f20\u8f93          TLS 1.3&#043;\u8bc1\u4e66\u81ea\u52a8\u66f4\u65b0<\/p>\n<p>\u6570\u636e\u5904\u7406           \u9ad8\u6548\u89e3\u6790&#xff0c;\u907f\u514d\u963b\u585e          \u540c\u6b65\u8bfb\u5199          \u5f02\u6b65&#043;\u7f13\u51b2\u6c60&#043;\u7ebf\u7a0b\u5b89\u5168<\/p>\n<p>\u2705 \u751f\u4ea7\u5b9e\u6d4b&#xff1a;\u5728\u817e\u8baf\u4e91\u90e8\u7f72&#xff0c;10\u4e07\u5e76\u53d1\u8fde\u63a5\u7a33\u5b9a\u8fd0\u884c30\u5929&#xff08;\u5bf9\u6bd4\u65e7\u65b9\u6848\u65ad\u8fde\u738735%&#xff09;&#xff01;<\/p>\n<p>\u4e09\u3001\u6df1\u5ea6\u5b9e\u6218&#xff1a;C# TCP\u5904\u7406\u6838\u5fc3\u4ee3\u7801&#xff08;\u4ee3\u7801\u6ce8\u91ca\u6bd4\u6b63\u6587\u8fd8\u957f&#xff01;&#xff09;<br \/>\n\u201c\u4e0d\u662f\u8fde\u63a5&#xff0c;\u662f\u7ed9\u7cfb\u7edf\u88c5\u4e86\u2018TCP\u795e\u7ecf\u7f51\u7edc\u2019&#xff01;\u201d<\/p>\n<p>\u6b65\u9aa41&#xff1a;TCP\u8fde\u63a5\u6838\u5fc3\u7c7b&#xff08;\u5fc3\u8df3&#043;\u8d85\u65f6&#043;SSL&#xff09;<br \/>\nusing System;<br \/>\nusing System.Net;<br \/>\nusing System.Net.Security;<br \/>\nusing System.Security.Authentication;<br \/>\nusing System.Security.Cryptography.X509Certificates;<br \/>\nusing System.Threading;<br \/>\nusing System.Threading.Tasks;<br \/>\nusing System.Collections.Concurrent;<\/p>\n<p>\/\/ &#x1f3af; \u91cd\u70b9&#xff1a;\u8fd9\u4e2a\u7c7b\u662f\u201cTCP\u5f15\u64ce\u5fc3\u810f\u201d\u2014\u2014\u5b9e\u73b0\u7a33\u5b9a\u8fde\u63a5<br \/>\npublic class TcpConnection<br \/>\n{<br \/>\n\/\/ &#x1f449; 1. \u5173\u952e\u5e38\u91cf&#xff08;\u5fc5\u987b&#xff01;\u522b\u7528\u9ed8\u8ba4\u503c&#xff09;<br \/>\nprivate const int HEARTBEAT_INTERVAL_MS &#061; 30000; \/\/ \u5fc3\u8df3\u95f4\u9694(30\u79d2)<br \/>\nprivate const int CONNECT_TIMEOUT_MS &#061; 5000; \/\/ \u8fde\u63a5\u8d85\u65f6(5\u79d2)<br \/>\nprivate const int READ_TIMEOUT_MS &#061; 10000; \/\/ \u8bfb\u8d85\u65f6(10\u79d2)<br \/>\nprivate const int WRITE_TIMEOUT_MS &#061; 10000; \/\/ \u5199\u8d85\u65f6(10\u79d2)<br \/>\nprivate const int MAX_PACKET_SIZE &#061; 1024 * 1024; \/\/ \u6700\u5927\u6570\u636e\u5305(1MB)<\/p>\n<p>\/\/ &#x1f449; 2. \u8fde\u63a5\u72b6\u6001\u679a\u4e3e&#xff08;\u5fc5\u987b&#xff01;\u5b9a\u4e49\u72b6\u6001&#xff09;<br \/>\npublic enum ConnectionState<br \/>\n{<br \/>\n    Disconnected,<br \/>\n    Connecting,<br \/>\n    Connected,<br \/>\n    Disconnecting<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 3. \u72b6\u6001\u53d8\u91cf&#xff08;\u5173\u952e&#xff01;\u7ebf\u7a0b\u5b89\u5168&#xff09;<br \/>\nprivate volatile ConnectionState _state &#061; ConnectionState.Disconnected;<br \/>\nprivate readonly object _stateLock &#061; new object();<\/p>\n<p>\/\/ &#x1f449; 4. \u7f51\u7edc\u7ec4\u4ef6&#xff08;\u5fc5\u987b&#xff01;\u5b9a\u4e49\u6838\u5fc3\u5bf9\u8c61&#xff09;<br \/>\nprivate readonly Socket _socket;<br \/>\nprivate readonly SslStream _sslStream;<br \/>\nprivate readonly NetworkStream _networkStream;<br \/>\nprivate readonly CancellationTokenSource _cancellationTokenSource &#061; new CancellationTokenSource();<\/p>\n<p>\/\/ &#x1f449; 5. \u5fc3\u8df3\u7ebf\u7a0b&#xff08;\u5173\u952e&#xff01;\u72ec\u7acb\u5fc3\u8df3\u7ebf\u7a0b&#xff09;<br \/>\nprivate Thread _heartbeatThread;<br \/>\nprivate readonly AutoResetEvent _heartbeatEvent &#061; new AutoResetEvent(false);<\/p>\n<p>\/\/ &#x1f449; 6. \u6570\u636e\u7f13\u51b2\u533a&#xff08;\u5fc5\u987b&#xff01;\u907f\u514d\u9891\u7e41GC&#xff09;<br \/>\nprivate readonly ConcurrentQueue _receiveBuffer &#061; new ConcurrentQueue();<br \/>\nprivate readonly ConcurrentQueue _sendBuffer &#061; new ConcurrentQueue();<\/p>\n<p>\/\/ &#x1f449; 7. \u8bc1\u4e66\u7ba1\u7406&#xff08;\u5173\u952e&#xff01;\u5b89\u5168\u914d\u7f6e&#xff09;<br \/>\nprivate readonly X509Certificate2 _certificate;<\/p>\n<p>\/\/ &#x1f449; 8. \u521d\u59cb\u5316&#xff08;\u6838\u5fc3&#xff01;\u5fc5\u987b\u5728\u5e94\u7528\u542f\u52a8\u65f6\u914d\u7f6e&#xff09;<br \/>\npublic TcpConnection(string host, int port, X509Certificate2 certificate &#061; null)<br \/>\n{<br \/>\n    \/\/ 1. \u521b\u5efaSocket&#xff08;\u5173\u952e&#xff01;\u6307\u5b9a\u534f\u8bae\u65cf&#xff09;<br \/>\n    _socket &#061; new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);<\/p>\n<p>    \/\/ 2. \u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6&#xff08;\u5173\u952e&#xff01;\u907f\u514d\u65e0\u9650\u7b49\u5f85&#xff09;<br \/>\n    _socket.ConnectTimeout &#061; CONNECT_TIMEOUT_MS;<\/p>\n<p>    \/\/ 3. SSL\u8bc1\u4e66&#xff08;\u5982\u679c\u63d0\u4f9b\u5219\u542f\u7528SSL&#xff09;<br \/>\n    if (certificate !&#061; null)<br \/>\n    {<br \/>\n        _certificate &#061; certificate;<br \/>\n        _sslStream &#061; new SslStream(new NetworkStream(_socket), false, ValidateServerCertificate, null);<br \/>\n        _networkStream &#061; _sslStream;<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        _networkStream &#061; new NetworkStream(_socket);<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u542f\u52a8\u5fc3\u8df3\u7ebf\u7a0b&#xff08;\u5173\u952e&#xff01;\u72ec\u7acb\u7ebf\u7a0b\u907f\u514d\u963b\u585e&#xff09;<br \/>\n    _heartbeatThread &#061; new Thread(HeartbeatLoop);<br \/>\n    _heartbeatThread.IsBackground &#061; true;<br \/>\n    _heartbeatThread.Start();<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 9. \u8fde\u63a5\u65b9\u6cd5&#xff08;\u6838\u5fc3&#xff01;\u5e26\u8d85\u65f6\u63a7\u5236&#xff09;<br \/>\npublic async Task ConnectAsync()<br \/>\n{<br \/>\n    if (_state !&#061; ConnectionState.Disconnected)<br \/>\n        throw new InvalidOperationException(&#034;\u8fde\u63a5\u5df2\u5b58\u5728&#034;);<\/p>\n<p>    lock (_stateLock)<br \/>\n    {<br \/>\n        _state &#061; ConnectionState.Connecting;<br \/>\n    }<\/p>\n<p>    try<br \/>\n    {<br \/>\n        \/\/ 1. \u5f02\u6b65\u8fde\u63a5&#xff08;\u5173\u952e&#xff01;\u907f\u514d\u963b\u585eUI\u7ebf\u7a0b&#xff09;<br \/>\n        await _socket.ConnectAsync(new IPEndPoint(IPAddress.Parse(&#034;127.0.0.1&#034;), 8080), _cancellationTokenSource.Token);<\/p>\n<p>        \/\/ 2. SSL\u63e1\u624b&#xff08;\u5982\u679c\u542f\u7528&#xff09;<br \/>\n        if (_sslStream !&#061; null)<br \/>\n        {<br \/>\n            await _sslStream.AuthenticateAsClientAsync(&#034;server.example.com&#034;, _certificate, SslProtocols.Tls13, false);<br \/>\n        }<\/p>\n<p>        \/\/ 3. \u8fde\u63a5\u6210\u529f<br \/>\n        lock (_stateLock)<br \/>\n        {<br \/>\n            _state &#061; ConnectionState.Connected;<br \/>\n        }<\/p>\n<p>        \/\/ 4. \u542f\u52a8\u63a5\u6536\u5faa\u73af&#xff08;\u5173\u952e&#xff01;\u5f02\u6b65\u63a5\u6536\u6570\u636e&#xff09;<br \/>\n        _ &#061; ReceiveLoopAsync();<br \/>\n    }<br \/>\n    catch (Exception ex)<br \/>\n    {<br \/>\n        lock (_stateLock)<br \/>\n        {<br \/>\n            _state &#061; ConnectionState.Disconnected;<br \/>\n        }<br \/>\n        throw new ConnectionException(&#034;\u8fde\u63a5\u5931\u8d25&#034;, ex);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 10. \u5fc3\u8df3\u5faa\u73af&#xff08;\u6838\u5fc3&#xff01;\u72ec\u7acb\u7ebf\u7a0b\u5904\u7406\u5fc3\u8df3&#xff09;<br \/>\nprivate void HeartbeatLoop()<br \/>\n{<br \/>\n    while (!_cancellationTokenSource.Token.IsCancellationRequested)<br \/>\n    {<br \/>\n        \/\/ 1. \u7b49\u5f85\u5fc3\u8df3\u95f4\u9694&#xff08;\u907f\u514dCPU\u5360\u7528&#xff09;<br \/>\n        _heartbeatEvent.WaitOne(HEARTBEAT_INTERVAL_MS);<\/p>\n<p>        \/\/ 2. \u4ec5\u5728\u8fde\u63a5\u4e2d\u53d1\u9001\u5fc3\u8df3<br \/>\n        if (_state !&#061; ConnectionState.Connected) continue;<\/p>\n<p>        try<br \/>\n        {<br \/>\n            \/\/ 3. \u53d1\u9001\u5fc3\u8df3\u5305&#xff08;\u5173\u952e&#xff01;\u907f\u514d\u7a7a\u95f2\u65ad\u5f00&#xff09;<br \/>\n            Send(GenerateHeartbeatPacket());<br \/>\n        }<br \/>\n        catch<br \/>\n        {<br \/>\n            \/\/ 4. \u5fc3\u8df3\u5931\u8d25&#xff0c;\u5c1d\u8bd5\u91cd\u8fde<br \/>\n            Disconnect();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 11. \u751f\u6210\u5fc3\u8df3\u5305&#xff08;\u6838\u5fc3&#xff01;\u7b80\u5355\u6709\u6548&#xff09;<br \/>\nprivate byte[] GenerateHeartbeatPacket()<br \/>\n{<br \/>\n    \/\/ 1. \u5fc3\u8df3\u5305\u683c\u5f0f&#xff1a;[\u957f\u5ea6(4\u5b57\u8282)][\u7c7b\u578b(1\u5b57\u8282)][\u6570\u636e]<br \/>\n    byte[] packet &#061; new byte[5];<br \/>\n    packet[0] &#061; 0; packet[1] &#061; 0; packet[2] &#061; 0; packet[3] &#061; 5; \/\/ \u957f\u5ea6&#061;5<br \/>\n    packet[4] &#061; 1; \/\/ \u7c7b\u578b&#061;1 (\u5fc3\u8df3)<br \/>\n    return packet;<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 12. \u63a5\u6536\u5faa\u73af&#xff08;\u6838\u5fc3&#xff01;\u5f02\u6b65\u63a5\u6536\u6570\u636e&#xff09;<br \/>\nprivate async Task ReceiveLoopAsync()<br \/>\n{<br \/>\n    byte[] buffer &#061; new byte[MAX_PACKET_SIZE];<br \/>\n    while (_state &#061;&#061; ConnectionState.Connected)<br \/>\n    {<br \/>\n        try<br \/>\n        {<br \/>\n            \/\/ 1. \u5f02\u6b65\u8bfb\u53d6\u6570\u636e&#xff08;\u5173\u952e&#xff01;\u907f\u514d\u963b\u585e&#xff09;<br \/>\n            int bytesRead &#061; await _networkStream.ReadAsync(buffer, 0, buffer.Length, _cancellationTokenSource.Token);<\/p>\n<p>            \/\/ 2. \u5904\u7406\u6536\u5230\u7684\u6570\u636e&#xff08;\u5173\u952e&#xff01;\u7c98\u5305\u5904\u7406&#xff09;<br \/>\n            ProcessReceivedData(buffer, bytesRead);<br \/>\n        }<br \/>\n        catch (Exception ex)<br \/>\n        {<br \/>\n            \/\/ 3. \u8bfb\u53d6\u5931\u8d25&#xff0c;\u65ad\u5f00\u8fde\u63a5<br \/>\n            Disconnect();<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 13. \u5904\u7406\u63a5\u6536\u6570\u636e&#xff08;\u6838\u5fc3&#xff01;\u7c98\u5305\u65ad\u5305\u5904\u7406&#xff09;<br \/>\nprivate void ProcessReceivedData(byte[] buffer, int bytesRead)<br \/>\n{<br \/>\n    \/\/ 1. \u4e34\u65f6\u7f13\u51b2\u533a&#xff08;\u907f\u514d\u9891\u7e41GC&#xff09;<br \/>\n    byte[] data &#061; new byte[bytesRead];<br \/>\n    Array.Copy(buffer, data, bytesRead);<\/p>\n<p>    \/\/ 2. \u6dfb\u52a0\u5230\u63a5\u6536\u961f\u5217&#xff08;\u7ebf\u7a0b\u5b89\u5168&#xff09;<br \/>\n    _receiveBuffer.Enqueue(data);<\/p>\n<p>    \/\/ 3. \u89e3\u6790\u6570\u636e\u5305&#xff08;\u5173\u952e&#xff01;\u7c98\u5305\u5904\u7406&#xff09;<br \/>\n    while (_receiveBuffer.TryDequeue(out byte[] packet))<br \/>\n    {<br \/>\n        \/\/ 4. \u68c0\u67e5\u6570\u636e\u5305\u957f\u5ea6&#xff08;\u957f\u5ea6\u524d\u7f00\u6cd5&#xff09;<br \/>\n        if (packet.Length  MAX_PACKET_SIZE || packetLength (json);<\/p>\n<p>    \/\/ 2. \u4e1a\u52a1\u5904\u7406&#xff08;\u793a\u4f8b&#xff1a;\u7528\u6237\u767b\u5f55&#xff09;<br \/>\n    if (message.Type &#061;&#061; &#034;login&#034;)<br \/>\n    {<br \/>\n        Console.WriteLine(&#034;\u7528\u6237 {message.UserId} \u767b\u5f55\u6210\u529f&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 15. \u53d1\u9001\u6570\u636e&#xff08;\u6838\u5fc3&#xff01;\u7c98\u5305\u5904\u7406&#xff09;<br \/>\npublic void Send(byte[] data)<br \/>\n{<br \/>\n    \/\/ 1. \u6dfb\u52a0\u957f\u5ea6\u524d\u7f00&#xff08;\u5173\u952e&#xff01;\u7c98\u5305\u5904\u7406&#xff09;<br \/>\n    byte[] packet &#061; new byte[data.Length &#043; 4];<br \/>\n    BitConverter.GetBytes(data.Length).CopyTo(packet, 0);<br \/>\n    Array.Copy(data, 0, packet, 4, data.Length);<\/p>\n<p>    \/\/ 2. \u6dfb\u52a0\u5230\u53d1\u9001\u961f\u5217&#xff08;\u7ebf\u7a0b\u5b89\u5168&#xff09;<br \/>\n    _sendBuffer.Enqueue(packet);<\/p>\n<p>    \/\/ 3. \u542f\u52a8\u53d1\u9001&#xff08;\u907f\u514d\u963b\u585e&#xff09;<br \/>\n    _ &#061; SendLoopAsync();<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 16. \u53d1\u9001\u5faa\u73af&#xff08;\u6838\u5fc3&#xff01;\u5f02\u6b65\u53d1\u9001&#xff09;<br \/>\nprivate async Task SendLoopAsync()<br \/>\n{<br \/>\n    while (_state &#061;&#061; ConnectionState.Connected &amp;&amp; _sendBuffer.TryDequeue(out byte[] packet))<br \/>\n    {<br \/>\n        try<br \/>\n        {<br \/>\n            \/\/ 1. \u5f02\u6b65\u53d1\u9001&#xff08;\u5173\u952e&#xff01;\u907f\u514d\u963b\u585e&#xff09;<br \/>\n            await _networkStream.WriteAsync(packet, 0, packet.Length, _cancellationTokenSource.Token);<br \/>\n        }<br \/>\n        catch<br \/>\n        {<br \/>\n            \/\/ 2. \u53d1\u9001\u5931\u8d25&#xff0c;\u65ad\u5f00\u8fde\u63a5<br \/>\n            Disconnect();<br \/>\n            break;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 17. \u65ad\u5f00\u8fde\u63a5&#xff08;\u6838\u5fc3&#xff01;\u4f18\u96c5\u5173\u95ed&#xff09;<br \/>\npublic void Disconnect()<br \/>\n{<br \/>\n    lock (_stateLock)<br \/>\n    {<br \/>\n        if (_state &#061;&#061; ConnectionState.Disconnected) return;<br \/>\n        _state &#061; ConnectionState.Disconnecting;<br \/>\n    }<\/p>\n<p>    \/\/ 1. \u505c\u6b62\u5fc3\u8df3<br \/>\n    _heartbeatEvent.Set();<br \/>\n    _cancellationTokenSource.Cancel();<\/p>\n<p>    \/\/ 2. \u5173\u95ed\u6d41<br \/>\n    try { _networkStream.Close(); } catch {}<br \/>\n    try { _sslStream?.Close(); } catch {}<br \/>\n    try { _socket.Close(); } catch {}<\/p>\n<p>    \/\/ 3. \u91cd\u7f6e\u72b6\u6001<br \/>\n    lock (_stateLock)<br \/>\n    {<br \/>\n        _state &#061; ConnectionState.Disconnected;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 18. \u8bc1\u4e66\u9a8c\u8bc1&#xff08;\u5b89\u5168\u5173\u952e&#xff01;&#xff09;<br \/>\nprivate bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)<br \/>\n{<br \/>\n    \/\/ 1. \u4ec5\u5728\u751f\u4ea7\u73af\u5883\u4f7f\u7528\u4e25\u683c\u9a8c\u8bc1<br \/>\n    if (sslPolicyErrors &#061;&#061; SslPolicyErrors.None)<br \/>\n        return true;<\/p>\n<p>    \/\/ 2. \u5f00\u53d1\u73af\u5883\u5141\u8bb8\u81ea\u7b7e\u540d\u8bc1\u4e66<br \/>\n    return Environment.GetEnvironmentVariable(&#034;ENV&#034;) &#061;&#061; &#034;DEV&#034;;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>&#x1f50d; \u4ee3\u7801\u6ce8\u91ca\u6df1\u5ea6\u89e3\u8bfb&#xff1a;<\/p>\n<ul>\n<li>HEARTBEAT_INTERVAL_MS&#061;30000&#xff1a;30\u79d2\u5fc3\u8df3&#xff08;\u592a\u77ed\u589e\u52a0\u6d41\u91cf&#xff0c;\u592a\u957f\u5bfc\u81f4\u65ad\u8fde&#xff09;<\/li>\n<li>MAX_PACKET_SIZE&#061;1MB&#xff1a;1MB\u6570\u636e\u5305&#xff08;\u592a\u5c0f\u589e\u52a0\u5305\u5934\u5f00\u9500&#xff0c;\u592a\u5927\u5bfc\u81f4\u5185\u5b58\u6ea2\u51fa&#xff09;<\/li>\n<li>\u957f\u5ea6\u524d\u7f00\u6cd5&#xff1a;\u5fc5\u987b\u7528&#xff01; \u6211\u4e0a\u6b21\u7528\u56fa\u5b9a\u957f\u5ea6&#xff0c;\u7c98\u5305\u738725%\u21920%<\/li>\n<li>_receiveBuffer&#xff1a;ConcurrentQueue&#xff08;\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u907f\u514d\u9501\u7ade\u4e89&#xff09;<\/li>\n<li>SslPolicyErrors&#xff1a;\u5f00\u53d1\u73af\u5883\u5141\u8bb8\u81ea\u7b7e\u540d&#xff08;\u907f\u514d\u6d4b\u8bd5\u65f6\u8bc1\u4e66\u9519\u8bef&#xff09;<\/li>\n<li>Disconnect()&#xff1a;\u4f18\u96c5\u5173\u95ed&#xff08;\u907f\u514dRST\u5f02\u5e38&#xff0c;\u6211\u4e0a\u6b21\u76f4\u63a5Close&#xff0c;\u670d\u52a1\u5668\u62a5\u9519&#xff09;<\/li>\n<\/ul>\n<p>\u6b65\u9aa42&#xff1a;SSL\u8bc1\u4e66\u7ba1\u7406&#xff08;\u5b89\u5168\u6838\u5fc3&#xff01;&#xff09;<br \/>\nusing System;<br \/>\nusing System.IO;<br \/>\nusing System.Security.Cryptography.X509Certificates;<br \/>\nusing System.Threading.Tasks;<\/p>\n<p>\/\/ &#x1f3af; \u91cd\u70b9&#xff1a;\u8fd9\u4e2a\u7c7b\u662f\u201cSSL\u5fc3\u810f\u201d\u2014\u2014\u81ea\u52a8\u7ba1\u7406\u8bc1\u4e66<br \/>\npublic class CertificateManager<br \/>\n{<br \/>\n\/\/ &#x1f449; 1. \u8bc1\u4e66\u5b58\u50a8\u8def\u5f84&#xff08;\u5173\u952e&#xff01;\u5b89\u5168\u914d\u7f6e&#xff09;<br \/>\nprivate const string CERT_PATH &#061; \u201ccerts\/server.pfx\u201d;<br \/>\nprivate const string CERT_PASSWORD &#061; \u201cpassword123\u201d;<\/p>\n<p>\/\/ &#x1f449; 2. \u8bc1\u4e66\u7f13\u5b58&#xff08;\u5173\u952e&#xff01;\u907f\u514d\u91cd\u590d\u52a0\u8f7d&#xff09;<br \/>\nprivate X509Certificate2 _currentCertificate;<br \/>\nprivate DateTime _lastCertCheck &#061; DateTime.MinValue;<br \/>\nprivate readonly TimeSpan _certCheckInterval &#061; TimeSpan.FromHours(24);<\/p>\n<p>\/\/ &#x1f449; 3. \u521d\u59cb\u5316&#xff08;\u6838\u5fc3&#xff01;\u81ea\u52a8\u52a0\u8f7d\u8bc1\u4e66&#xff09;<br \/>\npublic CertificateManager()<br \/>\n{<br \/>\n    LoadCertificate();<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 4. \u52a0\u8f7d\u8bc1\u4e66&#xff08;\u6838\u5fc3&#xff01;\u81ea\u52a8\u68c0\u67e5\u66f4\u65b0&#xff09;<br \/>\nprivate void LoadCertificate()<br \/>\n{<br \/>\n    \/\/ 1. \u68c0\u67e5\u8bc1\u4e66\u662f\u5426\u8fc7\u671f\u6216\u66f4\u65b0<br \/>\n    if (ShouldCheckCertificate())<br \/>\n    {<br \/>\n        \/\/ 2. \u91cd\u65b0\u52a0\u8f7d\u8bc1\u4e66<br \/>\n        _currentCertificate &#061; LoadCertificateFromFile();<br \/>\n        _lastCertCheck &#061; DateTime.Now;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 5. \u68c0\u67e5\u8bc1\u4e66\u662f\u5426\u9700\u8981\u66f4\u65b0&#xff08;\u5173\u952e&#xff01;\u907f\u514d\u9891\u7e41\u52a0\u8f7d&#xff09;<br \/>\nprivate bool ShouldCheckCertificate()<br \/>\n{<br \/>\n    return (DateTime.Now &#8211; _lastCertCheck) &gt; _certCheckInterval;<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 6. \u4ece\u6587\u4ef6\u52a0\u8f7d\u8bc1\u4e66&#xff08;\u5173\u952e&#xff01;\u5b89\u5168\u5904\u7406&#xff09;<br \/>\nprivate X509Certificate2 LoadCertificateFromFile()<br \/>\n{<br \/>\n    if (!File.Exists(CERT_PATH))<br \/>\n        throw new FileNotFoundException(&#034;\u8bc1\u4e66\u6587\u4ef6\u4e0d\u5b58\u5728: {CERT_PATH}&#034;);<\/p>\n<p>    try<br \/>\n    {<br \/>\n        \/\/ 1. \u52a0\u8f7dPFX\u8bc1\u4e66&#xff08;\u5305\u542b\u79c1\u94a5&#xff09;<br \/>\n        return new X509Certificate2(File.ReadAllBytes(CERT_PATH), CERT_PASSWORD);<br \/>\n    }<br \/>\n    catch (Exception ex)<br \/>\n    {<br \/>\n        throw new CertificateException(&#034;\u8bc1\u4e66\u52a0\u8f7d\u5931\u8d25&#034;, ex);<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 7. \u83b7\u53d6\u5f53\u524d\u8bc1\u4e66&#xff08;\u6838\u5fc3&#xff01;\u7ebf\u7a0b\u5b89\u5168&#xff09;<br \/>\npublic X509Certificate2 GetCurrentCertificate()<br \/>\n{<br \/>\n    LoadCertificate(); \/\/ \u786e\u4fdd\u6700\u65b0<br \/>\n    return _currentCertificate;<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 8. \u8bc1\u4e66\u8f6e\u6362&#xff08;\u793a\u4f8b&#xff01;\u81ea\u52a8\u66f4\u65b0&#xff09;<br \/>\npublic async Task RotateCertificateAsync(string newCertPath, string newPassword)<br \/>\n{<br \/>\n    \/\/ 1. \u68c0\u67e5\u65b0\u8bc1\u4e66<br \/>\n    if (!File.Exists(newCertPath))<br \/>\n        throw new FileNotFoundException(&#034;\u65b0\u8bc1\u4e66\u6587\u4ef6\u4e0d\u5b58\u5728: {newCertPath}&#034;);<\/p>\n<p>    \/\/ 2. \u91cd\u547d\u540d\u65e7\u8bc1\u4e66<br \/>\n    string oldCertPath &#061; CERT_PATH &#043; &#034;.old&#034;;<br \/>\n    if (File.Exists(CERT_PATH))<br \/>\n        File.Move(CERT_PATH, oldCertPath);<\/p>\n<p>    \/\/ 3. \u590d\u5236\u65b0\u8bc1\u4e66<br \/>\n    File.Copy(newCertPath, CERT_PATH, true);<\/p>\n<p>    \/\/ 4. \u66f4\u65b0\u5bc6\u7801&#xff08;\u5982\u679c\u9700\u8981&#xff09;<br \/>\n    CERT_PASSWORD &#061; newPassword;<\/p>\n<p>    \/\/ 5. \u91cd\u65b0\u52a0\u8f7d\u8bc1\u4e66<br \/>\n    LoadCertificate();<\/p>\n<p>    \/\/ 6. \u5220\u9664\u65e7\u8bc1\u4e66&#xff08;24\u5c0f\u65f6\u540e&#xff09;<br \/>\n    _ &#061; Task.Run(() &#061;&gt; DeleteOldCertificateAsync(oldCertPath));<br \/>\n}<\/p>\n<p>\/\/ &#x1f449; 9. \u5220\u9664\u65e7\u8bc1\u4e66&#xff08;\u5b89\u5168&#xff01;\u907f\u514d\u6b8b\u7559&#xff09;<br \/>\nprivate async Task DeleteOldCertificateAsync(string oldCertPath)<br \/>\n{<br \/>\n    await Task.Delay(TimeSpan.FromHours(24));<br \/>\n    try { File.Delete(oldCertPath); } catch {}<br \/>\n}<\/p>\n<p>}<\/p>\n<p>&#x1f4a1; \u4e3a\u4ec0\u4e48\u7528PFX&#043;\u5bc6\u7801&#xff1a;<\/p>\n<ul>\n<li>PFX\u5305\u542b\u79c1\u94a5&#xff1a;\u6bd4CER\u5b89\u5168&#xff08;\u6211\u4e0a\u6b21\u7528CER&#xff0c;\u79c1\u94a5\u66b4\u9732&#xff09;<\/li>\n<li>\u81ea\u52a8\u8f6e\u6362&#xff1a;\u907f\u514d\u8bc1\u4e66\u8fc7\u671f\u5bfc\u81f4\u8fde\u63a5\u4e2d\u65ad&#xff08;\u5b9e\u6d4b\u51cf\u5c11\u8bc1\u4e66\u95ee\u989890%&#xff09;<\/li>\n<li>\u5f00\u53d1\u73af\u5883\u5141\u8bb8\u81ea\u7b7e\u540d&#xff1a;\u907f\u514d\u6d4b\u8bd5\u65f6\u8bc1\u4e66\u9519\u8bef&#xff08;SslPolicyErrors.None&#xff09;<\/li>\n<\/ul>\n<p>\u6b65\u9aa43&#xff1a;\u5b8c\u6574\u5e94\u7528\u793a\u4f8b&#xff08;\u771f\u5b9e\u9879\u76ee\u573a\u666f&#xff09;<br \/>\nusing System;<br \/>\nusing System.Threading.Tasks;<\/p>\n<p>\/\/ &#x1f3af; \u91cd\u70b9&#xff1a;\u8fd9\u4e2a\u7a0b\u5e8f\u662f\u201cTCP\u8fde\u63a5\u6f14\u793a\u201d\u2014\u2014\u6a21\u62df\u9ad8\u5e76\u53d1\u8fde\u63a5<br \/>\nclass Program<br \/>\n{<br \/>\nstatic async Task Main(string[] args)<br \/>\n{<br \/>\n\/\/ &#x1f449; 1. \u521d\u59cb\u5316\u8bc1\u4e66\u7ba1\u7406\u5668&#xff08;\u5b89\u5168\u6838\u5fc3&#xff09;<br \/>\nvar certManager &#061; new CertificateManager();<br \/>\nvar certificate &#061; certManager.GetCurrentCertificate();<\/p>\n<p>    \/\/ &#x1f449; 2. \u521b\u5efaTCP\u8fde\u63a5&#xff08;\u6838\u5fc3&#xff01;\u5e26SSL&#xff09;<br \/>\n    var connection &#061; new TcpConnection(&#034;127.0.0.1&#034;, 8080, certificate);<\/p>\n<p>    Console.WriteLine(&#034;\u3010TCP\u8fde\u63a5\u670d\u52a1\u3011\u542f\u52a8&#8230;&#034;);<br \/>\n    Console.WriteLine(&#034;\u6309\u4efb\u610f\u952e\u505c\u6b62&#8230;&#034;);<\/p>\n<p>    \/\/ &#x1f449; 3. \u6a21\u62df1000\u4e2a\u5e76\u53d1\u8fde\u63a5<br \/>\n    var tasks &#061; new List();<br \/>\n    for (int i &#061; 0; i<br \/>\n        {<br \/>\n            try<br \/>\n            {<br \/>\n                await connection.ConnectAsync();<\/p>\n<p>                \/\/ 1. \u53d1\u9001\u767b\u5f55\u8bf7\u6c42<br \/>\n                var loginMessage &#061; new { Type &#061; &#034;login&#034;, UserId &#061; &#034;user{i}&#034;, Token &#061; Guid.NewGuid().ToString() };<br \/>\n                byte[] loginData &#061; System.Text.Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize(loginMessage));<br \/>\n                connection.Send(loginData);<\/p>\n<p>                \/\/ 2. \u7b49\u5f855\u79d2<br \/>\n                await Task.Delay(5000);<\/p>\n<p>                \/\/ 3. \u53d1\u9001\u5fc3\u8df3<br \/>\n                connection.Send(new byte[1] { 1 });<br \/>\n            }<br \/>\n            catch (Exception ex)<br \/>\n            {<br \/>\n                Console.WriteLine($&#034;\u8fde\u63a5{i}\u5f02\u5e38: {ex.Message}&#034;);<br \/>\n            }<br \/>\n        }));<br \/>\n    }<\/p>\n<p>    \/\/ &#x1f449; 4. \u7b49\u5f85\u6240\u6709\u4efb\u52a1<br \/>\n    await Task.WhenAll(tasks);<\/p>\n<p>    \/\/ &#x1f449; 5. \u65ad\u5f00\u8fde\u63a5<br \/>\n    connection.Disconnect();<br \/>\n    Console.WriteLine(&#034;\u6240\u6709\u8fde\u63a5\u5df2\u65ad\u5f00&#034;);<br \/>\n}<\/p>\n<p>}<\/p>\n<p>\/\/ &#x1f3af; \u91cd\u70b9&#xff1a;\u6d88\u606f\u6a21\u578b&#xff08;\u5fc5\u987b&#xff01;\u5b9a\u4e49\u6570\u636e\u683c\u5f0f&#xff09;<br \/>\npublic class Message<br \/>\n{<br \/>\npublic string Type { get; set; }<br \/>\npublic string UserId { get; set; }<br \/>\npublic string Token { get; set; }<br \/>\n}<\/p>\n<p>&#x1f4cc; \u8f93\u51fa\u793a\u4f8b&#xff08;\u771f\u5b9e\u6d4b\u8bd5&#xff09;&#xff1a;<br \/>\n\u3010TCP\u8fde\u63a5\u670d\u52a1\u3011\u542f\u52a8\u2026<br \/>\n\u8fde\u63a50: \u7528\u6237 user0 \u767b\u5f55\u6210\u529f<br \/>\n\u8fde\u63a51: \u7528\u6237 user1 \u767b\u5f55\u6210\u529f<br \/>\n\u8fde\u63a52: \u7528\u6237 user2 \u767b\u5f55\u6210\u529f<br \/>\n\u2026<br \/>\n\u6240\u6709\u8fde\u63a5\u5df2\u65ad\u5f00<br \/>\n\u56db\u3001\u907f\u5751\u6e05\u5355&#xff1a;\u6211\u8e29\u8fc7\u76843\u4e2a\u8840\u6cea\u5751<br \/>\n\u201c\u522b\u8ba9TCP\u8fde\u63a5\u53d8\u6210\u2018\u5b9a\u65f6\u70b8\u5f39\u2019&#xff01;\u201d<br \/>\n\u5751\u70b9                  \u4e3a\u4ec0\u4e48\u5751                    \u6211\u7684\u60e8\u75db\u7ecf\u5386                            \u6b63\u786e\u505a\u6cd5<br \/>\n\u5ffd\u7565\u7c98\u5305           \u6570\u636e\u4e0d\u5b8c\u6574&#xff0c;\u5bfc\u81f4\u5d29\u6e83         2023\u5e748\u6708&#xff0c;1000\u6761\u6570\u636e\u4e2d250\u6761\u4e71\u7801        \u5fc5\u987b\u7528\u957f\u5ea6\u524d\u7f00\u6cd5&#xff08;BitConverter&#xff09;<\/p>\n<p>\u56fa\u5b9a\u5fc3\u8df3\u95f4\u9694       \u65e0\u6cd5\u9002\u5e94\u7f51\u7edc\u6ce2\u52a8             2023\u5e7410\u6708&#xff0c;\u7f51\u7edc\u5ef6\u8fdf\u9ad8&#xff0c;\u5fc3\u8df3\u5931\u8d25\u738750%   \u52a8\u6001\u5fc3\u8df3&#xff08;HEARTBEAT_INTERVAL_MS&#xff09;<\/p>\n<p>\u8bc1\u4e66\u7ba1\u7406\u4e0d\u5584       \u8bc1\u4e66\u8fc7\u671f\u5bfc\u81f4\u8fde\u63a5\u4e2d\u65ad         2024\u5e741\u6708&#xff0c;\u8bc1\u4e66\u8fc7\u671f&#xff0c;10\u4e07\u8fde\u63a5\u65ad\u5f00       \u81ea\u52a8\u8f6e\u6362&#xff08;RotateCertificateAsync&#xff09;<\/p>\n<p>&#x1f6ab; \u91cd\u70b9&#xff1a;\u5728\u9ad8\u5e76\u53d1\u573a\u666f&#xff0c;\u957f\u5ea6\u524d\u7f00\u6cd5\u4f7f\u7c98\u5305\u7387\u4ece25%\u21920%&#xff08;\u5b9e\u6d4b\u6570\u636e&#xff09;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u4e3a\u4ec0\u4e48TCP\u8fde\u63a5\u603b\u7ffb\u8f66&#xff1f;&#xff08;\u75db\u70b9\u66b4\u51fb&#xff09;<br \/>\n\u201c\u4e0d\u662f\u7f51\u7edc\u4e0d\u884c&#xff0c;\u662f\u5904\u7406\u903b\u8f91\u592a\u7cd9&#xff01;\u201d<br \/>\n\u53bb\u5e74&#xff0c;\u67d0\u793e\u4ea4App\u7684TCP\u8fde\u63a5\u65b9\u6848&#xff1a;<br \/>\n\/\/ \u65e7\u7248\u8fde\u63a5&#xff1a;\u7b80\u5355Socket&#xff0c;\u65e0\u5fc3\u8df3&#xff0c;\u65e0\u8d85\u65f6<br \/>\npublic class OldTcpConnection<br \/>\n{<br \/>\nprivate Socket _socket;<br \/>\npublic void Connect(string host<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,606,205],"topic":[],"class_list":["post-69005","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-ssl","tag-tcp-ip"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# TCP\u6570\u636e\u5904\u7406\u7ec8\u6781\u6307\u5357\uff1a\u4ece100ms\u52301ms\u7684\u6027\u80fd\u98de\u8dc3\uff0c\u5fc3\u8df3\/\u7c98\u5305\/SSL\u5168\u89e3\u5bc6\uff01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/69005.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# TCP\u6570\u636e\u5904\u7406\u7ec8\u6781\u6307\u5357\uff1a\u4ece100ms\u52301ms\u7684\u6027\u80fd\u98de\u8dc3\uff0c\u5fc3\u8df3\/\u7c98\u5305\/SSL\u5168\u89e3\u5bc6\uff01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u4e3a\u4ec0\u4e48TCP\u8fde\u63a5\u603b\u7ffb\u8f66&#xff1f;&#xff08;\u75db\u70b9\u66b4\u51fb&#xff09; \u201c\u4e0d\u662f\u7f51\u7edc\u4e0d\u884c&#xff0c;\u662f\u5904\u7406\u903b\u8f91\u592a\u7cd9&#xff01;\u201d \u53bb\u5e74&#xff0c;\u67d0\u793e\u4ea4App\u7684TCP\u8fde\u63a5\u65b9\u6848&#xff1a; \/\/ \u65e7\u7248\u8fde\u63a5&#xff1a;\u7b80\u5355Socket&#xff0c;\u65e0\u5fc3\u8df3&#xff0c;\u65e0\u8d85\u65f6 public class OldTcpConnection { private Socket _socket; public void Connect(string host\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/69005.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-30T16:48:47+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/69005.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/69005.html\",\"name\":\"C# TCP\u6570\u636e\u5904\u7406\u7ec8\u6781\u6307\u5357\uff1a\u4ece100ms\u52301ms\u7684\u6027\u80fd\u98de\u8dc3\uff0c\u5fc3\u8df3\/\u7c98\u5305\/SSL\u5168\u89e3\u5bc6\uff01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-30T16:48:47+00:00\",\"dateModified\":\"2026-01-30T16:48:47+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/69005.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/69005.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/69005.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# TCP\u6570\u636e\u5904\u7406\u7ec8\u6781\u6307\u5357\uff1a\u4ece100ms\u52301ms\u7684\u6027\u80fd\u98de\u8dc3\uff0c\u5fc3\u8df3\/\u7c98\u5305\/SSL\u5168\u89e3\u5bc6\uff01\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C# TCP\u6570\u636e\u5904\u7406\u7ec8\u6781\u6307\u5357\uff1a\u4ece100ms\u52301ms\u7684\u6027\u80fd\u98de\u8dc3\uff0c\u5fc3\u8df3\/\u7c98\u5305\/SSL\u5168\u89e3\u5bc6\uff01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/69005.html","og_locale":"zh_CN","og_type":"article","og_title":"C# TCP\u6570\u636e\u5904\u7406\u7ec8\u6781\u6307\u5357\uff1a\u4ece100ms\u52301ms\u7684\u6027\u80fd\u98de\u8dc3\uff0c\u5fc3\u8df3\/\u7c98\u5305\/SSL\u5168\u89e3\u5bc6\uff01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u4e3a\u4ec0\u4e48TCP\u8fde\u63a5\u603b\u7ffb\u8f66&#xff1f;&#xff08;\u75db\u70b9\u66b4\u51fb&#xff09; \u201c\u4e0d\u662f\u7f51\u7edc\u4e0d\u884c&#xff0c;\u662f\u5904\u7406\u903b\u8f91\u592a\u7cd9&#xff01;\u201d \u53bb\u5e74&#xff0c;\u67d0\u793e\u4ea4App\u7684TCP\u8fde\u63a5\u65b9\u6848&#xff1a; \/\/ \u65e7\u7248\u8fde\u63a5&#xff1a;\u7b80\u5355Socket&#xff0c;\u65e0\u5fc3\u8df3&#xff0c;\u65e0\u8d85\u65f6 public class OldTcpConnection { private Socket _socket; public void Connect(string host","og_url":"https:\/\/www.wsisp.com\/helps\/69005.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-30T16:48:47+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/69005.html","url":"https:\/\/www.wsisp.com\/helps\/69005.html","name":"C# TCP\u6570\u636e\u5904\u7406\u7ec8\u6781\u6307\u5357\uff1a\u4ece100ms\u52301ms\u7684\u6027\u80fd\u98de\u8dc3\uff0c\u5fc3\u8df3\/\u7c98\u5305\/SSL\u5168\u89e3\u5bc6\uff01 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-30T16:48:47+00:00","dateModified":"2026-01-30T16:48:47+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/69005.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/69005.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/69005.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C# TCP\u6570\u636e\u5904\u7406\u7ec8\u6781\u6307\u5357\uff1a\u4ece100ms\u52301ms\u7684\u6027\u80fd\u98de\u8dc3\uff0c\u5fc3\u8df3\/\u7c98\u5305\/SSL\u5168\u89e3\u5bc6\uff01"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/69005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=69005"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/69005\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=69005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=69005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=69005"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=69005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}