C # Network programming (Asynchronous transfer string)

Source: Internet
Author: User
Tags time interval

In this article we will take a big step forward by using the asynchronous way to program the server so that it becomes a truly servers: it can serve multiple requests to many clients. But before we begin, we need to address one of the remaining issues in the previous section.

Issues when messages are sent

The question is: When the client writes data to the stream two times (such as a string), we consider the two writes as two requests subjectively, while the server may combine the two times as a request, especially if the two request interval is shorter. Similarly, it is possible for a client to issue a request, but the server treats it as a two request processing. The following is a list of possible scenarios, assuming that we are sending two "Welcome to tracefact.net!" on the client side, which may have three scenarios when the data arrives at the server:

Note: Here we assume that ASCII encoding is used because at this point the box above represents a byte, and the string reaches the end of the last 0 (because byte is a value type and the smallest is 0).

The first case above is the ideal scenario where two messages are treated as two separate requests that are fully received by the server. The second scenario is as follows, at which point a message is received as two messages:

In the third case, the two messages are merged into a single receive:

If you download the source code that came with the previous article, then change the Client2.cs, not through the user input, but using a for loop to send three consecutive requests in the past, which will make the request more time interval, the following is the key code:

string msg = "Welcome to TraceFact.Net!";

for (int i = 0; i <= 2; i++) {
    byte[] buffer = Encoding.Unicode.GetBytes(msg);     // 获得缓存
    try {
        streamToServer.Write(buffer, 0, buffer.Length); // 发往服务器
        Console.WriteLine("Sent: {0}", msg);
    } catch (Exception ex) {
        Console.WriteLine(ex.Message);
        break;
    }
}

Run the server, and then run the client, and you may see the result:

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.