Several days ago, ASP. NET _ multithreading _ Socket & lt; 15 & gt;, asp.net _ socket

Source: Internet
Author: User

Several days ago, ASP. NET _ multithreading _ Socket <15>, asp.net _ socket

I. ASP. NET

B/S is a website (dynamic) and Management System (OA, invoicing, etc)

C/S -- form software

Why do I rarely use C/S for invoicing and other software?

B/S deployment convenience C/S Installation

Dynamic: interaction with the Server (IIS (software)

If we use C # To write a website browser, we do not know C #, so our C # language is executed by IIS.

Ii. Delegation

Is the method pointer

Pointer: point to the address in the Method Memory.

Security: The delegate encapsulates the address of a method, as if a pointer points to the address of this method in the memory,

So the memory address of this method is found during the call, so you are calling this method. Why is it safe,

The pointer can point to any method. If it points to the system method, you may encounter a problem when writing data,

So the delegate is like I have stipulated that you can only call the function you passed in to me,

Delegated chain

3. file stream

4. Multithreading

Thread thread = new Thread (a delegate variable );

Thread. start ();

1) method reconnection (Data Synchronization)

When multiple threads call a method, there will be a synchronization problem with data confusion,

That is, when the first thread calls this method, because the CPU executes very fast, the first thread has not finished executing this method.

When threads come in and execute this method again, the data will not be synchronized at this time. The solution is to add a lock to this method.

After the lock, the method is locked after the first thread comes in. The lock is released only when the thread finishes executing the method,

Other threads can execute

Private void button#click (object sender, EventArgs e) {// define a delegate without parameters and return values. Call the constructor without parameters of the Thread delegate parameter, specify the method ThreadStart del = new ThreadStart (ShowNum) to be called by Delegate; // do not perform cross-Thread Check. The default value is true, TextBox. checkforillegalcrossthreadcils = false; // create a new process and pass the delegate variable to Thread thread = new Thread (del); // The system sets the process to run. start (); ThreadStart del1 = new ThreadStart (ShowNum); Thread thread1 = new Thread (del1); thread1.Start () ;}void ShowNum () {// lock (this) {for (int I = 0; I <2000; I ++) {int a = Convert. toInt32 (textBox1.Text); a ++; textBox1.Text =. toString ();}}}

2) Background thread

Our UI thread is the foreground thread, and the threads we create are the foreground threads by default,

A process ends only after all foreground threads are completed. For example, we create a form program,

Create a new thread in it

Our form thread has a UI thread by default. When this new thread is being executed,

If the form is closed (that is, the UI thread is ended), the program will not be ended,

Because this new thread is created by default as the foreground thread, the program will be closed only after all foreground threads are completed,

In this case, when you click the close button of the form to close the program,

It will be closed only after the new thread is executed.

// Define a delegate and call a method through the delegate

ThreadStart start = new ThreadStart (SocktListen );

// Create a new thread

Thread thread = new Thread (start );

// Set it to a background thread

Thread. IsBackground = true;

TextBox. checkforillegalcrossthreadcils = false;

Thread. Start ();

-- Solution

Set the thread we created to a background thread.

Thread thread = new Thread (a delegate variable );

Thread. IsBackground = true;

Thread. start ();

We recommend that you set all created threads as background threads. When all foreground processes are finished, background threads will automatically end,

This will not happen.

3) The thread calls a method with parameters.

The constructor of Thread contains two overload variables with delegate variables. The signature of a delegate has no return value and no parameter,

One is a parameter with no return value and an object type,

Then we can pass multiple values through a set of Start () methods, thread. Start (list );

Pass multiple values: pass through the set

Private void button2_Click (object sender, EventArgs e) {// prepare the parameter List <string> list = new List <string> () {"Haha", "Haha ", ""}; // defines a Thread constructor with ParameterizedThreadStart delegate variable. This delegate is a delegate with an object-type parameter ParameterizedThreadStart par = new ParameterizedThreadStart (Func ); thread thread = new Thread (par); // pass the parameter to thread through Strat. start (list);} void Func (object obj) {List <string> list = obj as List <string>; foreach (string item in list) {MessageBox. show (item );}}

4) socket

1) the browser and server software communicate through Socket. (Contact the Prime Minister by phone)

2) browsers and server software regulate the format of data sent through the http protocol syntax

(This http protocol is like two people talking in Chinese on the phone. One person cannot speak in Chinese and the other is in English,

In this way, two people cannot communicate)

Before http1.1, the client connection is used, that is, when the browser sends a request, the server is responsible for listening to the Socket

(Listener Socket) will generate a new Socket (Communication Socket) for data exchange with it,

Then the communication socket will send the data requested by the browser (client) to the browser (this is the response) in the hard disk ),

The connection will be closed immediately. If the browser wants to request another request, the connection needs to be established.

Http1.1 starts to use persistent connections. After the server receives a request and sends a response message, it will keep the connection for a period of time,

During this period, the current connection will be used when the browser needs to request data again,

If this daunting time is exceeded, the connection will be disconnected.

Benefits

-- The server will not keep connected to the browser, making the efficiency very low.

-- "When the server needs to request data again, it can use the current connection and will be automatically disconnected after a period of time

Private void btnListen_Click (object sender, EventArgs e) {// define a delegate and call a method ThreadStart start = new ThreadStart (SocktListen) through the delegate ); // create a new Thread thread Thread = new thread (start); // set it to a background Thread thread. isBackground = true; TextBox. checkforillegalcrossthreadcils = false; thread. start (); // SocktListen ();} void SocktListen () {// first, the server needs to create a Socket listener object to listen to client requests // The first parameter: IP version // second parameter: Interactive Data Transmission in the form of a stream // third parameter: protocol used: TCP Socket socketlisten = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // obtain the ip address IPAddress ip = IPAddress. parse (txtIP. text); // communication node, including ip address and port number IPEndPoint ippoint = new IPEndPoint (ip, Convert. toInt32 (txtDunkou. text); // bind the Communication Node (IP address and port number) to socketlisten on the Socket responsible for listening. bind (ippoint); // set the listening queue. Set this socket to the listening status. You can set the maximum number of listeners. the maximum number of listeners is based on the server performance, // if this number is set to 10, 10 requests from the client will be queued for processing at a time. After the first 10 requests are processed, the socketlisten will be processed in sequence. listen (10); Socket clientsocket = socketlisten. accept (); ShowText ("client connection successful"); // send a new Socket to each client for data exchange} void ShowText (string str) {txtContent. text = str ;}

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.