C # Network programming is actually a further packaging of the Winsock.
1. Ipaddress,ipendpoint, SocketAddress class
IPAddress is an IP address class, IPEndPoint is Ipaddress+port, the corresponding is sockaddr_in this struct. The SocketAddress class can store information after the serialization of the IPEndPoint class.
1.1 These two classes are commonly used and noted in some places: ¼
1) IPAddress IP = ipaddress.parse ("192.168.123.1");
2) Ipaddress.any is 0.0.0.0, which is used when there are multiple network interfaces in the system and the user does not want to bind the socket to any interface.
3 Ipaddress.none is 255.255.255.255, which is often used to create a pseudo socket.
4 a method to obtain the native IP address: dns.gethostbyname (Dns.gethostname ()). ADDRESSLIST[0]
2, Socket class System.Net.Sockets.Socket in C #
public Socket(
AddressFamily addressFamily,
SocketType socketType,
ProtocolType protocolType
);
In the above constructor, SocketType and ProtocolType should correspond to themselves. The corresponding relationship is as follows:
Dgram (UDP) connectionless communication
Stream (TCP) connection-oriented communication
Raw (ICMP) ICMP protocol
Raw (RAW) Simple IP packet
Socket T = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp);
2.1 Socket Socket class common properties and Functions
Blocking is used to set whether the socket is blocked
Connected Indicates whether the Socket is connected to a remote host. Even if Connected returns True, it is not guaranteed that the Socket is still in the Connected state. A value of true only means that the Socket is connected during the last I/O operation.
The SetSocketOption () method, which sets the specified Socket option to the specified value, appears to set some values for the protocol header. (Too many options, look at MSDN, check with Google, you can find many examples)
Other methods, such as bind (), listen (), send (), receive () and winsocket are essentially the same. Slightly
2.2 Socket exception, socketexception
2.3 To simplify the socket operation, C # provides 3 socket assistants:
TcpClient, TcpListener, UdpClient