From the question to see the essence: What is the socket?

Source: Internet
Author: User
Tags bind data structures socket port number server port

first, the introduction of--socket is to solve the problem of inter-process communication between different computers.

1.socket Relationship to the process

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

2). Interprocess communication (native)

interprocess communication (different computers, networking)


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

1) socket is first used in Unix operating system, if you understand the UNIX system I/O, it is easy to understand the socket, because the socket data transmission is actually a special I/O.

2) can be used for file operation

3) There is a file descriptor. The essence of a file descriptor is a non-negative integer. Just used to differentiate. A similar process ID is also available.

3. The relationship between the server port and the number of connections
1) The service side listens on 8088 and then generates a new socket-to-client communication. (Note: The server-side listener port is
unchanged, but the socket connection can be generated all the time, one thread corresponds to a socket.)
at the same time, a port can only establish one connection.  
Listen on a port, but while listening to the port, generate a wait queue, each connection from the client is sent to the waiting queue, the server uses a certain algorithm to select the appropriate connection request processing, so on a port can listen to multiple requests. If you have too many connections at the same time, the server will have a corresponding duration for each connection to become longer. will become slower.
2) QQ Implementation method is to tell the server when landing you have logged in, send a message, first you will send a package to the server, the server to see if you need to send the other party online, if the return package online 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 the message will be forwarded to you through the server
3) network IO number and your CPU number consistent will be a better choice (considering that multi-threaded multiple processes can improve efficiency).  
There is no need to assign one port per customer. Is definitely a waste of unnecessary.  


4. Someone knows a port on the socket listener. How many customers can I connect to?
1) There is a parameter in listen () that should be determined for the number of concurrent connections clients.
2) The maximum length of the queue of pending connections.   If This value is somaxconn and then the underlying service provider responsible for socket s would   Set the backlog to a maximum "reasonable" value. There is no standard provision to find out the actual backlog value.
3) linux2.4, can have up to 1024 socket connections
4) The simultaneous connection request appears to be 5 (connection request, not connection), the maintained link is theoretically 65535 (2 byte socket port number),

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

5. Q: Now the server and client want to establish a socket connection, the server only know the client's IP, the port number does not know, can establish a connection? How to build it. There's no code to look at.
A: C and S is relative, the initiator of the connection is C, and the listening port accepts the connection is s,c if not know the port, how to initiate the connection,
In addition, for S, the port is a distinguishing flag for each service on S, and if the wrong port number is used to connect, the correct service is not available.
The client port is not required to be specified, the server binds the port, and then listens, client uses the server's IP and port to establish the socket connection


6. The best questions

Q: The article I saw said "each network communication loops through the TCP application layer of the host computer." 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 address of the machine and the port number used by the TCP software. ”
Also say "through the socket () function can create a socket, and then bind it to the port number ..."
So what exactly is the concept of socket sockets? is limited to the file descriptor returned by the socket (). Or a combination of IP and port numbers.   If so, what is the role of the socket descriptor generated after the socket () call? Socket descriptor, IP address, port number the relationship between the three is what.
Thank you for your predecessors ' answers.
Answer: A socket handle represents two addresses to "local Ip:port"--"remote Ip:port"
Ask: So the concept of socket so far. For example, using a socket () can produce a socket handle, but before bind () or connect () it is just a file descriptor, like any other file descriptor in Linux.
If the socket represents a two-address pair, then the function of the handle is not just the address pair that is used to differentiate and flag after bind () or connect (). Because then he can relate to the concept of the Internet. In this case, the meaning of the socket should be to say the IP address and port number address pairs of the communication parties described with the file descriptor. (and file descriptors are tokens that differentiate these pairs of addresses.) )
Answer: The socket is a kernel object that is maintained by the operating system kernel with its buffers, reference counts, and can be used in multiple processes.
As for the name "handle" "File descriptor" is the same, it is only the kernel open to the user process using an integer just
Q: Thank you upstairs, I didn't describe it clearly. I have no objection to "handle" and "File descriptor".
I think my problem is the relationship between the handle and the IP, port, do not know I say this to No:
1. Each socket essentially refers to an IP address and port pair
2. In order to operate such an address pair, a file descriptor is used
3. The socket () function only creates a normal file descriptor, and does not say that a socket was created for network traffic until bind () or connect ()
4. The socket is created only after bind () or connect ()
Answer: the socket () created a socket kernel object.
Accept or connect before 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"
Under Windows called handle, under Linux called file descriptor
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 name "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 introduce concepts.
When using socket programming, we all know that before network communication is first to establish a connection, and the connection is established by some operations on the socket to complete. Then, the process of establishing a connection can be broadly divided into the following steps:
1) set up socket sockets.
2) give the address to the socket, which is not the usual concept of a network address.
3) Establish a socket connection.

