Socket Programming Learning Notes (2), C # Network programming conceptual Introduction

Source: Internet
Author: User
Tags bind socket

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.