C # Network Communication

Source: Internet
Author: User

Server:

Using system. net; using system. net. sockets; using system. collections. generic; using system. LINQ; using system. text; namespace serverconsole {class server {static void main (string [] ARGs) {console. writeline ("server is runing... "); IPaddress IP = new IPaddress (New byte [] {127,0, 8500}); tcplistener listener = new tcplistener (IP,); listener. start (); // start listening while (true) {// gets a connection, the method of interruption tcpcl Ient remoteclient = listener. accepttcpclient (); // print the client connection information console. writeline ("Client Connected! {0} ----> {1} ", remoteclient. Client. localendpoint, remoteclient. Client. remoteendpoint );}}}}

Client:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. net; using system. net. sockets; namespace clientconsole {class client {static void main (string [] ARGs) {console. writeline ("client is running... "); tcpclient client = NULL; For (INT I = 0; I <3; I ++) {client = new tcpclient (); try {// establish a connection with the server client. connect ("localhost", 8500);} catch (exception ex ){ Console. writeline ("exception occured .." + ex. Message); return;} // The output is already connected to console. writeline ("server connected! {0} --> {1} ", client. client. localendpoint, client. client. remoteendpoint);} consolekey key; do {key = console. readkey (true ). key;} while (key! = Consolekey. q );}}}

 

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.