TCP synchronous transmission: sent by the client and received by the server segment

Source: Internet
Author: User

1. server programs

You can call the GetStream () method on TcpClient to obtain the network stream NetworkStream linked to the remote computer. When called on the client, he gets the stream from the server. When called on the server, he gets the stream from the client.

 

  

Class Program {static void Main (string [] args) {const int BufferSize = 8192; // cache size Console. writeLine ("server is running... "); IPAddress ip = new IPAddress (new byte [] {192,168, 1,102}); // IP TcpListener listener = new TcpListener (ip, 8500); listener. start (); // Start listening to the Console. writeLine ("start listener... "); // get a method for Link interruption TcpClient remoteclient = listener. acceptTcpClient (); // print the information Console linked to the client. wr IteLine ("client connected! Local: {0} <-- Client: {1} ", remoteclient. client. localEndPoint, remoteclient. client. remoteEndPoint); // gets the stream and writes it to NetworkStream streamToClient = remoteclient in the Buffer. getStream (); byte [] buffer = new byte [BufferSize]; int bytesRead = streamToClient. read (buffer, 0, BufferSize); // obtain the request string msg = Encoding. unicode. getString (buffer, 0, bytesRead); Console. writeLine ("Received: {0} [{1} bytes]", msg, bytesRea D); Console. WriteLine ("\ n input \" Q \ "key to exit. "); ConsoleKey key; do {key = Console. ReadKey (true). Key ;}while (key! = ConsoleKey. Q );}}View Code

 

If the transmitted data is large in bytes, such as slices and audios, the Code is as follows:

 

// Obtain the string byte [] buffer = new byte [BufferSize]; int bytesRead; // number of bytes read MemoryStream MS = new MemoryStream (); do {bytesRead = streamToClient. read (buffer, 0, BufferSize); ms. write (buffer, 0, bytesRead);} while (bytesRead> 0); buffer = msStream. getBuffer (); string msg = Encoding. unicode. getString (buffer );View Code

 

 

 

2. Client Program

The client sends the string code to the server, which is similar to that on the server. First, the client obtains the stream linking to the server, saves the string to the buffer cache, and then writes it to the stream, the process of writing a stream is equivalent to sending a message to the server.

 

Class Program {static void Main (string [] args) {Console. writeLine ("client is running... "); TcpClient clint; try {clint = new TcpClient (); // establish a connection with the server. connect (IPAddress. parse ("192.168.1.102"), 8500);} catch (Exception ex) {Console. writeLine (ex. message); return;} // print the information Console connected to the server. writeLine ("client connected! Local: {0} <-- Client: {1} ", clint. client. localEndPoint, clint. client. remoteEndPoint); // the message to be sent string msg = "hello"; NetworkStream streamToServer = clint. getStream (); // get the cache byte [] buffer = Encoding. unicode. getBytes (msg); // send streamToServer. write (buffer, 0, buffer. length); Console. writeLine ("Sent: {0}", msg); Console. writeLine ("\ n input \" Q \ "key to exit. "); ConsoleKey key; do {key = Console. ReadKey (true). Key ;}while (key! = ConsoleKey. Q );}}View Code

 

In this way, you can successfully send and receive a string. If you want to complete some complex and uninterrupted interactions, you need to make some adjustments on your own.

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.