1. Set up socket sockets.
When we set up sockets using sockets, we actually created a data structure. This data structure is the main
Information is specifying the type of connection and the protocol used, as well as some structure fields for connection queue operations
(They are not involved here first).
When we use the socket function, if it succeeds, it returns a descriptor of type int, which points to the previous
The socket data structure that is maintained in the kernel. Any of our operations are done through this descriptor to that number.
The structure of the data. It's like we get a file descriptor after creating a file, and the operation of the file is
is done by file descriptors, rather than directly to the INODE data structure. The reason why I use file description
Because the socket data structure is also closely related to the Inode data structure, it does not exist independently of the kernel
, but is located in a VFS inode structure. So, there are some more abstract features that we can use to file
Operation to make an inappropriate analogy to deepen understanding.
As mentioned earlier, when this socket is established, we can get a socket description like a file descriptor
Character. Just as we operate on files, we can send data to our sockets by writing data to them
Where specified, this place can be a remote host, or it can be a local host. If you are interested, also
The IPC can be implemented using the socket mechanism, but the efficiency is low, so try it (I haven't tried it).

2. Assigns an address to a socket.
Depending on the purpose of the socket, there are two ways to give a socket address: server-side using bind, client
Use CONNETC.
Bind:
As we all know, with IP, prot can differentiate a TCP/IP connection (this connection, of course, refers to a
Connection channel, if you want to differentiate between specific host connections, you also need a third attribute hostname).
We can use the BIND function to give the address and port of communication to a socket that is used in a server-side routine.
Here we call the IP address and port of the communication together to form a socket address, and specify a socket to make
The process of passing with a specific IP and port combination is to give the socket an address.
To give the socket address, you have to use a data structure to indicate a specific socket address, and this data structure
is a struct sockaddr. I'm not going to use it because the purpose of this document is to clarify the concept and not to say
Use of the method. The function of the BIND function is to assign this particular data structure with the socket address information and
The socket socket is associated with an address that is given to the socket. But in terms of concrete implementation, how did they two
Connected, I don't know yet.
The lifetime of the address of a specific socket is after bind succeeds to the connection before disconnecting. You can create a
The data structure of the socket structure and the socket address, but the two of them are not related before bind.
, and after bind they both had a relationship. This relationship is maintained until the end of the connection, when a connection ends
, the data structure for the socket data structures and socket addresses is still present, but they are no longer related
The If you use this socket to reconnect on the socket, you need to bind both of them again. Again
Once, I said this connection is a connection channel, not a connection between a particular host.
The IP specified by bind is usually a local IP (typically not specifically specified, and is declared using Inaddr_any), while the primary
The function is to specify the port. After the server-side socket is bind, it is used to listen the socket
The address is ready to be connected.
Connect
For the client, the bind is not used (not useless, but it makes no sense) and 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 server-side socket address. While connect establishes the socket and socket address, it is also
Try to establish a remote connection.
3. Establish a socket connection.
For the ready to establish a connection, the server side takes two steps: Bind, listen; client one step:
Connct. If the server accept a connect, and the client receives the acknowledgement of this accept, then
A connection is established.

Third, the customer/ Server understanding of pattern patterns

The client/server model takes the active request method:

First, the server must start, and provide the appropriate services upon request:

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

2. Wait for customer request to reach the port;

3. Receives a duplicate service request, processes the request, and sends a response signal. Receives a concurrent service request to activate a new process to handle this customer request (such as fork, exec in a UNIX system). The new process handles this customer request and does not need to respond to other requests. After the service is complete, close the communication link with the customer for this new process and terminate.

4. Return to the second step and wait for another customer request.

5. Shutting down the server

Client side:

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

2. Send a service request message to the server, wait and receive an answer, continue to make a request ...

3. Close the communication channel and terminate after the request has ended.

From the process described above:

1. The role of the client and the server process is asymmetric, so the encoding is different.

2. The service process is generally initiated by  Ji . As long as the system is running, the service process persists until normal or forced termination.

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.