Method for implementation of C # host socket (1)

zhaozj2021-02-16  72

Socket is a socket, which is the most frequently encountered concept and tool in network programming. In the TCP / IP network, transmit and receive data often use to Socket, because complex data can be processed on the network using Socket, which involves data transfer and reception, usually use Socket, It can be seen that you have to master the network programming, which is very important to master Socket. Due to the complexity of the socket itself, it is more difficult to master it. Visual C # is the main language recommended by Microsoft's recommendation. With .NET's deep people, there are currently many far-sighted companies to turn the previous software to the .NET platform. Mastering Network Programming is always the focus of learning a development language. This is the same for Visual C #. Visual C # Implementing Network Functions Its key is also the method of use of managed Socket. This article describes the implementation methods of using managed Socket to realize network data transfer and reception in Visual C # and their precautions.

One. Introduction Socket:

Socket was born in the early 1980s, the US government's advanced research engineering (ARPA) provided funds to Berkeley, California, Berkeley, entrusting them to implement TCP / IP protocols under UNIX operating systems. So the R & D personnel have developed an API (application interface) for TCP / IP network communication. This API is called a Socket interface (socket). So sometimes, Socket is the API on TCP / IP.

In the 1990s, some of the network developers, including Sun and Microsoft companies, together, together with a set of network programming interfaces under Windows, the WindowsSockets specification, referred to as the Winsock specification. The Winsock specification is a set of open, supporting a network programming interface under Windows in a variety of protocols. From 1.0 version from 1991 to version 2.0.8, it has become a factual standard for Windows network programming with the strong support of Intel, Microsoft, Sun and other companies. At present, the Winsock specification has mainly version 1.1 and version 2.0. The most important difference between the two is the 1.1 version only support TCP / IP protocol, and version 2.0 can support multi-protocols. 2.0 has good backward compatibility, any source code, binary files, binary files, and applications can be used without modifying it in 2.0 specification.

It can be seen that the Socket interface is actually a TCP / IP network API interface function. Socket data transfer is actually a special I / O. Common Socket types There are two types: stream socket (socket_stream) and datagram socket (sock_dgram). Flow is a connection-oriented socket, targeting a connected TCP service application; Data reported Socket is an unconnected socket, corresponding to a connected UDP service application.

two. Visual C # Operation Socket:

Although Visual C # can be transmitted, receive data, but NetWorkStream is very limited in use, using NetWorkStream only transmits and receives data type data, and if you want to transfer some complex data such as: binary data Wait, it seems to be limited. However, when using NetWorkStream to handle itself, it is more convenient than Socket. Socket can almost handle any data types that need to be transmitted in the network. We know that the difference between Visual C # and Visual C is that Visual C # does not belong to your own class library, and Visual C is there, Visual C # The class library used by Visual C # is the .NET framework for all development .NET platform programs A common class library provided by the language - .NET Framework SDK. Visual C # primary network features primarily use the two namespaces "System.Net.Sockets" and "System.Net" in the .NET Framework SDK. The implementation of the socket is the Socket class in the namespace "System.Net.sockTs". Visual C # Implements the host of the Socket by creating an instance of the Socket class. After you create a socket instance in Visual C #, you can bind to the endpoints specified in the network through this Socket instance, or you can establish a connection to the specified endpoint through its Connect method. After the connection is created, you can send the data to the socket using its Send or Sendto method; you can read data from the socket using its Receive or ReceiveFrom method. After the Socket is used, use its Shutdown method to disable the socket and use the Close method to close the socket. Tables 01 and Table 02 are common attributes and methods in the socket class and their brief description.

Property description

AddressFamily Gets the address family of Socket.

Available Gets the amount of data that has been received from the network and is available.

Blocking gets or sets a value indicating whether the socket is in blocking mode.

Connected gets a value that indicates whether the socket is connected to the remote resource.

Handle Gets the operating system handle of the socket.

LocalendPoint Gets the local endpoint.

ProtocolType Gets the protocol type of the socket.

RemoteEndPoint Gets the remote endpoint.

SocketType Gets the type of socket.

Table 01: Common properties and descriptions of the Socket class

Method description

Accept creates a new Socket to process incoming connection requests.

BeginAccept starts an asynchronous request to create a new Socket to accept incoming connection requests.

BeGinconnect starts an asynchronous request to connect to the network device.

BeginReceive starts receiving data from the connected socket.

BeginReceiveFrom starts receiving data from the specified network device.

Beginsend sends data asynchronously to the connection

BeGinsendto sends data to a particular remote host.

Bind causes the socket associated with a local endpoint.

Close enforces socket connection to close.

Connect is established to the remote device.

Endaccept ends asynchronous requests to create new sockets to receive incoming connection requests

EndConnect ends the pending asynchronous connection request.

EndRecEive ends the pending asynchronous reading.

EndReceiveFrom ends hang, asynchronously read from a specific endpoint.

Endsend ends the pending asynchronous transmission

EndsendTo ends the pending and sends asynchronous to the specified location.

GetSocketOption returns the value of the socket option.

IOCONTROL Sets low-level operating modes for Socket

Listen places Socket in a listening state.

Poll

Receive receives data from the connection socket.

ReceiveFrom receives the data text report and stores the source endpoint.

SELECT determines the status of one or more sockets.

Send sends the data to the connection

Sendto sends data to a specific endpoint.

SetSocketOption Sets the socket option.

Shutdown disables sending and receiving on a Socket.

Table 02: Common methods of Socket class and its description

Among them, "Beginaccept" and "Endaccept", "BeginConnect" and "EndConnect",

"BeginReceive" and "Endreceive", "BeginReceiveFrom", and "EndreceiveFrom",

"Beginsend" and "endsend", "beginsendto" and "endsendto" are six sets of asynchronous methods,

Its functionality is equivalent to "Accept", "Connect", "Receive", "ReceiveFrom",

"Send" and "Sendto" methods.

Below, the specific method is introduced to how to implement data transfer and reception through managed Socket in Visual C # through a specific example.

The example of this article is actually consisting of two parts, or it is also possible to be a client program and a server program. The client program function is passed

Socket creates a connection to the server program and sending data to the server after the connection is completed; the server program accepts the connection request of the network's socket, after the connection is complete, receive data sent from the client, and display come out. Let's first introduce Visual C # to implement the specific method of the client program by hosting socket.

转载请注明原文地址:https://www.9cbs.com/read-16394.html

New Post(0)