From the point of view of the essence: what is the socket in the end?

Source: Internet
Author: User
Tags server port

The introduction of--socket is to solve the problem of interprocess communication between different computers

1.socket Relationship to the process

1. The relationship between the socket and the process: the socket is used to exchange information (IPC) between a process and other processes, and the socket interface is the API interface function of the TCP/IP network.

2). Interprocess communication (in this machine)

interprocess communication (different computers, to be networked)


2, the relationship between the socket and the file-- How do I understand that a socket is a special I/O?

1 socket first applied to the UNIX operating system, if you understand the UNIX system I/O, it is easy to understand the socket, because the socket data transfer is actually a special I/O.

2 can be used for file operation

3) There are file descriptors. The essence of a file descriptor is a non-negative integer. Just to differentiate. Similarly, there is the process ID.

3. The relationship between the server port and the number of connections
1) The service side listens on 8088, then generates a new socket and client communication. (Note: The server-side listening port is
unchanged, but the socket connection can be generated and one thread corresponds to a socket.)
at the same time, a port can only establish one connection. &NBSP
Listening on a port, but while listening to the port, the generation of a waiting queue, each from the client's connection will be sent to the waiting queue, the server uses a certain algorithm to select the corresponding connection request processing, so in a port can listen to many requests. If there are too many connections at the same time, the server corresponds to the length of each connection. It slows down.
2) QQ implementation method is in the landing time to tell the server you have landed, when sending a message, first you will send a packet to the server, the server to see if you need to send the other side is online, if the online return package to tell you the other side online, and then you will establish a connection with each other, Then send a message directly to each other, if the other side is not on the line, then will be forwarded through the server you this message
3) The number of network IO and your CPU number is consistent will be a better choice (considering multithreading multiple processes can improve efficiency). &NBSP
There is no need to allocate one port per client. Is definitely a meaningless waste.  


4. It is known that a socket is listening on a port. How many customer connections can there be?
1 Listen () has a parameter, it should be to determine the number of concurrent connections customers ...
2) The maximum length of the queue of pending connections.   If This value is Somaxconn, then the underlying service provider responsible for socket s   Set the backlog to a maximum "reasonable" value. There is no standard provision the actual backlog value.
3) linux2.4, you can have up to 1024 socket connections
4 at the same time the connection request seems to be 5 (is the connection request, not the connection), the link that can be maintained is theoretically 65535 (2 bytes of socket port number),

3.Socket is one end of two-way communication between two programs running on the network, it can accept the request, also can send the request, use it to write the data on the network conveniently.

5. Q: Now the server and client want to establish a socket connection, server only know the client's IP, the port number does not know, can establish a connection? There's no code to look at.
A: C and S are relative, the one on which the connection is initiated is C, and the one on which the listening port accepts the connection is s,c. If you don't know the port that s listens to, how do you initiate the connection?
In addition, for S, the port is a distinction between the services on S, and if you connect with the wrong port number, you can't get the right service.
The client's port is not required, the server binds the port, and then listens, the client uses the server's IP and port to establish the socket connection


6. Excellent question and answer

Q: See the article says "Each network communication loops into and out of the main computer's TCP application layer." It is uniquely identified by two connected numbers. The two numbers together are called sockets. The two numbers that make up the socket are the IP addresses of the machine and the port number used by the TCP software. ”
"You can create a socket by using the socket () function and then bind it to the port number ..."
So where does the concept of socket sockets go? is a file descriptor that is only returned by the socket (). Or a combination of IP and port numbers.   If so, what is the function of the socket descriptor that is generated after the socket () call? Socket descriptor, IP address, port number of the relationship between the three.
Thank you for your predecessors ' answers.
Answer: A socket handle represents two addresses to "local Ip:port"--"remote Ip:port"
Ask: So what is the concept of socket so far? For example, using a socket () can produce a socket handle, but before bind () or connect () it is a file descriptor, just like any other file descriptor in Linux.
If the socket represents a two-address pair, then the function of the handle is to differentiate and mark such an address only after bind () or connect (). Because of this he can connect with the concept of network. In this case, the meaning of the socket should be said to use the file descriptor described by both the IP address and the port number address pair. (The file descriptor is the tag that distinguishes these address pairs.) )
Answer: The socket is a kernel object that is maintained by the operating system kernel for its buffers, reference counts, and can be used in multiple processes.
As for the "handle" "File descriptor" is the same, it is only the kernel open to the user process using the integer only
Q: Thank you upstairs, I did not describe clearly. I have no objection to "handle" and "File descriptor".
I think my problem is that the handle and IP, port relationship, do not know I say this to No:
1. Each socket essentially refers to an IP address and a port pair
2. To operate such an address pair, a file descriptor is used
3. The socket () function creates only a common file descriptor and cannot be said to have created a socket for network communication until bind () or connect ()
4. The socket was created only after bind () or connect ()
Answer: the socket () creates a socket kernel object.
Accept or connect, you can read and write to the socket handle. Because the IP and port inside the socket kernel object will be set only after connect or bind,listen,accept.

