Talk C Chestnut Bar (149th: C language Instance--socket Communication III)

Source: Internet
Author: User

Ladies and gentlemen, crossing, the last time we were talking about the socket communication example, let's go on to this example. Gossip Hugh, words return to the positive. Let's talk C chestnuts together!

Crossing, we described the steps of the socket communication in the previous time, referring to the system calls that were used to implement these steps, and some crossing did not understand these system calls, and today we are on the strike to introduce these system calls.

intsocket(intintint protocol)
    • This function is used to create a socket;
    • The three parameters of the function correspond to the three properties of the socket: domain, type, and protocol;
    • The function returns a socket descriptor when it runs successfully, and returns 1 if the run fails;

The header file to be included with this function is: The #include <sys/socket.h> file contains the system calls related to the socket communication, and the system calls we described in this chapter need to include the header file. In addition, the third parameter of the function is typically 0, which means that the default communication protocol is used.

int connect(intconststruct sockaddr *addr,socklen_t addrlen)
    • This function is used to establish a connection between the client and the server side;
    • The first parameter of the function is the socket descriptor of the client;
    • The second parameter of the function is the server-side address, and the address contains detailed server information;
    • The third parameter of the function is the length of the server-side address, which is the length of the second parameter;
    • This function returns 0 when it runs successfully, and returns 1 if the run fails;
int bind(intconststruct sockaddr *addr,socklen_t addrlen)
    • This function is used to bind the server-side socket with the server's address;
    • The first parameter of the function is a server-side socket descriptor;
    • The second parameter of the function is the address of the server, and the address contains detailed server information;
    • The third parameter of the function is the length of the server-side address, which is the length of the second parameter;
    • This function returns 0 when it runs successfully, and returns 1 if the run fails;
intlisten(intint backlog)
    • This function is used to create a socket queue on the server side, so that the server listens for communication connection requests from clients;
    • The first parameter of the function is a server-side socket descriptor;
    • The second parameter of the function is the length of the socket queue, that is, the number of communication connection requests that the server is allowed to accept;
    • This function returns 0 when it runs successfully, and returns 1 if the run fails;

The number of communication connection requests that the server allows to accept is an important value and cannot be too large. Set this value to refer to the server's load capacity and the server's processing power, or it will cause the server to crash. The server is also essentially a computer, but the performance is better than our daily use of the computer, if you open hundreds of programs on your own computer, the computer will be slow like a snail, or even crash. Similarly, if there are too many connections open on the server, the server crashes. I believe it is easy for us to understand, so we will not say much.

intaccept(int*addr*addrlen)
    • This function can let the server side accept the connection request from the client, let the server side and the client establish the connection;
    • The first parameter of the function is a server-side socket descriptor;
    • The second parameter of the function is the address of the client, not the address of the server;
    • The third parameter of the function is the length of the client address, which is the length of the address pointed to by the second parameter;
    • The function returns a socket descriptor when it runs successfully, and returns 1 if the run fails;

For the function to do the following instructions, it is considered to be some considerations:

    • The last two parameters of the function are pointers, the contents of which are null before the function is called, and the address of the client and its length are obtained after the function is called. The pointer region needs to accommodate the client address, and if you do not want to know the address of the client, it is possible to use a null pointer.
    • The function creates a new socket whose descriptor is the socket descriptor it returns, and the type of the new socket descriptor is the same as the type of the server-side socket descriptor. We communicate with the client through this socket, and the server-side original socket, which is the socket represented by the first parameter in the function, continues to be listening, ready to accept other requests from the client.
    • This function has blocking capability, and if there is no connection request in the socket queue created by listen, it will block until a new connection request is made in the queue.
intshutdown(intint how)
    • This function is used to close the communication connection established using sockets;
    • The first parameter of the function is a socket descriptor, which closes the communication connection associated with it;
    • The second parameter of the function is the way to close the connection, please refer to the following introduction;
    • This function returns 0 when it runs successfully, returns 1 when the run fails, and sets the errno global variable;

Recall that when we introduced the pipeline in front of the Zhanghuizhong, when using the pipeline for interprocess communication, the pipeline is unidirectional, either side of the pipe can read or write only, if you want to read and write at the same time on either side, then you need to build a pipeline. However, the communication connection established using sockets is bidirectional, which means that we can read and write to either the client or the server side. Using the close function closes both ends of the socket communication connection, and sometimes we just need to close one end of it, and this function comes in handy. We control the direction of the closure by the second parameter of the function, which can be one of the following three types:

    • SHUT_RD (indicates the read direction of closing the connection, while the other end of the connection cannot write data through the connection)
    • SHUT_WR (indicates the write direction of closing the connection, while the other end of the connection cannot read the data through the connection)
    • Shut_rdwr (indicates that the read and write direction of the connection is closed, while the other end of the connection cannot write data and read data through the connection)

This function is more flexible than close in operation and can be seen as an extension of the close function.

You crossing, for example of socket communication we are here. I want to know what the following example, and listen to tell.

Talk C Chestnut Bar (149th: C language Instance--socket Communication III)

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.