C # socket Overview

Source: Internet
Author: User
I. Summary

Using C # for network communication recently, I learned how C # implements network communication, especially the use of Socket socket.ArticleSummarize the use of sockets and the C # implementations of common transmission protocols TCP and UDP. This blog post only explains and distinguishes the terms involved in socket.

 

Ii. Experimental Platform

Visual Studio 2010

 

Iii. Experiment Principles

The most common C/S mode is used as an example. First, the server has a process (or multiple processes) waiting for the client to connect to the specified port.ProgramWait for the customer's connection information. Once connected, the data can be transmitted according to the designed data exchange method and format.

During socket programming, we often encounter terms such as synchronous, asynchronous, blocking, and non-blocking. Let's take a look at them. In addition, it should be noted that these concepts are not limited to socktet.

3.1 synchronous and asynchronous

(1) synchronization can be understood as waiting for the system return value or message after a function or method is executed. At this time, the program is in the waiting state, execute other commands only after receiving the returned value or message.

(2) asynchronous: After a function or method is executed, you do not have to wait for the return value or message in an obstructive manner. Instead, you only need to delegate an asynchronous process to the system. When the system receives the return value or message, the system automatically triggers the asynchronous process of delegation to complete a complete process.

The following is an example of an image comparison:

Imagine that you are a PE Instructor and need to test the 100-meter score of 400 students. Of course, you won't let 100 students start together, because when the students return to the end, you won't be able to record their scores. If you start a student every time and wait for him to return to the end, write down the score and then let the next student start until all the students finish running. Congratulations, you have mastered the synchronous blocking mode.

You have designed a function. The input parameter is the student ID and start time, and the return value is the time at the end. You can call this function 100 times to complete this test. This function is synchronized, because you can get the result if you call it. This function is also blocked, because once you call it, you must wait until it gives you the result, you cannot do other things.

If you start a student every 10 seconds until all the students start, and each student on the other side returns to the end, the score is recorded until all the students finish. Congratulations, you have mastered the asynchronous non-blocking mode.

You have designed two functions, one of which records the start time and student number. You will call this function 100 times. The other function records the arrival time and student number, this function is an event-driven callback function. When a student reaches the endpoint, you call it passively. The function you call is asynchronous, because you call it, it does not tell you the result; this function is also non-blocking, because once you call it, it will return immediately, you can call it again without waiting. However, if you only call this function for 100 times and you have not completed your test task, you still need to wait for 100 times to call another function.

Of course, you will immediately realize that the efficiency of the synchronous blocking mode is significantly lower than that of the asynchronous non-blocking mode. So who else will use the synchronous blocking mode?

Yes, the asynchronous mode is highly efficient, but it is more troublesome. You need to record the data of the Start student while recording the data of the arrival student, in addition, the order in which students return to the destination is not the same as the starting order, so you have to keep searching for student numbers in your transcript. In the midst of chaos, you will often wear Zhang guanli.

You may come up with a smarter way: you bring a lot of stopwatches and let students group and test each other. Congratulations! You have mastered the multithread synchronization mode!

Every person with a stopwatch can independently call your synchronization function, which is neither prone to errors nor highly efficient. As long as there are enough stopwatches, the synchronization efficiency can reach or even exceed Asynchronization.

As you can understand, the question may be: is there a need for asynchronous mode since multi-thread synchronization is fast and good?

Unfortunately, the asynchronous mode is still very important, because in many cases, you cannot get a lot of stopwatches. The peer system you need to communicate with may only allow you to establish a socket connection. Many large business systems in the Financial and telecom industries have such requirements.

The above is the difference between synchronization and Asynchronization in socket programming.

3.2 blocking and non-blocking

(1) blocking call means that the current thread will be suspended before the call result is returned. The function is returned only after the result is obtained.

(2) The concept of non-blocking and blocking corresponds to that the function does not block the current thread and returns immediately before the result is immediately obtained.

3.3 differences between synchronization and blocking

Some people may equate blocking with synchronization. In fact, they are different. Synchronous Asynchronization and blocking and non-blocking are two different concepts. synchronous Asynchronization refers to the communication mode, blocking and non-blocking refer to whether to wait for the action to complete before returning the message during receiving and sending. Therefore, these four words cannot be obfuscated.

 

 

 

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.