Visual C #. Net network program development-Socket page 1/2

Source: Internet
Author: User

Visual C #. Net network program development-Socket
Author: Song Hua

Author: www.ASPCool.com

Microsoft. net Framework provides applications with hierarchical, scalable, and governed network services to access the Internet. Its namespace is System. net and System. net. sockets contains a variety of classes to develop a variety of network applications .. . Net class uses a layered structure that allows applications to access the network at different control levels. developers can choose to compile programs at different levels as needed, these levels cover almost all the needs of the Internet-from socket sockets to common requests/responses. More importantly, this layer can be expanded to meet the needs of continuous Internet expansion.

Aside from the layer-7 architecture of the ISO/OSI model, we can look at the logic layer of the TCP/IP model ,. net class can be considered to contain three layers: Request/response layer, application protocol layer, and transmission layer. WebReqeust and WebResponse represent the request/response layer. Classes supporting Http, Tcp, and Udp constitute the application protocol layer, while the Socket class is in the transmission layer. It can be illustrated as follows:

It can be seen that the transport layer is at the bottom of this structure. When the application protocol layer and request/response layer above it cannot meet the special needs of the application, you need to use this layer for Socket programming.

In. Net, the System. Net. Sockets namespace provides a managed implementation of the Windows Sockets (Winsock) interface for developers who need to strictly control network access. System. all other network protocol classes in the. Net namespace are built on the Socket implementation. For example, TCPClient, TCPListener, and UDPClient encapsulate detailed information about TCP and UDP connections created to the Internet; the NetworkStream class provides basic data streams for network access. Many common Internet services can see Socket traces, such as Telnet, Http, Email, and Echo, despite the different definitions of the communication Protocol, the basic transmission of these services uses sockets.

In fact, the Socket can be considered as a data channel like a Stream. This channel is set up between the application end (client) and the remote server end, and then reads (receives) data) and write (send) for this channel.

It can be seen that after a Socket object is created on the application or server, you can use the Send/SentTo method to Send data to the connected Socket, alternatively, use the Receive/ReceiveFrom method to Receive data from the connected Socket;

For Socket programming, the Socket class of the. NET Framework is the managed code version of the Socket service provided by the Winsock32 API. A large number of methods are provided for network programming. In most cases, the Socket class method only sends data to their local Win32 copies and handles any necessary security checks. If you are familiar with Winsock API functions, it will be very easy to write network programs using the Socket class. Of course, if you have never been in touch with it, it will not be too difficult. Follow the instructions below, you will find that there are rules to develop windows network applications using the Socket class, which follow roughly the same steps in most cases.

Before using it, you must first create an instance of the Socket object, which can be achieved through the Socket class constructor:

Public Socket (AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType );

The addressFamily parameter specifies the addressing scheme used by the Socket, The socketType parameter specifies the Socket type, and the protocolType parameter specifies the protocol used by the Socket.

The following example creates a Socket that can be used for communication over TCP/IP-based networks (such as the Internet.

Socket s = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );

To use UDP instead of TCP, you need to change the protocol type, as shown in the following example:

Socket s = new Socket (AddressFamily. InterNetwork, SocketType. Dgram, ProtocolType. Udp );

Once a Socket is created on the client, you can Connect to the specified server through the Connect method and Send data to the remote server through the Send/SendTo method, then, you can use Receive/ReceiveFrom to Receive data from the server. on the server side, you need to Bind the specified interface using the Bind method to associate the Socket with a local endpoint, the Listen method is used to Listen for requests on this interface. When listening for a connection to the user end, the Accept is called to complete the connection operation, and a new Socket is created to process incoming connection requests. After using the Socket, remember to use the Shutdown method to disable the Socket and use the Close method to Close the Socket. The methods/functions used in this process include:

Socket. Connect Method: establish a connection to a remote device
Public void Connect (EndPoint remoteEP) (with overload method)
Socket. Send method: sends data to the connected Socket starting from the indicated position in the data.
Public int Send (byte [], int, SocketFlags); (there are overload methods)
The Socket. SendTo method sends data to a specific endpoint.
Public int SendTo (byte [], EndPoint); (there are overload methods)
Socket. Receive method: receives data from the connected Socket to a specific location in the receiving buffer.
Public int Receive (byte [], int, SocketFlags );
Socket. ReceiveFrom method: receives data from a specific location in the data buffer and stores the endpoint.
Public int ReceiveFrom (byte [], int, SocketFlags, ref EndPoint );
Socket. Bind method: Associate the Socket with a local endpoint:
Public void Bind (EndPoint localEP );
Socket. Listen method: place the Socket in the listening state.
Public void Listen (int backlog );
Socket. Accept method: Create a new Socket to process incoming connection requests.
Public Socket Accept ();
Socket. Shutdown method: Disable sending and receiving on a Socket
Public void Shutdown (SocketShutdown how );
Socket. Close method: Force Socket connection to Close
Public void Close ();

As you can see, the preceding methods contain EndPoint parameters. On the Internet, TCP/IP uses a network address and a service port number to uniquely identify the device. The network address identifies a specific device on the network, and the port number identifies a specific service on the device to be connected. The combination of network addresses and service ports is called an endpoint. in the. NET Framework, the EndPoint class indicates the EndPoint. It provides an abstraction that represents network resources or services and marks network addresses and other information .. Net also defines the child of an EndPoint for each supported address family. For an IP address family, this class is IPEndPoint. The IPEndPoint class contains the host and port information required by the application to connect to the service on the host. The IPEndPoint class forms a connection point to the service by combining the Host IP address and port number of the service.

When using the IPEndPoint class, it inevitably involves the computer ip address. There are two types of. Net IP address instances:

IPAddress: The IPAddress class contains the IP address of the computer on the IP network. The Parse method can convert an IP address string to an IPAddress instance. The following statement creates an IPAddress instance:

IPAddress myIP = IPAddress. Parse ("192.168.1.2 ");

Dns: Provides domain name services to applications that use TCP/IP Internet services. Its Resolve method queries DNS servers to map user-friendly Domain Names (such as "host.contoso.com") to digital Internet addresses (such as 192.168.1.1 ). The Resolve method returns an IPHostEnty instance that contains a list of the addresses and aliases of the requested names. In most cases, you can use the first address returned in the AddressList array. The following code obtains an IPAddress instance that contains the IP address of host.contoso.com.

IPHostEntry ipHostInfo = Dns. Resolve ("host.contoso.com ");
IPAddress ipAddress = ipHostInfo. AddressList [0];

You can also use the GetHostName method to obtain the IPHostEntry instance:

IPHosntEntry hostInfo = Dns. GetHostByName ("host.contoso.com ")

When using the preceding methods, you may need to handle the following exceptions:

SocketException exception: the operating system error occurs when the Socket is accessed.

ArgumentNullException exception: the parameter is null.

ObjectDisposedException exception: the Socket has been closed.

After learning the above knowledge, the following code combines the IP address and port number of the server host (host.contoso.com) to create a remote endpoint for the connection:

IPEndPoint ipe = new IPEndPoint (ipAddress, 11000 );

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.