network communication between Linux processes

Source: Internet
Author: User

First, how is the process of network communication? The process of socket communication?

There are many ways to communicate between different processes on the same machine, primarily by using message passing or shared memory. While the process across the network is almost always using socket communication, such as Web server, QQ.

The socket is a special kind of file, the operating system provides some socket function is the operation of it (read/write Io, open, close), inter-process communication is done by reading and writing their sockets.

The process of communication

Server

    1. Create a specified type and protocol socket using the socket () system call
    2. Use the bind () system call to name the socket you created, which is the usual server address (IP address + port number), such as the server's 80 port
    3. Use the Listen () system call to listen for connections from the client.
    4. Use the Accept () system call to accept the connection from the client, which is blocked until the client has a connection.
    5. Read and write data (communication) to the socket established by the connection

  Note: Server initially creates a socktet, and after receiving a connection request (accept ()) creates a socket that is different from the original named socket. This new socket only communicates with this particular client, and the named socket remains to process the connection from the client.

Client

    1. Use the socket () system call to create a socket of the specified type and protocol.
    2. Connect the socket created in 1 to the address of the server using Connect ().
    3. Using system calls to send and receive data, the simplest is the read () and write () functions.
Ii. Introduction to key knowledge points and system invocations

1. int socket(int domain, int type, int protocol); To create a socket of the specified type, two processes can communicate and must use sockets of the same domain and type .

    • Domain: There are mainly af_inet,af_init6, respectively, representing IPV4, IPv6 domain
    • type:sock_stream represents an ordered, reliable, bidirectional, connection-oriented byte stream; The Sock_dgram represents a fixed, non-connected, unreliable message delivery.
    • Protocol: Generally 0, the system will be based on the previous domain name and type, select the appropriate protocol such as TCP, UDP protocol.

2. int bind(int sockfd, const struct SOCKADDR *addr, socklen_t addrlen);

    • SOCKFD: The socket descriptor returned when creating Scoket. Similar to file description symbols.
    • Addr:socket-bound Address
    • Addrlen: The second argument is a pointer, and the third argument is the length

Address format:

struct SOCKADDR_IN {

    sa_family_t    sin_family;/* Address family:af_inet */    in_port_t      sin_port;   /* port in Network byte order */    struct in_addr sin_addr;   /* Internet address */};

Note: Usually when the server is started, it binds a well-known address (such as IP address + port number), which is used to provide the service, the client can connect to the server, and the clients do not have to specify that the system automatically assigns a port number and its own IP address combination.

This is why the usual server-side is called before listen bind (), and the client is not called, but is randomly generated by the system at Connect ().

3. Other

    • int listen (int sockfd, int backlog); A descriptor for the listener that represents the maximum number of connections
    • int connect (int sockfd, const struct SOCKADDR *addr, socklen_t addrlen);//Address of Client Connection server
    • int accept (int sockfd, struct sockaddr *addr, socklen_t *addrlen); Server accepts connections
    • Read ()/write ()
    • Recv ()/send ()
    • Readv ()/writev ()
    • Recvmsg ()/sendmsg ()
    • Recvfrom ()/sendto ()

  

4. precautions3.socktet Communication Example

Reference:

network communication between Linux processes

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.