. Net network programming-use TcpClient and TcpListener to establish a connection between the client and the server, java Server Android Client

Source: Internet
Author: User

. Net network programming-use TcpClient and TcpListener to establish a connection between the client and the server, java Server Android Client




1. How to establish a connection in. NET



In the network, we can use the IP address to uniquely locate a host. In the host, we need to determine which packet is sent to, through the port number, simply put, the port number does not cause the message packets you want to send to QQ friends to be mistakenly sent to your OC program.


Generally, we call the end Of the connection as the client, which is the active side, and the end that waits for the connection to arrive as the server. This concept is relative.


In. Net, we can use TcpClient to establish a connection, and use TcpListener to listen for the connection, so as to establish a connection between the client and the server.



2. The server establishes a listener.


Using System; using System. collections. generic; using System. linq; using System. text; using System. net; using System. net. sockets; namespace server listens on the port {class Program {static void Main (string [] args) {Console. writeLine ("the server is running... "); // 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]; // same as TcpListener listener = new TcpListener (ip, 8500); // select listener port listener. start (); // Start listening to the Console. writeLine ("Start listening... "); Console. WriteLine (" \ n input \ "Q \" key to exit .. "); ConsoleKey key; do {key = Console. ReadKey (true). Key ;}while (key! = ConsoleKey. Q );}}}


After the program is started, use netstat-a to view the port information:







The port is being listening ....




3. establish a connection between the client and the server



When the server listening port is opened, it can be connected to the server port:



Using System; using System. collections. generic; using System. linq; using System. text; using System. net; using System. net. sockets; connection between the namespace server and the client {class Program {static void Main (string [] args) {# connection between the region client and the server // Console. writeLine ("client startup .. "); // 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 // Console. writeLine ("the server connection is successful .. Local IP port: {0} ------> service IP port: {1} ", client. client. localEndPoint, client. client. remoteEndPoint); // client. the Client obtains the socket # endregion # region connection Console between multiple clients and the server. writeLine ("client started la .... "); TcpClient client; for (int I = 0; I <2; I ++) {try {client = new TcpClient (); // each time a new TcpClient is created, a new Socket is created to communicate with the server ,. net will automatically assign a port number for this socket. // Establish a connection with the server client. connect (IPAddress. parse ("127.0.0.1"), 8500);} catch (Exception ex) {Console. writeLine (ex. message); Console. writeLine (ex. stackTrace); return;} Console. writeLine ("the server connection is successful .. Local IP port: {0} ------> service IP port: {1} ", client. client. localEndPoint, client. client. remoteEndPoint); // client. client Received socket} # endregion }}}



During connection, the TcpClient object is used to pass in the IP address and port number of the server to be connected, just like sending an email, as long as you select an existing sender, it can be sent.








Related Article

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.