How to create and use a socket link pool

Source: Internet
Author: User
Tags bit set exception handling socket advantage

The use of CS procedures will inevitably encounter socket connection problems, many times, the use of the programming language with the socket library, the use of some of the unused, although the system with the library in a lot of exception handling, stability and a lot of effort, but to understand and use those libraries, For example, do socket connection pool to go a lot of detours. I'm here to discuss how to create and use the socket link pool.

The general socket link has the following two ways: long (regular) links and short links.

Long link: When the data is sent, the socket link continues to open. Until the exception or the program exits, the advantage of this approach is that you do not have to initiate a connection disconnect each time, the speed can be faster than the short connection, but relatively to the server's resource pressure is greater. Long links with a wide range, such as game system, QQ, etc., long (often) links generally also need to ping data to the server, to ensure that the socket link unblocked. When you ping the server, you need to reopen the link.

Short link: When the data is sent over, the active disconnect, each send data to link, disconnect operation, the advantage of this way is: the resources of the server is relatively small, but because each time to relink, speed overhead is also relatively large, This approach is appropriate for a situation that does not require frequent interaction with the server.

The above two methods are very large in the case of a large number of deficiencies, so we consider a compromise approach, that is, the use of socket connection pool.

At the beginning of the program, a number of long links are created. Give them an identity bit that indicates whether the link is idle. When you need to send the data, the system assigns it a currently idle link. At the same time, the resulting link is set to "busy", when the data sent over, the link identification bit set to "free", so that the system can be assigned to the next user, so that the advantages of both ways to fully play out. Hangzhou with the purchase of Network Technology Co., Ltd. under the purchase of independent Shopping Network (http://www.shopxg.com) system is used in this way. When you have enough users, you only need to dynamically increase the number of link pools.

Below we use the specific procedure to explain below:

First we declare a socket class:

public class XieGouSocket
{
public Socket m_socket; //Socket对象
public bool m_isFree; //判断是否空闲
public int m_index; //在链接缓存池中的索引值
}

The following function is to create the socket link pool, where I have removed all exception handling parts in order to make the code clearer.

public XieGouSocket[] m_socket; //先定义个缓冲池
public void CreateSocketPool()
{
string ip= “127.0.0.1”;
string port= 2003;
IPAddress serverIp=IPAddress.Parse(ip);
int serverPort=Convert.ToInt32(port);
IPEndPoint iep=new IPEndPoint(serverIp,serverPort);
m_socket = new XieGouSocket[200];
for(int i =0; i < 200 ; i ++)
{
m_socket[i] = new XieGouSocket();
m_socket[i].m_index = i ;
m_socket[i].m_isFree = true;
m_socket[i].m_socket =new Socket (AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
m_socket[i].m_socket.SetSocketOption (SocketOptionLevel.Socket,SocketOptionName.SendTimeout,1000);
m_socket[i].m_socket.Connect(iep);
}
}

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.