Visual C # .NET Network Program Development - TCP (2)

xiaoxiao2021-03-06  43

Visual C # .Net web application development -Tcp papers (2) Author: Song Hua www.ASPCool.com time: 2002-5-5 17:36:39 Views: 6352

As we said, the TCPClient class creates above the socket, provides a higher level of abstraction in terms of TCP services, reflecting the transmission and acceptance of network data, is TCPCLIENT using standard Stream flow processing technology, making it read and write data more Convenient and intuitive, the .NET framework is responsible for providing a richer structure to process streams, running throughout the entire .NET framework has a broader compatibility, constructing a general method of more general flowomogenesis makes us no longer Need to confuse the actual content of the file (HTML, XML, or anything else), the application will use the consistent method (stream.write, stream.read) to send and receive data. In addition, the stream provides instant access to the data from the Internet download, and can start processing immediately when some data arrives, without waiting for the application to download the entire data set. These processing techniques are implemented through the NetWorkStream class in .NET. The NetWorkStream class contains in the system.net.sockets namespace of the .NET Framework, which provides basic data streams for network access. NetWorkStream implements standard .NET framework flow mechanisms that send and receive data over the network socket. NetworkStream supports synchronization and asynchronous access to network data streams. NetworkStream is inherited from Stream, which provides a set of methods and properties for easy network communication. Like other inheritance of the abstract base stream Stream, the NetworkStream network stream can also be considered a data channel, which is equipped between the data source terminal (customer client) and the receiving end (service server), and the subsequent data read and Writings are made for this channel. In the .NET framework, NetworkStream stream supports two aspects: 1, write flow. Writing is a data transmission from the data structure to stream. Figure 2, reading flow. Read is data transfer from streaming to data structures (such as byte array). Different from ordinary streaming stream is that the network stream does not have a unified concept of the current location, so it does not support the search and random access to the data stream. The corresponding attribute canseek always returns false, while the Seek and Position methods will also trigger NotSupportedException. Based on the application protocol on the Socket, you can get the NetworkStream network data stream in two ways: 1, use the NetworkStream constructor: public networkStream (Socket, FileAccess, Bool); (with overload method), it uses the specified access Permissions and the specified Socket is authorized to create a new instance of the NetWorkStream class for the specified Socket. You need to create a socket object instance before use, and establish a connection with the remote server through the socket.connect method, and then you can use this method to get the network transport stream. .

Examples are as follows: socket s = new socket (addressfamily.internetwork, sockettype.internem, protocoltype.tcp); // Create a client socket object instance try {s.connect ("www.tuha.net", 4088); // Establishment Connection with the remote host} catch (Exception E) {MessageBox.show ("Connection error:" E.Message);} trystream (S, FileAccess.Readwrite, false); // Get network transmission Flow} 2, through the TCPClient.getStream method: public networkStream etStream (); it returns the basic network stream networkStream for transmitting and receiving data. GetStream creates an instance of a NetWorkStream class by using the base socket as its constructor parameters. You need to create a TCPClient object instance before use and establish a connection with the remote host. Examples are as follows: tcpclient tcpclient = new tcpclient (); // Create TCPClient object instance try {tcpclient.connect ("www.tuha.net", 4088) ; // Try to connect to the remote host} catch (exception e) {messagebox.show ("connection error:" E.MESSAGE);} try {networkstream stream = tcpclient.getStream (); // Get network transport stream} catch (Exception E) {MessageBox.show ("TCPCLIENT Error:" E.MESSAGE); The above is the technical information to implement client programming under the .NET, in order to provide these services to the client, we also need to prepare the corresponding server program, the previous "Visual C # .NET Network Program Development - Socket" Once mentioned, Socket is based on other network protocols, which can be developed for client development, or for server-to-server, more in the transmission level, and on the application protocol level, the client we construct built in the Socket class TCPClient replaces Socket; correspondingly, TCPListener built on Socket provides a higher concept-class TCP service, making us more easily writing server applications. It is because of this reason that the application layer protocols like FTP and HTTP are created on the basis of the TCPListener class. TCPListener in .NET is used to monitor the incoming request on the TCP port, create the TCPListener object instance by binding the native IP address and the corresponding port (both should be consistent with the client's request), and start listening by the start method; When TCPListener detects that the client's connection, the AcceptTTCPClient method accepts the incoming connection request and creates a request, or the Acceptsocket method is accepted by the AcceptSocket method, depending on the client's different request, or to process the incoming connection request, or create a Socket to handle the request.

Finally, you need to use STOP to turn off the socket used to listen to the incoming connection, you must also close any instances returned from Acceptsocket or AcceptTCPClient. This process is detailed as follows: First, create a TCPListener object instance, which is implemented by the construction method of the TCPListener class: public tcplistener (port); // Specify the local port public tcplistener (IpendPoint) // Specify the native endpoint public tcplistener Ipaddress, port) / / Specifies the parameters in the native IP address and above, mentioned previously, this is no longer detailed, the only thing to remind is that these parameters are for the server host. The following example demonstrates an instance of the TCPListener class: iphostentry ipinfo = dns.resolve ("127.0.0.1"); // Host Information ipaddressList [] iPlist = ipinfo.ipaddressList; // IP array ipaddress ip = iPlist [0]; / / Ip try {tcplistener tcplistener = new tcplistener (iPaddress, 4088); // Create a TCPListener object instance to listen to the user-end connection} catch (exception e) {MessageBox.show ("TCPListener error:" E.MESSAGE); Subsequently, you need to call the Start method to start the listening: public void start (); Second, when it is listened to a user-end connection, you need to accept the hang-up request, which is completed by calling one of the following two methods: public Socket Acceptsocket (); public tclient accepttcpclient (); Previous method Returns the Socket object on behalf of the client, which can then communicate with the remote computer through the Socket class; the latter method returns the TCPCPClient object that represents the client, and then use the above introduction TCPClient.getStream method Gets the basic network stream of TCPClient, and communicates with the remote computer using the READ / WRITE method using the flow read / write method.

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

New Post(0)