TcpClient class
1 //Construction Method 12TcpClient T =NewTcpClient ();3T.connect ("www.163.com",8000);4 //Construction Method 25IPEndPoint IEP =NewIPEndPoint (Ipaddress.parse ("192.168.10.27"),8000);6TcpClient t2 =NewTcpClient (IEP);7T2. Connect ("www.163.com",8000);//You can also connect to a remote device using the Connect method8 //Construction Method 39TcpClient t3 =NewTcpClient ("www.163.com",8000);Ten One A //Common Methods - //Close (); To close a TCP connection - //Connect (); Used to establish a TCP connection with a remote device the //GetStream Returns the NetworkStream used to send and receive data - //GetType () Gets the type of the current instance - - //Common Properties + //availabe Gets the amount of data that has been received from the network and is available for reading - //Client Gets or sets the base socket + //Connected Gets a value that indicates whether the underlying socket of TcpClient is connected to a remote host A //ExclusiveAddressUse Gets or sets a Boolean value that specifies whether TcpClient allows only one client to use the port at //lingerstate Get or set socket hold time - //Nodelay Gets or sets the time that the socket remains - //Receiverbuffersize Gets or sets the size of the TCP receive buffer - //ReceiveTimeout Gets or sets the time-out for socket receive data - //Sendbuffersize Gets or sets the size of the TCP send buffer - //Sendtimeout Gets or sets the time-out of the socket sending data
TcpListener class:
1 //constructor Function2 //tcplistener (int port);3 //TcpListener (IPEndPoint ipe);4 //TcpListener (IPAddress addr,int port);5 //at least one parameter is required, which is the port number6 7 //methods of the TcpListener class8 //AcceptSocket receives a connection from the port and assigns it a socket object9 //AcceptTcpClient receives a connection from the port and assigns it TcpClient objectTen //Pending Determine if there are pending connection requests One //Start listens for incoming connection requests A //Stop shutdown Listener - - the //the process code for generating the TcpListener object and listening to incoming connections is as follows: - //Initializing Objects -TcpListener Server =NewTcpListener (Ipaddress.parse ("192.168.1.1"),8000); - //Start listening ports + Server.start (); - //This object receives data sent from the client +TcpClient newclient = Server.accepttcpclient ();
Test code:
1 //Client2 3 Static voidMain (string[] args)4 {5 Try6 {7 //build the TcpClient object and connect to localhost on 40018TcpClient newclient =NewTcpClient ();9Newclient. Connect ("127.0.0.1",4001);TenNetworkStream stm =newclient. GetStream (); One //sending data using NetworkStream objects A //byte[] sendbytes = Encoding.ASCII.GetBytes ("Data is coming" + "here"); - - stringStrtosend =console.readline (); the byte[] Sendbytes =Encoding.ASCII.GetBytes (strtosend); - -Stm. Write (Sendbytes,0, sendbytes.length); - //Close TcpClient + newclient. Close (); - console.readline (); + } A Catch(Exception e) at { - //Output Exception Information - Console.WriteLine (e.tostring ()); -Console.WriteLine ("Data has not been received"); - console.readline (); - } in}
1 //Server2 3 Static voidMain (string[] args)4 {5 //Server Simple Listener writing6 Try7 {8 //Create a TcpListener object, listen on port 4001, listen with the Start () method9TcpListener listener =NewTcpListener (4001);Ten Listener. Start (); One //The accepttcpclient () method takes a connection request, returns a TcpClient, and uses its GetStream method to create the NetworkStream object ATcpClient TC =Listener. AcceptTcpClient (); -NetworkStream stm =TC. GetStream (); - byte[] Redbuf =New byte[ -]; the //reading data using the Read () method -Stm. Read (Redbuf,0, -); - //Show Data - Console.WriteLine (Encoding.ASCII.GetString (REDBUF)); + STM. Close (); - console.readline (); + } A Catch(Exception e) at { - Console.WriteLine (e.tostring ()); - console.readline (); - } -}
TcpClient class and TcpListener class