Two cases __net of synchronous socket for NET socket development

Source: Internet
Author: User

from the Wild Animal bloghttp://www.cnblogs.com/wzd24/Two examples of synchronous socket implementations for. NET socket development today, let's talk about the application of synchronous sockets in the development of. NET network applications, many people think that the service socket of the network application should not use synchronous socket. Yes, in most cases this is true, but there are also scenarios where we might get more results using a synchronous socket. We can consider using synchronous sockets for the following two scenarios. One, the number of clients is less: the number is less than the number of simultaneous connections to the server is generally 50 people below. In this case, we can consider using synchronous Socket+thread to implement our service side. This will allow us to write logically clearer code and performance will not degrade too much. Second, a large number of clients but are short connection: Short connection refers to the client's connection after processing a send and receive after the production is disconnected scene, such as the HTTP protocol is a short connection. HTTP creates a socket connection when the client makes a request and sends a URL request through the socket, and the server disconnects the connection after processing the request and sending back the appropriate page. In this scenario we can also use synchronous sockets to achieve our needs. So what if the two requirements I mentioned above should be fulfilled? For both of these requirements, I will implement them in different scenarios. First of all, let's take a look at the first requirement, which I use Socket+thread to implement, the basic process is as follows: first create a socket, and bind it to a endpoint and start listening. Next we create a thread in which we use an infinite loop to receive the connection request from the client. After a request is received, a new thread is created for the client, and in this thread an infinite loop is also used to receive data from this client. Let's take a look at the code: first we create a socket to listen to the client's connection: Socket listener = new socket (addressfamily.internetwork, SocketType.Stream, prot OCOLTYPE.TCP);
IPEndPoint locep= New IPEndPoint (Ipaddress.any, 2000);
Listener. Bind (LOCEP);
Listener. Listen (100); Then create a thread to handle the client's connection request: Thread acceptthread = new Thread (new ThreadStart (Acceptworkthread));
Acceptthread.start ();

private void Acceptworkthread ()
... {
Thread.CurrentThread.IsBackground = true;
while (true)
... {
Socket Accept = listener. Accept ();
IPEndPoint Remoep = (ipendpoint) accept. Remoteendpoint;
String recstring = "received a connection from + remoEP.Address.ToString () +". ";
This. Invoke New Addlistitemhandler (this. addListItem), new string[] ... {recstring});
Thread receivethread = new Thread (new Parameterizedthreadstart (Receiveworkthread));
Receivethread.start (accept);
}
Finally, let's look at how to receive data: private void Receiveworkthread (Object obj)
... {
Thread.CurrentThread.IsBackground = true;
Socket socket = (socket) obj;
byte[] buffer = new byte[1024];
while (true)
... {
int receivecount = socket. Receive (buffer);
if (Receivecount > 0)
... {
IPEndPoint Remoep = (ipendpoint) socket. Remoteendpoint;
String recstring = "from client" + remoEP.Address.ToString () + "message:" + Encoding.Default.GetString (buffer, 0, receivecount);
This. Invoke New Addlistitemhandler (this. addListItem), new string[] ... {recstring});
Socket. Send (buffer, receivecount, socketflags.none);
}
Else
... {
Socket. Close ();
Break
}
}
Well, the whole implementation is done. Now let's look at the second requirement: This scenario will be implemented in another way, and why not use the previous approach to implement it. Let's analyze it. We know that in the previous implementation, each access to a client will create a thread, if there is a large number of client access, will create too many threads. But if there are too many threads, Windows needs more CPU time to switch the thread's context (which is why the previous implementation cannot access many clients). We know that in this scenario, each connection is a short connection. And the order is fixed. are: Access-> receive-> send in such order, then we can complete the whole process in one method. In this way, we can use the thread pool to achieve what we need. OK, let's use code to talk: first we create a socket to listen to the client's connection: Socket listener = new socket (addressfamily.internetwork, SocketType.Stream, P ROTOCOLTYPE.TCP);
IPEndPoint locep= New IPEndPoint (Ipaddress.any, 2000);
Listener. Bind (LOCEP);
Listener. Listen (100); Next we want to create a thread pool: thread[] clientthreadlist = new THREAD[30];
foreach (Thread th in clientthreadlist)
... {
th = new Thread (new ThreadStart (Clientworkthread));
Th. Start ();
Finally, let's take a look at what the threads are going to do:

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.