TcpClient class and TcpListener class
(1) Use of TcpClient:
Used to link, send, and receive stream data over the network in synchronous block mode, in which case a request to listen for this connection must be received and the task being listened to is given to the TcpListener instance or socket instance
(2) two ways to connect to the listener TcpClient
First: Create a tcpclient and invoke one of the 3 available connect methods
Second: Using the host name and port number of the remote host to create the TcpClient, this constructor will automatically try a connection
(3) Common Properties and methods of TcpClient
The available property gets the amount of data that has been received from the network and is available for reading
The connected property gets a value that indicates whether the underlying socket of TcpClient is connected to the remote host
The Receivebuffersize property gets or sets the size of the receive buffer
The Sendbuffersize property gets or sets the size of the send buffer
The Close method frees the TcpClient instance and does not close the underlying connection
The Connect method connects a client to a TCP host using the specified host name and port number
The GetStream method returns the Networdstream used to send and receive data
(4) TcpListener commonly used properties and methods
Localendpoint property gets The base of the current TcpListener endpoint
Server properties get base network socket
Acceptsocket/accepttcpclient method receives a pending link request
The Start method starts listening for incoming connection requests
The Stop method closes the listener
(5) example code (partial)
private void Button1_Click (object sender, EventArgs e) {//Instantiate a TcpListener object and initialize to null TcpListener tcplistener = null; Instantiate a IPAddress object to represent the network IP address IPAddress IPAddress = Ipaddress.parse (TextBox1.Text); Defines an int type variable that is used to store the port number int port = Convert.ToInt32 (TextBox2.Text); TcpListener = new TcpListener (ipaddress, Port); Initializes the TcpListener object TcpListener. Start (); Start TcpListener Listen richTextBox1.Text = "Wait for connection ... \ n"; TcpClient TcpClient = null; Instantiates an TcpClient object and assigns a value of NULL if (TcpListener. Pending ())//Determine if there is a pending connection request TcpClient = TcpListener. AcceptTcpClient (); Use AcceptTcpClient to initialize the TcpClient object Else TcpClient = new TcpClient (TextBox1.Text, Port); Initialize the TcpClient object using the TcpClient constructor richTextBox1.Text + = "Connection succeeded! \ n "; TcpClient. Close (); Close the TcpClient connection TcpListener. Stop (); Stop TcpListener Listen}
UdpClient class
(1) The UdpClient class is used to send and receive non-connected UDP data in a blocking synchronous mode.
UDP is a non-connected transport protocol, so there is no need to establish a remote host connection before sending and receiving data.
(2) two ways to set up a default remote host
First: Create an instance of the UdpClient class using the remote host name and port number as parameters
Second: Create An instance of the UdpClient class, and then call an instance of the Connect class
(3) Common Properties and methods of the UdpClient class
Available property gets the amount of readable data received from the network
Client property gets or sets the socket for the underlying network
The Close method closes the UDP connection
Connect method resume Default remote host
The Receive method returns a UDP datagram that has been sent by the remote host
The Send method sends a UDP datagram to a remote host
(3) Example code:
Textbox1:ip Address
TextBox2: Port number
TextBox3: Information sent
RichTextBox: Displaying information
private void Button1_Click (object sender, EventArgs e) {richTextBox1.Text = string. Empty; Instantiate the UdpClient object udpclient udpclient = new UdpClient (Convert.ToInt32 (TextBox2.Text)); Call connect for the UdpClient object to establish the default remote host UdpClient. Connect (TextBox1.Text, Convert.ToInt32 (TextBox2.Text)); Defines a byte array to hold the information sent to the remote host byte[] sendbytes = Encoding.Default.GetBytes (TextBox3.Text); Call the Send method of the UdpClient object to send the UDP datagram to the remote host UdpClient. Send (Sendbytes, sendbytes.length); Instantiates the IPEndPoint object, which is used to display the identity of the response host IPEndPoint IPEndPoint = new IPEndPoint (ipaddress.any, 0); Call the Receive method of the UdpClient object to get the UDP datagram returned from the remote host byte[] receivebytes = udpclient. Receive (ref IPEndPoint); Converts the obtained UDP datagram to a string form returndata = Encoding.Default.GetString (receivebytes); richTextBox1.Text = "received message:" + returndata.tostring (); Address and port using the IPEndPoint objectProperty gets the IP address and port number of the response host richTextBox1.Text + = "\ n This message comes from host" + IPEndPoint. Address.tostring () + "on" + IPEndPoint. Port.tostring () + "port"; Close the UdpClient connection udpclient. Close (); }