C # network programming

xiaoxiao2021-03-06  41

C # Network Programming Study Author: Ma Jinhu www.ASPCool.com Time: 2002-6-25 18:00:06 Views: 11074

We know one of the differences between C # and C , that is, he has no class library, the class library used is the class library in the .NET Framework SDK. Two namespaces are provided for network programming in the .NET Framework SDK: "System.Net" and "System.Net.Sockets". C # is a network communication with the classes and methods packaged in these two namespaces. First we explain the concepts often encountered during the network program: Synchronous, Asynchronous, Block and Nonclock: The so-called synchronization mode is the sender's sending data package. The next packet is then transmitted for unequal acceptance. The asynchronous mode is that after the sender sends a packet, it will then send the next packet after the acceptor response will be sent. The blocking socket refers to the network call to which this socket is executed until the call is successful, otherwise this set of hips is blocked on the network call, such as calling the StreamReader class Readlin () method read the network buffer. Data, if there is no data arrival when calling, then this readlin () method will always hang up until some data is read, and this function call returns; not blocking socket refers to this set When the word is called, no matter whether it is successful, it will return immediately. The READLIN () method of the StreamReader class is also called in the network buffer, and the data is returned if it is read or not, it will not be hanging on this function call. In Windows Network Communication Software Development, the most common method is asynchronous non-blocking socket. The method of usually said C / S (client / server) structure is asynchronous non-blocking mode. In fact, in the network programming with C #, we don't need to understand what synchronous, asynchronous, blocking, and non-blocking principles and working mechanisms, because these mechanisms have been packaged in .NET Framewrok SDK. Below we use C # to open a specific network program to illustrate the problem. One. The program design and operational environment described herein (1). Microsoft Window 2000 Server Edition (2) .. Net Framework SDK Beta 2 or above. Key Steps and Workarounds of Server-side programming: In the following programs, we use asynchronous blocking ways. (1). First, create a "TCPListener" object listening to the request on the network on a given port. An instance of a socket for processing access connection requests is generated by calling the "Acceptner" method by calling the "TCPListener" object after receiving the connection request.

Below is the specific implementation code: // Create a TCPListener object, this object is mainly listening to the giveholding port to listen to TCPListener = new TCPListener (1234); // Start listening TCPListener.Start (); // Return can be used to handle Connected Socket Instance SocketForClient = tcplistener.acceptSocket (); (2). Accept and send client data: At this point the socket instance has been generated, if there is a request on the network, after the request passes, the Socket instance constructs a "networkStream" object, "NetWorkStream" object provides basic data stream for network access. We have implemented access to the "NetWorkStream" object by encapsulated by Name Space "System.io". The Readline () method in the "StreamReader" class is to read a line of characters from the "NetworkStream" object; the WriteLine () method in the "StreamWriter" class is to write a line string in the "NetWorkStream" object.

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

New Post(0)