Source code for various TCP network servers in Linux

Source: Internet
Author: User
The source code of various TCP network servers in Linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. Everyone knows the compiling steps of various network server programs and knows that there are two types of network servers: cyclic services and concurrent services. Here is a summary of the source code.

First of all, the steps for Loop Network Server programming are as follows:


(400) {this. resized = true; this. width = 400; this. alt = 'click here to open new window';} "onmouseover =" if (this. resized) this. style. cursor = 'hand'; "onclick =" window. open ('HTTP: // zhoulifa.bokee.com/inc/directsocket.png'); ">
This type of server model is a typical cyclic service. Without the multi-process/thread technology, the Service throughput is limited, if the data of the previous connection service is not sent and received, the subsequent Connections cannot be processed. Therefore, there is usually a multi-process technology that enables a new process to process a new connection, and the listening socket continues to listen.

************************ ********************
* Filename: source code for various TCP network servers in Linux
* Purpose: records the source code of various tcp service programs in Linux.
* Wrote by: zhoulifa (zhoulifa@163.com) Zhou Lifa (http://zhoulifa.bokee.com)
Linux enthusiasts Linux knowledge disseminators sohowhose developers are best at C Language
* Date time: 2006-07-04 22:00:00
* Note: Anyone can copy the code and use these documents, including your commercial use.
* But follow the GPL
* Hope: more and more people are expected to contribute to the development of science and technology.
**************************************** *****************************/

The source code of a loop TCP Service (because fork is used for multi-process services, this service is also useful in reality) as follows:
CODE:/* ---------------------- source CODE start --------------------------------------------*/
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
/*************************************** ******************************
* Filename: cycletcpserver. c
* Purpose: a circular tcp server program
* Tidied by: zhoulifa (zhoulifa@163.com) Zhou Lifa (http://zhoulifa.bokee.com)
Linux enthusiasts Linux knowledge disseminators sohowhose developers are best at C Language
* Date time: 2006-07-04 22:00:00
* Note: Anyone can copy the code and use these documents, including your commercial use.
* But follow the GPL
* Thanks to: Google.com
**************************************** *****************************/
Int main (int argc, char ** argv)
{
Int sockfd, new_fd;/* listen to socket: sock_fd, data transmission socket: new_fd */
Struct sockaddr_in my_addr;/* local address information */
Struct sockaddr_in their_addr;/* customer address information */
Unsigned int sin_size, myport, lisnum;

If (argv [1]) myport = atoi (argv [1]);
Else myport = 7838;

If (argv [2]) lisnum = atoi (argv [2]);
Else lisnum = 2;

If (sockfd = socket (PF_INET, SOCK_STREAM, 0) =-1 ){
Perror ("socket ");
Exit (1 );
}
My_addr.sin_family = PF_INET;
My_addr.sin_port = htons (myport );
My_addr.sin_addr.s_addr = INADDR_ANY;
Bzero (& (my_addr.sin_zero), 0 );
If (bind (sockfd, (struct sockaddr *) & my_addr, sizeof (struct sockaddr) =-1 ){
Perror ("bind ");
Exit (1 );
}

If (listen (sockfd, lisnum) =-1 ){
Perror ("listen ");
Exit (1 );
}
While (1 ){
Sin_size = sizeof (struct sockaddr_in );
If (new_fd = accept (sockfd, (struct sockaddr *) & their_addr, & sin_size) =-1 ){
Perror ("accept ");
Continue;
}
Printf ("server: got connection from % s \ n", inet_ntoa (their_addr.sin_addr ));
If (! Fork () {/* child process code segment */
If (send (new_fd, "Hello, world! \ N ", 14, 0) =-1 ){
Perror ("send ");
Close (new_fd );
Exit (0 );
}
}
Close (new_fd);/* the socket is no longer required by the parent process */
Waitpid (-1, NULL, WNOHANG);/* wait until the child process ends and clear the resources occupied by the child process */
}
}
/* ---------------------- Source code end --------------------------------------------*/
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.