. NET network programming--using tcpclient and TcpListener to establish client-to-server connectivity

Source: Internet
Author: User




one,. NET how to make a connection



in the network, we can only locate a host by IP address, and in the host, we want to determine who received the packet sent to WHO, can be through the port number, the role of the port number simply is not so that you want to send QQ friends of the message packet was mistakenly sent to your OC program.


In general, we refer to the end of the initiating connection as the client, which is the active side, while silently waiting for the end of the connection to be the server. The concept is relatively relative.


In. NET, we can use TcpClient to establish a connection, use TcpListener to listen to the connection, and establish a connection between the client and the server.



two, the server set up monitoring


Using system;using system.collections.generic;using system.linq;using system.text;using System.Net;using System.net.sockets;namespace server listens on port {class program    {        static void Main (string[] args)        {            Console.WriteLine ("Good service in running ah ... ");            IPAddress IP = new IPAddress (new byte[] {127, 0, 0, 1});           IPAddress IP = ipaddress.parse ("127.0.0.1");            IPAddress IP = dns.gethostentry (Dns.gethostname ()). ADDRESSLIST[0]; Ibid            . TcpListener listener = new TcpListener (IP, 8500);  Select the listening port            listener. Start ();//begin Listening for            Console.WriteLine ("Start listening ... ");            Console.WriteLine ("\ n \ nthe input \" q\ "Key exits: ");            Consolekey key;            Do            {                key = Console.readkey (true). Key;            } while (KEY!=CONSOLEKEY.Q);}}}    


After starting the program, use NETSTAT-A to view the port condition:







Discovery Port is listening ....




third, the client and the server to establish a connection



With the server listening port open, you can connect to the server port:



Using system;using system.collections.generic;using system.linq;using system.text;using System.Net;using            System.net.sockets;namespace Server connection to client {class Program {static void Main (string[] args) { #region client-to-server connection//console.writeline ("client-initiated La La La.                ");                TcpClient client = new TcpClient (); Try//{////Connect to the server//client.                Connect (Ipaddress.parse ("127.0.0.1"), 8500); }//catch (Exception ex)//{//Console.WriteLine (ex.                Message);                Return ////Print the information connected to the server side//console.writeline ("The server is connected successfully. The local IP port is: {0}------> Service IP port: {1} ", client. Client.localendpoint, client. Client.remoteendpoint);//client. The client gets to the socket #endregion #region multiple client-to-server connections Console.WriteLine ("Clients start La la la ....                "); TcpClient client;               for (int i = 0; i < 2; i++) {try { Client = new TcpClient ();//Each creation of a new TcpClient is equivalent to creating a new socket socket with server-side communication.                        NET will automatically assign a port number to this socket. Establish a connection client with the server.                    Connect (Ipaddress.parse ("127.0.0.1"), 8500); } catch (Exception ex) {Console.WriteLine (ex.                        Message); Console.WriteLine (ex.                        StackTrace);                    Return } Console.WriteLine ("The server is connected successfully. The local IP port is: {0}------> Service IP port: {1} ", client. Client.localendpoint, client. Client.remoteendpoint);//client. Client gets to socket} #endregion}}}



When connecting, the main thing is to use the TcpClient object, passing in the IP and port number of the server you want to connect to, just like sending e-mail, as long as you choose a good sender, you can send that.








. NET network programming--using tcpclient and TcpListener to establish client-to-server connectivity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.