How to write the client/server software under Linux

Source: Internet
Author: User
Tags ftp socket linux

Linux is known for its source code, and its stability and reliability in the field of the operating system, the network application technology is more widely used. It has been one of Windows ' most important rivals for a long time. With the advent of the Internet era, this advantage of Linux has become more prominent. This article will discuss how to use the socket to implement client/server communication in Linux environment.

With the development of network technology, the network structure has been developed from the past host/terminal type and the Peer-to-peer type to the client/server type which is now widely used. The client/server model is widely used, and www,e-mail,ftp on the Internet is based on this model. In connection-oriented communication mode, the server opens the listening port, listens for connection requests made to the server by other clients on the network, establishes a connection to the client when a request signal is received, and then communicates interactively. Specific steps can be organized in this way:

Server:

1. Open a known listening port, such as SMTP 25, POP3 110, FTP 21, Telnet to 23, and so on.

2. Monitor the client's connection request on the listening port and establish a connection line if a client requests a connection.

3. Communicating with the client on the connecting line.

4. Close the connection line after the communication completes and continue to listen for the client's connection request.

Client:

1. Send a connection request to the specified server host and port.

2. When the server establishes the connection line, communicates with the server.

3. Close the connecting line after the communication is complete.

Many of the features of Linux are very useful for Web programming: First, Linux has posix.1 standard library functions, socket (), bind (), listen () These library functions can easily implement the server/client model, Instructions for using these library functions are described in the following sections. Second, Linux process management is also very consistent with the working principle of the server, the so-called process is the program in the Run-time state, you can say that the process is a dynamic program. In a computer running a Linux operating system, each process has a parent process that creates it, and it can create multiple child processes. On the server side we can use the parent process to listen to the client's connection request, and when there is a client connection request, the parent process creates a subprocess to establish the connection line and communicates with the client, and it can continue to listen to the other client's connection requests. This avoids the problem that the server will no longer be able to communicate with other clients after a client has established a connection to the server. Another feature of Linux is that it adheres to the outstanding feature of UNIX device independence, which is that it implements a unified device interface through a file descriptor, using unified I/O calls to disk, display terminals, audio devices, print devices, and even network traffic. These three features will make network programming in Linux a breeze. The comprehensive utilization of the above three features will be the essence of this article. The following client/server implementation process can be described as one or two, noting the differences from the steps described above.

Server:

1. Open a known listening port.

2. Monitor the client's connection request on the listening port, establish a connection line and return the communication file descriptor when a client requests a connection.

4. The parent process creates a child process that closes the communication file descriptor and continues to listen for client connection requests on the port.

3. The subprocess communicates with the client through the communication file descriptor, terminates the child process and closes the communication file descriptor after the communication ends.

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.