In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Linux network programming how to use multiple processes to achieve server concurrent access, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
A classic example of using a multi-process approach to achieve concurrent access to the server.
The program realizes the function:
1. The client reads a line of text from standard input and sends it to the server.
two。 After receiving the text from the client, the server returns it to the client as is.
3. The client receives the text from the server, outputs it to standard output, and then continues the above steps.
Server-side process: after establishing the listening socket, wait for the connection of the client, after receiving a connection, create a child process to communicate with the client, and the main process continues to wait for the connection of other clients. The code is as follows:
# include
# include
# include
# include
# include
# include
# include
# include
# include
# define SERV_PORT 1113
# define LISTENQ 32
# define MAXLINE 1024
/ * connection handler function * /
Void str_echo (int fd)
Int
Main (int argc, char * argv []) {
Int listenfd,connfd
Pid_t childpid
Socklen_t clilen
Struct sockaddr_in servaddr
Struct sockaddr_in cliaddr
If ((listenfd = socket (AF_INET, SOCK_STREAM,0)) =-1) {
Fprintf (stderr, "Socket error:%s\ n\ a", strerror (errno))
Exit (1)
}
/ * the server side is populated with sockaddr structure * /
Bzero (& servaddr, sizeof (servaddr))
Servaddr.sin_family = AF_INET
Servaddr.sin_addr.s_addr = htonl (INADDR_ANY)
Servaddr.sin_port = htons (SERV_PORT)
/ * bind listenfd descriptor * /
If (bind (listenfd, (struct sockaddr*) (& servaddr), sizeof (struct sockaddr)) =-1) {
Fprintf (stderr, "Bind error:%s\ n\ a", strerror (errno))
Exit (1)
}
/ * listen for listenfd descriptor * /
If (listen (listenfd,5) =-1) {
Fprintf (stderr, "Listen error:%s\ n\ a", strerror (errno))
Exit (1)
}
For (;;) {
Clilen = sizeof (cliaddr)
/ * the server blocks until the client establishes a connection * /
If ((connfd=accept (listenfd, (struct sockaddr*) (& cliaddr), & clilen)) =-1) {
Fprintf (stderr, "Accept error:%s\ n\ a", strerror (errno))
Exit (1)
}
/ / after a client has established a connection
If ((childpid = fork ()) = = 0) {/ * child process * /
Close (listenfd); / * turn off the listening socket * /
Str_echo (connfd); / * process the client's request * /
Exit (0)
}
Close (connfd); / * the parent process closes the connection socket and continues to wait for other connections to arrive * /
}
}
Void str_echo (int sockfd) {
Ssize_t n
Char buf [MAXLINE]
Again:
While ((n = read (sockfd, buf, MAXLINE)) > 0)
Write (sockfd, buf, n)
If (n < 0 & & errno = = EINTR) / / interrupted, reentrant
Goto again
Else if (n < 0) {/ / error
Fprintf (stderr, "read error:%s\ n\ a", strerror (errno))
Exit (1)
}
}
Client process: create a connection socket, actively initiate a connection request to the server, establish the connection, wait for the standard input, after the input is completed, send the input to the server, and then receive the content sent by the server. and output the received content to standard output. The code is as follows:
# include
# include
# include
# include
# include
# include
# include
# include
# include
# define SERV_PORT 1113
# define MAXLINE 1024
Void str_cli (FILE * fp, int sockfd)
Int
Main (int argc, char * * argv)
{
Int sockfd
Struct sockaddr_in servaddr
If (argc! = 2) {
Fprintf (stderr, "usage: tcpcli\ n\ a")
Exit (0)
}
If ((sockfd=socket (AF_INET,SOCK_STREAM,0) =-1) {
Fprintf (stderr, "Socket error:%s\ n\ a", strerror (errno))
Exit (1)
}
/ * the client program fills in the data of the server * /
Bzero (& servaddr,sizeof (servaddr))
Servaddr.sin_family=AF_INET
Servaddr.sin_port=htons (SERV_PORT)
If (inet_pton (AF_INET, argv [1], & servaddr.sin_addr)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.