1. Flowchart
First look at the programming process for connection-oriented sockets (TCP) in C #:
2, the server to establish a connection, send and receive data, close the connection program segment:
1) Establish a connection
//Create a local socket object, IPV4 addressing method, TCP-based stream socket (streaming socket 0)Socket Localsocket =NewSocket (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); Iphostentry Local= Dns.gethostbyname (Dns.gethostname ());//hfy:: Getting a native hostIPEndPoint IEP =NewIPEndPoint (Local. addresslist[0],8088);//hfy:: Get the IP and port number of the local hostLocalsocket.bind (IEP);//hfy:: Bind socket to local portLocalsocket.listen (Ten);//hfy:: Start monitoringSocket clientsocket = localsocket.accept ();//hfy:: Wait for the connection to be accepted, return a new socket that communicates with the client
2) Send and receive data
Clientsocket.send (Encoding.ASCII.GetBytes ("server Send data")); // Hfy:: Sending data to the client // Hfy:: Defines a 11,024-byte receive buffer, the data received from the client is stored as a buffer byte New byte[[1024x768]; int receivednum = clientsocket.receive (buffer);
3) Close the connection
// Hfy::shutdown function stops answering, ensures all data is sent and received, and then closes the socket connection Localsocket.shutdown (Socketshutdown.both); Localsocket.close ();
This method allows the socket object to wait until the data for the internal buffer has been sent out.
3, the client connects to the server, send and receive data, close the connection:
1) Connect to the server
// hfy:: Specify the IP and port number of the server, call the Connect function to initiate a connection to the server, and bind itself to the system-assigned endpoint IPAddress remotehost = Ipaddress.parse ("10.64.24.77"new IPEndPoint ( RemoteHost,8088new Socket (AddressFamily.InterNetwork, SocketType.Stream, PROTOCOLTYPE.TCP); Localsocket.connect (IEP);
2) Send and receive data
The process of sending and receiving data is similar to the server:
Localsocket.send (Encoding.ASCII.GetBytes ("client Send data")); // hfy:: Client sends data to server // Hfy:: Defines a 11,024-byte receive buffer in which data received from the server is stored in the buffer byte New byte[[1024x768]; int receivednum = localsocket.receive (buffer);
3) Close the connection
Closing the connection process is the same as the server:
Localsocket.shutdown (Socketshutdown.both); Localsocket.close ();
4. TCP class:
In order to simplify network programming,. NET encapsulates sockets and encapsulates the TcpListener class and the TcpClient class. The TcpListener class is used to listen for connection requests from clients, and the TcpClient class is used to provide connection information for local hosts and remote hosts.
4. Write the server using the TCP class TcpListener:
1) Create a TcpListener object, bind to the local IP and port number, and invoke the Start method on the specified port to listen:
IPAddress Localip = dns.gethostaddresses (Dns.gethostname ()) [0]; // hfy:: Get local IP New 8088); // hfy:: Create TcpListener object Tcplistener.start (); // hfy:: Start monitoring
2) in a separate thread, the loop calls the AcceptTcpClient method to accept the client's connection request, which returns the TcpClient object, using the GetStream method of the returned TcpClient object to get the NetworkStream object. The BinaryReader object and the BinaryWriter object are obtained through the NetworkStream object to prepare for communication with the client:
//hfy:: Create a thread that loops through the client's connection, Acceptclientconnect is a function of its own definitionThread threadaccept =NewThread (Acceptclientconnect); Threadaccept.start (); //hfy:: Start thread//hfy:: Thread-executed Acceptclientconnect method for receiving requests from clientsvoidAcceptclientconnect () { while(true)//Hfy:: Cyclic acceptance { Try{tcpClient= Tcplistener.accepttcpclient ();//hfy:: Get TcpClient object if(TcpClient! =NULL) {NetworkStream= Tcpclient.getstream ();//hfy:: Get NetworkStream Object } } Catch { } }}
C # Client Server development (i)