Chila asp.net_ Multithreading _socket< 15 >

Source: Internet
Author: User
Tags get ip

I. ASP.

b/S Do website (dynamic), management system (OA, invoicing, etc.)

c/s--form Software

Why now seldom use C/s to do invoicing and other software

b/S deployment convenient C/s to install

Dynamic: Interacting with the server (IIS (software))

We use C # to write Web browsers do not know C #, then our C # language is executed by IIS

Second, the Commission

Is the pointer to the method

Pointer: Point to the method again in memory address

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

Then the call is to find the memory address of this method, then you are called this method, why say it is safe,

The pointer can point to any method, then if you point to the method of the system you write the data, then there is a problem,

So the Commission is like I'm going to prescribe that you can only invoke this function that you passed in to me,

Entrust chain

Third, file stream

Four, multi-threaded

Thread Thread=new thread (a delegate variable);

Thread.Start ();

1) method re-entry problem (synchronization of data)

When we have multiple threads calling a method, there is a synchronization problem with data chaos,

is when the first thread calls to execute this method because the CPU executes very fast the first thread has not finished this method second

A thread comes in and executes this method, then the data is out of sync, and the solution is to add a lock to this method.

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

Other threads can execute

Private voidButton1_Click (Objectsender, EventArgs e) {             //defines a delegate with no parameter and no return value, and invokes the constructor of the thread delegate parameter without a parameter.specifies a method to invoke by a delegate ThreadStart del=NewThreadStart (Shownum); //does not perform cross-thread checks, which by default is true,Textbox.checkforillegalcrossthreadcalls =false; //Create a new process and pass the delegate variable overThread thread =NewThread (del); //the system will set the process to a running statethread.             Start (); ThreadStart Del1=NewThreadStart (Shownum); Thread Thread1=NewThread (DEL1); Thread1.         Start (); }         voidShownum () {//Lock LocksLock( This)             {                  for(inti =0; I < -; i++)                 {                     intA =Convert.ToInt32 (TextBox1.Text); A++; TextBox1.Text=a.tostring (); }             }         }

2) Background Thread

Our UI thread is the foreground thread, and the thread we create ourselves is the foreground thread by default.

Then a process only ends when all the foreground threads are finished, for example, we create a form program,

Create a new thread inside

Our form thread has a UI thread by default, and when this new thread executes,

If you close the form (that is, end the UI thread), the program is not ended,

Because this new thread is created by default to the foreground thread, the program will not close until all foreground threads are finished.

Then there will be a situation, that is, when the user clicks on the form Close button just to close the form of the program or not end,

It will wait until this new thread finishes executing before it shuts down.

Defines a delegate that invokes a method through a delegate designation

ThreadStart start = new ThreadStart (Socktlisten);

Create a new thread

Thread thread = new thread (start);

Set as Background thread

Thread. IsBackground = true;

Textbox.checkforillegalcrossthreadcalls = false;

Thread. Start ();

--"solution

Set the thread we created as a background thread

Thread Thread=new thread (a delegate variable);

Thread. Isbackground=true;

Thread.Start ();

We recommend that the threads we create are set as background threads, and that the background thread will end automatically when all foreground processes are finished.

There won't be a situation like that.

3) thread calls a method with parameters

The constructor of the thread is one of two overloads with a delegate variable, and the signature of a delegate is no parameter with no return value.

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

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

Pass multiple values: by collection

Private voidButton2_Click (Objectsender, EventArgs e) {             //Prepare ParametersList<string> list =Newlist<string> () {"haha","hehe","Hey"}; //defines a thread with a constructor for a parameterizedthreadstart delegate variableThis delegate is a delegate with an object type argument parameterizedthreadstart par=NewParameterizedthreadstart (Func); Thread Thread=NewThread (PAR); //Pass the parameters through the Strat () methodthread.         Start (list); }         voidFunc (Objectobj) {List<string> List=obj aslist<string>; foreach(stringIteminchlist)             {MessageBox.Show (item); }         }

4) socket

1) browser and server software is communicated through the socket. (for the prime Minister two people by phone)

2) browser and server software to standardize the data format sent by the HTTP protocol syntax

(This HTTP protocol is like two people on the phone in Chinese, not a person in Chinese, a person in English,

So that two of people can not communicate.

http1.1 version before, is used by the end connection, that is, when the browser sends a request, the server is responsible for monitoring the socket

(listening sockets) generates a new socket (communication socket) with which to exchange data,

The communication socket then sends the data requested by the browser (the client) to the browser (this is the response) to the hard disk.

If the browser wants to request again, the connection will need to be established again.

The http1.1 version starts with a long connection, and the server receives a request to send a response message and keeps the connection for a while,

This current connection is used during this time when the browser needs to re-request data.

This connection will be disconnected if the daunting time is exceeded.

Such a benefit

--The server does not remain connected to the browser, making it inefficient.

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

Private voidBtnlisten_click (Objectsender, EventArgs e) {             //defines a delegate that invokes a method through a delegate designationThreadStart Start=NewThreadStart (Socktlisten); //Create a new threadThread Thread=NewThread (start); //set as Background threadthread. IsBackground=true; Textbox.checkforillegalcrossthreadcalls=false; Thread.             Start (); //Socktlisten ();        }         voidSocktlisten () {//first, the server needs to create a socket listener object, listening to the client's request//first parameter: version of IP//second parameter: Interactive data transfer in the form of a stream//the third parameter: protocol used: TCPSocket Socketlisten=Newsockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); //Get IP AddressIPAddress IP=Ipaddress.parse (Txtip.text); //communication nodes, including IP address and port numberIPEndPoint Ippoint=Newipendpoint (IP, Convert.ToInt32 (txtdunkou.text)); //Bind the Communication node (IP address, port number) to the socket responsible for listeningSocketlisten.             Bind (Ippoint); //Set the listening queue, set the socket to listen state, you can set the maximum number of listeners,This maximum number of listeners is set according to the server's machine performance,//If this number is set to 10, then the client's 10 requests are processed at one time, and many will be queued,after the previous 10 requests have been processed, the following socketlisten are processed in turn. Listen (Ten); Socket Clientsocket=Socketlisten.                 Accept (); Showtext ("Client Connection succeeded"); //request a separate new socket for each client to communicate the data        }         voidShowtext (stringstr) {Txtcontent.text=str; }

Chila asp.net_ Multithreading _socket< 15 >

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.