C # network programming 2. Socket. tcplistener. tcpclient. client-side Communication

Source: Internet
Author: User
Tags getstream

Call the getstream () method on tcpclient to obtain the stream connected to the remote computer. Note that I have usedRemoteWhen called on the client, it gets the stream connected to the server; when called on the server, it gets the stream connected to the client. Let's take a look.CodeFirst, let's look at the server.

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. net; using system. net. sockets; namespace sockstest {class program {static void main (string [] ARGs) {int buffersize = 1000; console. writeline ("server is running... "); IPaddress IP = new IPaddress (New byte [] {127, 0, 0, 1}); tcplistener listener = new tcplistener (IP, 8500); listener. start (); // start listening for conso Le. writeline ("Start listening..."); tcpclient remoteclient = listener. accepttcpclient (); // accept the pending connection request console. writeline ("Client Connected! {0} <-- {1} ", remoteclient. client. localendpoint. tostring (), remoteclient. client. remoteendpoint. tostring (); networkstream streamclient = remoteclient. getstream (); byte [] buffer = new byte [buffersize]; int bytesread = streamclient. read (buffer, 0, buffersize); console. writeline ("reading data, {0} bytes... ", bytesread); string MSG = encoding. unicode. getstring (buffer, 0, bytesread); console. writeline ("Received: {0}", MSG); console. read ();}}}

Streamclient. the getstream () method obtains the stream connected to the client, reads data from the stream, stores the data in the buffer cache, and then uses encoding. unicode. the getstring () method obtains the actual string from the cache. Finally, print the string on the console. Note: When the total number of bytes of a string that can be read is greater than buffersize, string truncation occurs because the number of cached files is always limited. For large objects, for example, for images or other files, you must use the "read multiple times and then store them" method.

 

 
// Obtain the string byte [] buffer = new byte [buffersize]; int bytesread; // The number of bytes read. memorystream msstream = new memorystream (); do {bytesread = streamtoclient. read (buffer, 0, buffersize); msstream. write (buffer, 0, bytesread);} while (bytesread> 0); buffer = msstream. getbuffer (); string MSG = encoding. unicode. getstring (buffer );

 

Client

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. net. sockets; namespace socksclient {class program {static void main (string [] ARGs) {console. writeline ("client is running ...... "); tcpclient; try {tcpclient = new tcpclient (); tcpclient. connect ("localhost", 8500);} catch (exception e) {console. writeline (E. tostring (); console. read (); return;} Console. writeline ("server connected! {0} --> {1} ", tcpclient. client. localendpoint. tostring (), tcpclient. client. remoteendpoint. tostring (); networkstream streamserver = tcpclient. getstream (); string MSG = "Hi server, this is from client"; byte [] buffer = encoding. unicode. getbytes (MSG); streamserver. write (buffer, 0, buffer. length); console. read ();}}}

 

In the same way as in the previous article, how to implement multi-client and server communication is a challenge. Let's continue to explore it.

1. Single C <---> single s communication

2. Multi-C <---> Single-s communication

3. Single-C multi-information <---> Single-s communication

4. Multi-C multi-information <---> Single-s communication

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.