Second, socket and Port understanding

A socket handle represents two addresses to "local Ip:port"--"remote Ip:port"
Called a handle under Windows, called a file descriptor under Linux
The socket is a kernel object that is maintained by the operating system kernel for its buffers, reference counts, and can be used in multiple processes. As for "handle" "File descriptor" is the same
I assume that the reader is already familiar with the process of establishing a socket connection and various state transitions, because the purpose of this document is to clarify concepts rather than to introduce concepts.
When programming with sockets, we all know that we have to establish a connection before network communication, and the connection is built by some operations on the socket. Then, the process of establishing a connection can be roughly divided into the following steps:
1) To establish socket socket sockets.
2 to give the socket address, this address is not the concept of the usual network address.
3) Establish a socket connection.

1. Creates a socket socket.
When you use the socket to create a socket, we actually create a data structure. This data structure is most important
Information is the type of connection specified and the protocol used, in addition to the structure fields for connection queue operations
(They are not covered here first).
When we use the socket function, if successful, it returns an int descriptor, which points to the previous
The socket data structure that is maintained in the kernel. Any of our operations are based on this descriptor and the number
According to the structure. It's like we get a file descriptor after we create a file, and the operation of the file
is done through file descriptors, not directly to the INODE data structure. The reason I use file description
character, because the socket data structure is also closely related to the Inode data structure, it is not independent in the kernel
, but is located in a VFS inode structure. So, there are some more abstract features that we can use to document
Operation to make an improper analogy to deepen understanding.
As mentioned earlier, when this socket is established, we can get a socket description like a file descriptor
Character. As we do with files, we can transfer data to our sockets by writing data to the socket.
Specifies where this can be a remote host or a local host. If you are interested, also
You can use the socket mechanism to implement IPC, but the efficiency is low, try it on the line (I have not tried).

2. Assigns an address to a socket.
Depending on the purpose of establishing sockets, there are two ways to give a socket address: server-side use BIND, client
Use CONNETC.
Bind:
As we all know, as long as IP is used, prot can differentiate between a TCP/IP connection (this connection, of course, refers to a
Connection channels, a third attribute hostname is required if you want to differentiate between specific hosts.
We can use the BIND function to assign addresses and ports to a communication using a socket in a server-side routine.
Here we call the IP address of the communication and the port together to form a socket address, and specify a socket to make
The process of using specific IP and port combinations is to give this socket an address.
To give the socket address, you have to use a data structure to indicate the specific socket address, which
It's struct sockaddr. I'm not going to talk about it, because the purpose of this document is to clarify concepts, not to say
The method of use is prescribed. The function of the bind function is to have the data structure with the socket address information in this particular annotation and
The socket socket is connected by giving this socket an address. But in terms of concrete implementation, how did they both
I don't know if it's connected.
The lifetime of the address of a particular socket is after bind succeeds to the disconnect before the connection. You can build a
The data structure of the socket structure and the socket address, but the two are not related until bind
, the two of them had a relationship after bind. This relationship has been maintained until the end of the connection, when a connection ends
, the data structure of the socket structure and socket address is still present, but they are not related.
Out. If you use this socket to reconnect to the socket address, you need to bind them both again. Again
Once again, I said this connection is a connection channel, not a connection between specific hosts.
BIND specifies that the IP is typically a local IP (typically not specifically specified and used Inaddr_any to declare), while the most important
The function is to specify the port. After the socket on the server has been bind, it is used to listen the socket
The address is ready for the connection.
Connect
For the client, bind is not used (it is not useless, but it makes no sense) that they will pass
Connet function to establish the relationship between the socket and the socket address. The socket address is the one that it wants to connect to.
The socket address on the server side. When connect establishes the connection between the socket and the socket address, it also
Try to establish a remote connection.
3. Establish a socket connection.
For a connection to be set up, the server side takes two steps: Bind, listen; client one step:
Connct. If the server side accept a connect and the client gets the confirmation of this accept, then
A connection is established.

third, customer/ Server understanding of pattern patterns

The client/server model takes the form of a proactive request:

First, the server must start and provide the appropriate service on request:

1. Open a communication channel and inform the local host that it is willing to receive customer requests on a recognized address (such as FTP 21);

2. Wait for customer request to reach the port;

3. Receive a duplicate service request, process the request and send an answer signal. A concurrent service request is received, and a new process is activated to process the client request (for example, fork, exec) in UNIX systems. The new process handles this customer request and does not need to respond to other requests. When the service completes, close the communication link between this new process and the customer and terminate.

4. Return to step two and wait for another client request.

5. Shut down the server

Client side:

1. Open a communication channel and connect to a specific port on the host of the server;

2. Send the service request message to the server, wait and receive the reply, continue to make the request ...

3. Close the communication channel and terminate after the request is completed.

From the process described above:

1. The role of the client and server processes is asymmetric, so the coding is different.

2. The service process is generally the first Chung Ji  ji  request and started. As long as the system is running, the service process persists until it is normal or forced to terminate.


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.