Table of Contents
1. Network Basics
2. Socket Object
3. Bind() Binding and Connect() Connection
4. Listen() Listening for Connection Requests and Accept() Receiving Connection Requests
5. Receive() and Send()
6. Releasing Resources
7. IPAddress and IPEndPoint
Written for the first time on the evening of February 14, 2019, and will be continuously modified and updated.
This article is structured around the creation, connection, data transmission, and resource release processes of Socket. It provides detailed information about methods and parameters.
1. Network Basics
When it comes to Socket, it's necessary to learn some knowledge about TCP/IP and understand the OSI network model.
Recommended articles can help quickly understand these concepts.
https://www.cnblogs.com/ysyn/p/3399351.html
https://www.cnblogs.com/wangcq/p/3520400.html
2. Socket Object
Whether it's a server or a client, a SOCKET object must be created, and the creation method is the same.
Below is a common example of creating a Socket object.
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Monitor IPv4 addresses, socket type is TCP, protocol type is TCP
It has three constructors.
public Socket(SocketInformation socketInformation);
public Socket(SocketType socketType, ProtocolType protocolType);
public Socket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType);
The first constructor, SocketInformation, saves the information of
Socket(SocketType, ProtocolType)
Essentially, it is the same as the second constructor. It’s as if you can directly put (an apple, a pear) into a basket, or you can wrap the fruits first and then put them into the basket.
Below will explain the meaning of all parameters.
SocketType
Specifies the socket type represented by an instance of the Socket class.
TCP uses the host's IP address combined with the port number on the host as the endpoint of the TCP connection; this endpoint is known as a socket. A socket is represented as (IP address: port number).
SocketType is an enum type with the following fields.
SocketType |
Value |
Corresponding ProtocolType |
Description |
Unknown |
-1 |
Unknown |
Specifies an unknown Socket type |
| 类型 | 编号 | 协议 | 描述 |
|--------------|------|-------|------------------------------------------------------------|
| Stream(使用字节流) | 1 | Tcp | 支持可靠、双向、基于连接的字节流 |
| Dgram(使用数据报) | 2 | Udp | 面向无连接 |
| Raw | 3 | Icmp、lgmp | 支持对基础传输协议的访问 |
| Rdm | 4 | | 支持无连接、面向消息、以可靠方式发送的消息,并保留数据中的消息边界 |
<td style="box-sizing: border-box; outline: 0px; padding: 8px; margin: 0px; word-wrap: break-word; border-color: #dddddd; font-size: 14px; color: #4f4f4f; line-height: 22px; text-align: center;" valign="top">
<p style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; line-height: 22px; overflow-x: auto; word-wrap: break-word;" align="left"><span style="color: #000000; font-family: 'Segoe UI', SegoeUI, 'Segoe WP', 'Helvetica Neue', Helvetica, Tahoma, Arial, sans-serif;">Seqpacket</span></p>
</td>
<td style="box-sizing: border-box; outline: 0px; padding: 8px; margin: 0px; word-wrap: break-word; border-color: #dddddd; font-size: 14px; color: #4f4f4f; line-height: 22px; text-align: center;" valign="top">
<p style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 0px; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif; line-height: 22px; overflow-x: auto; word-wrap: break-word;" align="left">5</p>
</td>
<td style="box-sizing: border-box; outline: 0px; padding: 8px; margin: 0px; word-wrap: break-word; border-color: #dddddd; font-size: 14px; color: #4f4f4f; line-height: 22px; text-align: center;" valign="top"> </td>
<td style="box-sizing: border-box; outline: 0px; padding: 8px; margin: 0px; word-wrap: break-word; border-color: #dddddd; line-height: 22px; text-align: center;" valign="top">
<p style="box-sizing: border-box; outline: 0px; padding: 0px; margin: 0px; line-height: 22px; overflow-x: auto; word-wrap: break-word;" align="left"><span style="color: #4f4f4f; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif;"><span style="color: #4f4f4f; font-family: 'Microsoft YaHei', 'SF Pro Display', Roboto, Noto, Arial, 'PingFang SC', sans-serif;">Provides connection-oriented and reliable bi-directional transmission of sequenced byte streams over the network</span></span></p>
</td>
</tr>
</tbody>
</table>
<p>For more detailed information, please refer to Microsoft's documentation</p>
<p>URL: <a title="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.sockettype?view=netframework-4.7.2" href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.sockettype?view=netframework-4.7.2" target="_blank" rel="noopener noreferrer">https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.sockettype?view=netframework-4.7.2</a></p>
<h3>ProtocolType</h3>
<p>Indicates the protocol type, and it is an <strong>enum</strong> type.</p>
<p>All fields are as follows</p>
<table style="box-sizing: inherit; border-spacing: 0px; table-layout: fixed; width: 821px; font-size: 0.875rem; border-width: 0px; color: #000000; font-family: 'Segoe UI', SegoeUI, 'Segoe WP', 'Helvetica Neue', Helvetica, Tahoma, Arial, sans-serif; height: 1112px;">
<tbody style="box-sizing: inherit;">
<tr style="box-sizing: inherit;" data-moniker=" netcore-1.0 netcore-1.1 netcore-2.0 netcore-2.1 netcore-2.2 netframework-1.1 netframework-2.0 netframework-3.0 netframework-3.5 netframework-4.0 netframework-4.5 netframework-4.5.1 netframework-4.5.2 netframework-4.6 netframework-4.6.1 netframework-4.6.2 netframework-4.7 netframework-4.7.1 netframework-4.7.2 netframework-4.8 netstandard-1.3 netstandard-1.4 netstandard-1.6 netstandard-2.0 xamarinandroid-7.1 xamarinios-10.8 xamarinmac-3.0 ">
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Protocol Type</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">Value</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="aade6-104">Description</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;" data-moniker=" netcore-1.0 netcore-1.1 netcore-2.0 netcore-2.1 netcore-2.2 netframework-1.1 netframework-2.0 netframework-3.0 netframework-3.5 netframework-4.0 netframework-4.5 netframework-4.5.1 netframework-4.5.2 netframework-4.6 netframework-4.6.1 netframework-4.6.2 netframework-4.7 netframework-4.7.1 netframework-4.7.2 netframework-4.8 netstandard-1.3 netstandard-1.4 netstandard-1.6 netstandard-2.0 xamarinandroid-7.1 xamarinios-10.8 xamarinmac-3.0 ">
<td id="System_Net_Sockets_ProtocolType_Ggp" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Ggp</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">3</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="aade6-104">Gateway-to-Gateway Protocol.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;" data-moniker=" netcore-1.0 netcore-1.1 netcore-2.0 netcore-2.1 netcore-2.2 netframework-1.1 netframework-2.0 netframework-3.0 netframework-3.5 netframework-4.0 netframework-4.5 netframework-4.5.1 netframework-4.5.2 netframework-4.6 netframework-4.6.1 netframework-4.6.2 netframework-4.7 netframework-4.7.1 netframework-4.7.2 netframework-4.8 netstandard-1.3 netstandard-1.4 netstandard-1.6 netstandard-2.0 xamarinandroid-7.1 xamarinios-10.8 xamarinmac-3.0 ">
<td id="System_Net_Sockets_ProtocolType_Icmp" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Icmp</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">1</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="aade6-105">Internet Control Message Protocol.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;" data-moniker=" netcore-1.0 netcore-1.1 netcore-2.0 netcore-2.1 netcore-2.2 netframework-2.0 netframework-3.0 netframework-3.5 netframework-4.0 netframework-4.5 netframework-4.5.1 netframework-4.5.2 netframework-4.6 netframework-4.6.1 netframework-4.6.2 netframework-4.7 netframework-4.7.1 netframework-4.7.2 netframework-4.8 netstandard-1.3 netstandard-1.4 netstandard-1.6 netstandard-2.0 xamarinandroid-7.1 xamarinios-10.8 xamarinmac-3.0 ">
<td id="System_Net_Sockets_ProtocolType_IcmpV6" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">IcmpV6</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">58</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="aade6-106">Internet Control Message Protocol for IPv6</span></p>
</td>
</tr>
Internet 协议版本 6。
Internet Protocol Version 6 (IPv6).
IPv6 Destination Options header.
IPv6 Fragment header.
IPv6 Hop-by-Hop Options header.
IPv6 No Next Header.
IPv6 Routing header.
Internet Packet Exchange Protocol.
Network Disk Protocol (informal).
PARC Universal Packet Protocol.
Raw IP Packet Protocol.
Sequenced Packet Exchange Protocol.
Sequenced Packet Exchange Version 2 Protocol.
Banyan address.
IPv4地址。
IPv6地址。
LDAP地址。
</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_Ccitt" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Ccitt</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">10</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-108">Address for the CCITT protocols (e.g., X.25).</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_Chaos" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Chaos</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">5</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-109">Address for the MIT CHAOS protocol.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_Cluster" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Cluster</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">24</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-110">Address for Microsoft cluster products.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_DataKit" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">DataKit</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">9</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-111">Address for the Datakit protocol.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_DataLink" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">DataLink</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">13</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-112">Address for direct data link interface.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_DecNet" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">DecNet</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">12</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-113">DECnet address.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_Ecma" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Ecma</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">8</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-114">Address for the European Computer Manufacturers Association (ECMA).</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_FireFox" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">FireFox</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">19</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-115">FireFox address.</span>
| Address Family | Value | Description |
|-------------------------------|-------|------------------------------------|
| HyperChannel | 15 | NSC Hyperchannel address. |
| Ieee12844 | 25 | IEEE 1284.4 workgroup address. |
| ImpLink | 3 | ARPANET IMP address. |
| InterNetwork | 2 | Address for IP version 4. |
| InterNetworkV6 | 23 | Address for IP version 6. |
| Ipx | 6 | IPX or SPX address. |
| Irda | 26 | IrDA address. |
| Iso | 7 | Address for ISO protocol. |
</span></p>
<td id="System_Net_Sockets_AddressFamily_Lat" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Lat</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">14</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-124">LAT Address.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;" data-moniker=" netcore-2.0 netcore-2.1 netcore-2.2 netframework-1.1 netframework-2.0 netframework-3.0 netframework-3.5 netframework-4.0 netframework-4.5 netframework-4.5.1 netframework-4.5.2 netframework-4.6 netframework-4.6.1 netframework-4.6.2 netframework-4.7 netframework-4.7.1 netframework-4.7.2 netframework-4.8 netstandard-2.0 xamarinandroid-7.1 xamarinios-10.8 xamarinmac-3.0 ">
<td id="System_Net_Sockets_AddressFamily_Max" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Max</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">29</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-125">MAX Address.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_NetBios" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">NetBios</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">17</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-126">NetBios Address.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_NetworkDesigners" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">NetworkDesigners</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">28</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-127">Addresses supporting protocols for OSI gateways designed for network designers.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_NS" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">NS</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">6</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-128">Xerox NS protocol address.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_Osi" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Osi</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">7</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-129">OSI protocol address.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_Pup" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Pup</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">4</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-130">PUP protocol address.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_Sna" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Sna</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">11</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-131">IBM SNA Address</span></p>
</td>
。</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_Unix" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Unix</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">1</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-132">Local address from Unix to host.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_Unknown" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Unknown</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">-1</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-133">Unknown address family.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_Unspecified" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">Unspecified</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">0</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-134">Unspecified address family.</span></p>
</td>
</tr>
<tr style="box-sizing: inherit;">
<td id="System_Net_Sockets_AddressFamily_VoiceView" style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 14px 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166.667px; min-width: 200px;"><span class="lang-csharp" style="box-sizing: inherit; font-size: 14px;">VoiceView</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; float: none; width: 166px;"><span style="font-size: 14px;">18</span></td>
<td style="box-sizing: inherit; border-color: var(--border); word-wrap: break-word; padding: 14px 0px; line-height: 1.5; vertical-align: top; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; position: relative; float: none; width: 332.667px;">
<p style="box-sizing: inherit; margin: 0px;"><span style="box-sizing: inherit; font-size: 14px;" data-ttu-id="ed926-135">VoiceView address.</span></p>
</td>
</tr>
</tbody>
</table>
<p> </p>
<p> Official Socket documentation link</p>
<p><a title="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket?redirectedfrom=MSDN&view=netframework-4.7.2#属性" href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket?redirectedfrom=MSDN&view=netframework-4.7.2#属性" target="_blank" rel="noopener noreferrer">https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket?redirectedfrom=MSDN&view=netframework-4.7.2</a></p>
<hr />
<h2><a name="t3"></a>3. Bind() Binding and Connect() Connecting</h2>
<p><strong>Bind()</strong> is used to bind an <strong>IPEndPoint</strong> object, used on the server side.</p>
<p><strong>Connect()</strong> is used on the client side to connect to the server.</p>
<p>After creating a <strong>Socket</strong> object, proceed to <strong>bind the local Socket / connect to the server</strong>.</p>
<h3>Bind()</h3>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Bind (System.Net.EndPoint localEP);</pre>
</div>
<p>Usage</p>
<div class="cnblogs_code">
<pre> Socket serverSocket = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress iP </span>= IPAddress.Parse(<span style="color: #800000;">"</span><span style="color: #800000;">127.0.0.1</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">The above is not important, see below</span></pre>
<p><strong> //IPEndPoint iPEndPoint = new IPEndPoint(iP, 2300);</strong><br /><strong> //serverSocket.Bind(iPEndPoint);</strong></p>
<pre> <strong>serverSocket.Bind(<span style="color: #0000ff;">new</span> IPEndPoint(iP, <span style="color: #800080;">2300</span>))</strong></pre>
</div>
<p> You will create a local <strong> IPEndPoint</strong> object that has access to this <strong>ip:post</strong>. The purpose is to bind a port on the local machine, such that all data passing through this port will be under your control.</p>
<p> </p>
<h3>Connect()</h3>
<p> Establishing a connection with the remote host. <strong>Connect()</strong> has four overload methods, but it's sufficient to know that you must provide both an IP and a Post value.</p>
<p>Usage</p>
<div class="cnblogs_code">
<pre> IPAddress iP = IPAddress.Parse(<span style="color: #800000;">"</span><span style="color: #800000;">127.0.0.1</span><span style="color: #800000;">"</span><span style="color: #000000;">);
IPEndPoint iPEndPoint </span>= <span style="color: #0000ff;">new</span> IPEndPoint(iP, <span style="color: #800080;">2300</span><span style="color: #000000;">);
Socket serverSocket </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);<br /><br /> <strong> //Create a connection to the remote host</strong>
<strong> serverSocket.Connect(iPEndPoint);</strong></span></pre>
</div>
<hr />
<h2><a name="t44"></a>4. Listen() Listening for connection requests and Accept() Receiving connection requests</h2>
<h3>Listen()</h3>
<p>Monitors all connection requests sent to this host on the specified port. Used on the server side; not required on the client side.</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Listen (<span style="color: #0000ff;">int</span> backlog);</pre>
</div>
<p>After using <strong>Bind()</strong>, use the Listen() method to monitor, where the backlog <strong>parameter specifies the number of incoming connections that can be queued for acceptance, i.e., the maximum length of the pending connection queue</strong>.</p>
<p> Example</p>
<div class="cnblogs_code">
<pre>serverSocket.Listen(<span style="color: #800080;">10</span>); <span style="color: #008000;">//</span><span style="color: #008000;">Start listening</span></pre>
</div>
<h3>Accept()</h3>
<p><strong>Accept()</strong> synchronously listens on the socket, extracting the first pending connection request from the connection request queue, and then creates and returns a new <strong>Socket</strong> object.</p>
<p>Code example</p>
<div class="cnblogs_code">
<pre> <span style="color: #808080;">//</span><span style="color: #008000;">Create endpoint (EndPoint)</span>
IPAddress ip = IPAddress.Any;
IPEndPoint ipe = <span style="color: #0000ff;">new</span> IPEndPoint(ip, <span style="color: #800080;">8000</span>);
<span style="color: #808080;">//</span><span style="color: #008000;">Create socket and start listening</span>
Socket serverSocket = <span style="color: #0000ff;">new</span> Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(ipe);
serverSocket.Listen(10);<span style="color: #008000;">//</span><span style="color: #008000;">Start listening</span>
<span style="color: #808080;">//</span><span style="color: #008000;">Accept client connection, create a new socket for this connection, and receive information</span>
<strong>Socket temp = serverSocket.Accept();<span style="color: #008000;">//</span><span style="color: #008000;">Create a new socket for the new connection</span></strong></pre>
</div>
<div class="cnblogs_code">
<pre><span style="color: #008000;">//</span><span style="color: #008000;">Close connection</span>
temp.Close(); </pre>
</div>
<p> </p>
<p>Note that each time a connection is established with an <strong>Accept()</strong> object, if you want to facilitate mutual communication between the server and client, you should use the same <strong>Accept()</strong> object. Each <strong>Accept</strong> object starts from the request to establish a connection from the client, and as long as the same <strong>Accept</strong> object is used, data transmission can occur.</p>
Five, Receive() and Send()
- Receive() to receive information
- Send() to send information
These two methods are used on both the server side and the client side.
Receive()
Usage example
string recvStr = ""; byte[] recvBytes = new byte[1024]; int bytes; bytes = temp.Receive(recvBytes, recvBytes.Length, 0);//Accept information from the client recvStr += Encoding.ASCII.GetString(recvBytes, 0, bytes);
Directly copied from Microsoft.
Receive(Byte[], Int32, Int32, SocketFlags, SocketError) |
Uses the specified Socket to receive data from the bound SocketFlags and stores the data in the receive buffer. |
Receive(Byte[], Int32, Int32, SocketFlags) |
Uses the specified Socket to receive a specified number of bytes from the bound SocketFlags and stores them at the specified offset in the receive buffer. |
Receive(IList<ArraySegment<Byte>>, SocketFlags, SocketError) |
Uses the specified Socket to receive data from the bound SocketFlags and stores the data in a list of receive buffers. |
Receive(Byte[], Int32, SocketFlags) |
Uses the specified Socket to receive a specified number of bytes of data from the bound SocketFlags and stores the data in the receive buffer. |
Receive(Byte[], SocketFlags) |
Uses the specified Socket to receive data from the bound SocketFlags and stores the data in the receive buffer. |
Receive(IList<ArraySegment<Byte>>, SocketFlags) |
Uses the specified Socket to receive data from the bound SocketFlags and stores the data in a list of receive buffers. |
Receive(IList<ArraySegment<Byte>>) |
Receives data from the bound Socket and fills the data into a list of receive buffers. |
</span></p>
</td>
</tr>
<tr data-moniker=" netcore-1.0 netcore-1.1 netcore-2.0 netcore-2.1 netcore-2.2 netframework-1.1 netframework-2.0 netframework-3.0 netframework-3.5 netframework-4.0 netframework-4.5 netframework-4.5.1 netframework-4.5.2 netframework-4.6 netframework-4.6.1 netframework-4.6.2 netframework-4.7 netframework-4.7.1 netframework-4.7.2 netframework-4.8 netstandard-1.3 netstandard-1.4 netstandard-1.6 netstandard-2.0 xamarinandroid-7.1 xamarinios-10.8 xamarinmac-3.0 ">
<td><span class="lang-csharp break-text"><a class="xref" href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket.receive?view=netframework-4.7.2#System_Net_Sockets_Socket_Receive_System_Byte___" data-linktype="relative-path">Receive(Byte[])</a></span></td>
<td>
<p><span data-ttu-id="d5a66-2126">Receive data from the bound <a class="xref" href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket?view=netframework-4.7.2" data-linktype="relative-path">Socket</a> and store it in the receive buffer.</span></p>
</td>
</tr>
</tbody>
</table>
<h4 class="propertyInfoTitle stack">Parameters</h4>
<div class="propertyInfo"><dl class="parameterList"><dt><strong>Byte[]</strong> buffer </dt></dl>
<p><span data-ttu-id="d5a66-2357">An array of type Byte where the received data will be stored.</span></p>
</div>
<div class="propertyInfo stack"><dl class="parameterList"><dt><strong>Int32</strong> offset </dt></dl>
<p><span data-ttu-id="d5a66-2358"><code data-dev-comment-type="paramref">buffer</code> parameter position for storing the received data.</span></p>
</div>
<div class="propertyInfo stack"><dl class="parameterList"><dt><strong><span class="lang-csharp">Int32 </span></strong>size</dt></dl>
<p><span data-ttu-id="d5a66-2359">The number of bytes to receive.</span></p>
</div>
<div class="propertyInfo stack"><dl class="parameterList"><dt><strong>SocketFlags</strong> socketFlags<span class="lang-csharp"><br /></span></dt></dl>
<p><span data-ttu-id="d5a66-2360">A bitwise combination of SocketFlags values.</span></p>
<div class="propertyInfo stack"><dl class="parameterList"><dt><strong>SocketError</strong> errorCode</dt></dl>
<p><span data-ttu-id="d5a66-2361">A SocketError object that stores socket errors.</span></p>
<p><span data-ttu-id="d5a66-2361"><strong>socketFlags</strong> defaults to <strong>0</strong> or <strong>None</strong>, and the author does not understand the application scenarios for <strong>socketFlags</strong>.</span></p>
</div>
<h3>Return</h3>
<p>Returns the number of bytes that have been successfully read.</p>
<h3>Send()</h3>
<p>The send() function is similar to the Receive() function,</p>
<p>Example code is as follows</p>
<div class="cnblogs_code">
<pre> <span style="color: #0000ff;">string</span> str =<span style="color: #000000;"> "hello";
</span><strong><span style="color: #0000ff;">byte</span>[] a =</strong><span style="color: #000000;"><strong> Encoding.UTF8.GetBytes(str);</strong>
send </span>= socket.Send(a, <span style="color: #800080;">0</span>);</pre>
</div>
<p> </p>
<p>Both sending and receiving use the byte[] byte stream, so conversion is required during reception.</p>
<hr />
<h2><a name="t5"></a>6. Resource Release</h2>
<p>There is a <strong>Accept</strong> release and a <strong>Socket</strong> release.</p>
<p><strong>Accept</strong> is a connection object, a single <strong>Socket</strong> may have dozens of Accept connections.</p>
<p>Using the <strong>Shutdown( )</strong> method can disable operations on the <strong>Accept</strong> object (disabling sending and receiving for a particular <strong>Socket</strong> object).</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> Shutdown (System.Net.Sockets.SocketShutdown how);</pre>
</div>
<p><strong>SocketShutdown</strong> is an enum type.</p>
<p>Example</p>
<div class="cnblogs_code">
<pre><span style="color: #000000;"> temp.Shutdown(SocketShutdown.Receive);
</span><span style="color: #008000;">//</span><span style="color: #008000;">Disable receiving </span></pre>
</div>
<table style="height: 85px; width: 580px;">
<thead>
<tr><th><span data-ttu-id="d5a66-3509">Value</span></th><th><span data-ttu-id="d5a66-3509">Usage</span></th><th><span data-ttu-id="d5a66-3510">Description</span></th></tr>
</thead>
<tbody>
<tr>
<td><span data-ttu-id="d5a66-3511">Send</span></td>
<td><span data-ttu-id="d5a66-3511">Send</span></td>
<td><span data-ttu-id="d5a66-3512">Disables sending to this <a class="xref" href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket?view=netframework-4.7.2" data-linktype="relative-path">Socket</a>.</span></td>
</tr>
<tr>
<td><span data-ttu-id="d5a66-3513">Receive</span></td>
<td><span data-ttu-id="d5a66-3513">Receive</span></td>
<td><span data-ttu-id="d5a66-3514">Disables receiving to this <a class="xref" href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket?view=netframework-4.7.2" data-linktype="relative-path">Socket</a>.</span></td>
</tr>
<tr>
<td><span data-ttu-id="d5a66-3515">Both</span></td>
<td><span data-ttu-id="d5a66-3515">Both</span></td>
<td class="x-hidden-focus"><span data-ttu-id="d5a66-3516">Disables both sending and receiving for this <a class="xref" href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.sockets.socket?view=netframework-4.7.2" data-linktype="relative-path">Socket</a>.</span></td>
</tr>
</tbody>
</table>
<h3>close()</h3>
<p>This will directly release resources; both <strong>Accept</strong> and <strong>Socket</strong> objects can use it. Objects will be permanently released after use.</p>
<hr />
<h2><a name="t6"></a>7. IPAddress and IPEndPoint</h2>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">//Import<br />using</span> System.Net;</pre>
</div>
<h3>IPAddress to handle and convert IP addresses</h3>
<p>The <strong>IPAddress.Parse()</strong> method converts a dotted-decimal IP representation into an <strong>IPAddress</strong> class.</p>
<div class="cnblogs_code">
<pre>IPAddress ip = IPAddress.Parse(<span style="color: #800000;">"</span><span style="color: #800000;">127.0.0.1</span><span style="color: #800000;">"</span>);<span style="color: #008000;">//</span><span style="color: #008000;">Convert the IP address string to an instance of IPAddress type</span></pre>
</div>
<p>IPAddress provides four read-only fields</p>
<ul>
<li>Any represents any available IP address on the local system</li>
<li>Broadcast represents the local network's IP broadcast address</li>
<li>Loopback represents the system's loopback address</li>
<li>None represents no network interfaces on the system</li>
</ul>
<p>For its types of usage and all methods, constructors, etc., please refer to the Microsoft documentation.</p>
<p>Documentation link <a title="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.ipaddress?view=netframework-4.7.2" href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.ipaddress?view=netframework-4.7.2" target="_blank" rel="noopener noreferrer">https://docs.microsoft.com/zh-cn/dotnet/api/system.net.ipaddress?view=netframework-4.7.2</a></p>
<h3>IPEndPoint represents the binding of an IPAddress object to a port</h3>
<div class="cnblogs_code">
<pre> IPAddress ip = IPAddress.Any; <span style="color: #008000;">//</span><span style="color: #008000;">Convert the IP address string to an instance of IPAddress type</span>
IPEndPoint ipe = <span style="color: #0000ff;">new</span> IPEndPoint(ip, <span style="color: #800080;">8000</span>);<span style="color: #008000;">//</span><span style="color: #008000;">Initialize a new instance of IPEndPoint class with specified port and IP</span></pre>
</div>
<p>The above code creates a listening point with a port of <strong>8000</strong>, and the object represents <strong>all local IPs</strong>.</p>
<p>Additionally, this class can obtain the value range of the ports, but besides that, this class has little significance.</p>
<p>Microsoft documentation link <a title="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.ipendpoint?view=netframework-4.7.2" href="https://docs.microsoft.com/zh-cn/dotnet/api/system.net.ipendpoint?view=netframework-4.7.2" target="_blank" rel="noopener noreferrer">https://docs.microsoft.com/zh-cn/dotnet/api/system.net.ipendpoint?view=netframework-4.7.2</a></p>
<p> </p>
<pre>SocketType</pre>
文章评论