Design of repetitive server communication software for TCP/IP network

Source: Internet
Author: User
Tags message queue set socket socket port number advantage

This paper introduces a new design method of repetitive server communication software based on message queue, which is different from the same hairstyle server and the common repetitive server communication software, this new software has the advantage of fewer sub processes, and it is easy to manage the connection between client and server. It can improve the efficiency of the server effectively because of the large number of clients and the random data communication.

The difference between a concurrent server and a duplicate server

General TCP/IP server communication software is concurrent, that is, a daemon is responsible for listening to the client's connection requests, and then by the daemon to generate one or more child processes and the client specifically establish a connection to complete the communication, the disadvantage is that as the number of connected clients increased, The number of communication subprocess generated will be more and more, in the application of a large number of clients is bound to affect the server's operational efficiency. A generic duplicate server refers to a server that connects to a client after it has received a connection request. Then, after processing the communication task with the client to receive another client's request connection, the advantage is that the communication subprocess does not have to be generated, the disadvantage is that the client has to establish a connection with the server before each communication, the cost is too large , and can not be used for random data communication and busy business processing.

This paper presents a new type of duplicate server, which is different from the common duplicate server. It rejects the shortcomings of the two types of servers combined with its advantages, the server communication software has the characteristics of a general duplicate server but can handle the random access of clients, in the number of clients and business busy applications will play its advantages. Duplicate server communication software completes the connection with all clients in only three processes and always maintains these connections.

The method of establishing a connection between the repetitive server communication software and the client

Basic ideas

When the first client requests a connection to the server, the server's daemon establishes an initial connection (L0), and the client uses L0 to send two port numbers to the server, which registers the client's IP address and port number in the shared memory record, and then closes the L0. After the two communication subprocess generated by the daemon obtains the client IP address and the port number from shared memory, request a connection to the client, establish a read-from-Client connection (L1), and a connection to the client (L2), and record the handle of two connected sockets in shared memory. When another client requests a connection, the daemon no longer generates the communication child process, but also registers the client IP address and the port number in shared memory. The communication subprocess queries the shared memory for new records and, if so, establishes a connection with the client. It then polls the read sockets of all established connections to see if the data is readable, reads the data, and indicates that the data was obtained from a read socket on which record in shared memory. Another communication subprocess obtains the corresponding write socket from the shared memory according to the number of the record, and finally writes the result data to the client for that socket. 2.2 Establishing the connection

The initial process of the ⑴ server communication software first establishes a socket on the public port, establishes a listening queue on the socket, generates a daemon (Daemon) tcp_s, and then exits the initial process. The daemon blocks at function accept until there is a connection request from the client, a connection request calls the server function processing, and then continues to wait for another client's request. Because TCP/IP after the connection is removed in order to avoid duplication of the phenomenon, is generally put the connection in the outdated connection table, the connection after the removal to avoid in the TIME_WAIT state (outdated connection), you can call setsockopt set socket linger delay flag, The delay time is also set to 0. The server registers a globally recognized public port number in the/etc/services file: Tcp_server 2000/tcp.

suct servent *sp;
suct sockaddr_in peeraddr_in,myaddr_in;
linkf=0;
sp=getservbyname("tcp_server","tcp");
ls=socket(AF_INET,SOCK_STREAM,0); /* 创建监听套接字 */
myaddr_in.sin_addr.s_addr=INADDR_ANY;
myaddr_in.sin_port=sp->s_port; /* 公用端口号 */
bind(ls,&myaddr_in,sizeof(suct sockaddr_in));
listen(ls,5);
qid3=msgget(MSGKEY3,0x1ff); /* 获得消息队列的标志号 */
qid4=msgget(MSGKEY4,0x1ff);
signal(SIGCLD,SIG_IGN); /* 避免子进程在退出后变为僵死进程 */
addrlen=sizeof(suct sockaddr_in);
lingerlen=sizeof(suct linger);
linger.l_onoff=1;
linger.l_linger=0;
setpgrp();
switch(fork()){ /* 生成Daemon */
case -1:exit(1);
case 0: /* Daemon */
for(;;){
s=accept(ls,&peeraddr_in,&addrlen);
setsockopt(s,SOL_SOCKET,SO_LINGER,&linger,lingerlen);
server();
close(s);
}
default:
fprintf(serr,"初始进程退出,由守护进程监听客户机的连接请求.\n");
}

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.