Listening socket connection sockets

Source: Internet
Author: User

Summary: For the most important step in server programming to wait and accept the customer's connection, then this step in programming how to complete, the Accept function is to complete this step. It removes the established client connection from the kernel and then returns the established connection to the user program, where the user program can communicate to the customer at point-to-point.

The Accept function waits and accepts the customer request:

#include <sys/socket.h>
int accept (int sockfd, struct sockaddr* addr, socklen_t* len)
Return: Non-negative descriptive word--success, -1--failure

Accept defaults to block the process until a client connection is established, and it returns a newly available socket, which is a connection socket. At this point we need to differentiate between the two sockets, a socket as the parameter of accept SOCKFD, which is a listening socket, after calling the Listen function, a socket from the active connected socket becomes a listening socket, and accept return is a connection socket, It represents a point-in-point connection where a network already exists. The natural question is: why are there two types of sockets? The reason is simple, if you use a descriptive word, then it has too many functions, making it very intuitive to use, while the kernel does produce a new descriptive word like this.


Parameter SOCKFD
The parameter SOCKFD is the listener socket in the above explanation, which is used to listen on a port, and when a client connects to the server, it uses this port number, which is associated with the socket. Of course, the customer does not know the details of the socket, it only knows an address and a port number.
Parameter addr
This is a result parameter, it is used to accept a return value, this return value specifies the address of the client, of course, this address is described by an address structure, the user should know what the address structure. If you are not interested in the customer's address, you can set this value to NULL.
Parameter len
As you can see, it is also the parameter of the result, which is used to accept the size of the addr structure, which indicates the number of bytes that the addr structure occupies. Similarly, it can also be set to null.

If accept returns successfully, the server and the customer have established a connection correctly, and the server completes the communication with the customer through the socket returned by the accept socket.

This week the students in the network programming, encountered a listening socket problem, here is about to describe:

For example, my program opened a listening port, and after establishing a connection with the client, a new socket was generated. I then executed a statement that closed the listening port only, and found that the listening port and the established connection still exist. I have closed the listener socket, why can the client continue to send messages to the listening port? What the hell is this all about? What is the relationship between a new socket and a listening socket?

For example, you open a 80 listening port, there is a client connection you accept, then close the 80 port. But at this time the client sends the message must be sent to 80 fracture, but 80 has been closed Ah, but the communication is still normal. In fact, when I first contacted the socket, I also thought that all the data sent from the client would need to be forwarded by a listening socket to receive it. All beginners are apt to make this misunderstanding.

After a period of use, I now understand that the listening socket is a string to refer to the road, you are essentially talking to the person it refers to. Because the person you are looking for cannot be waiting for you at any time, and the listening socket is a professional waiting for you to ask, it answers the person you are looking for, and wakes up the person you are looking for, so the call is built up, just like the real life of the operator.

That is, after the connection is established, the client sends data to the server with the socket that makes the connection, which is sent to the server's newly created socket instead of the server's listening socket. The server's listener socket is always used only to accept connection requests.

This is like you go to dinner, the restaurant door has a welcome miss (listening SOCKET) to see you come and greet you, and then (ACCEPT) a new waiter to receive you (new SOCKET), and then keep listening to the door to continue to listen to the next. The Listening lady is gone, and the waiter who receives you is of course unaffected.

Speaking of which, it is necessary to say the accept () function. The following is the "Linux Network Programming" book, the sixth chapter Berkeley socket to the Accept () function Description:

The function accept () is somewhat difficult to understand. When it is called, the approximate process is the following:

Someone is trying to call connect () from far and far away to connect to a port on your machine (of course you are already in listen).

His connection will be listen join the wait queue waiting for the call of the Accept () function (the maximum number of join waiting queues is determined by the second parameter backlog that calls the Listen () function).

You call the Accept () function and tell him that you are ready to connect.

The Accept () function returns a new socket descriptor that represents the connection!

Well, when you have two socket descriptors, the one returned to you is the connection to the remote computer, and the first socket descriptor is still listen () on the original port on your machine.

The new socket descriptor you get will be able to perform the send () operation and the recv () operation.

Through the above explanation, I believe you must have a better understanding of the monitoring socket!

Listening socket connection sockets

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.