What listen [Python] dry socket in the () parameters (numbers) in the end mean?

Excerpt: https: //blog.csdn.net/HFZeng/article/details/93843442

doubt

  • When calling socket, we will use to listen () function, which has a parameter called the backlog, such as:. Socket.listen (5) then the number 5 in the end what does that mean?
  • Online there are many claims, telling the concepts, many of which are copied and pasted, misleading.

Solution
used below to explain the particular code fragment:

  • This is a computer simulation of the machine and end customer service side of the program, the main function is to establish the socket connection, the client input the keywords corresponding scripts, the server returns the results.
  • Server service two customers at the same time meet the query operation
  • This program also includes multi-threading and semaphores

To listen (1), to explain the parameters set to 1, the parameter is not set to 1, the connection will open three customer service being given it?
Not! As shown below:

 

 

So the question is!

Question 1:
As the example code, there is listen (1), this one refers to what? Can only be established with a socket link? Why do I use the above code can create tcp connection of no greater than an error, saying in theory, if other people use the Internet connection should be greater than one being given!

Since: listen (n) value is passed, the n-Server indicates rejection (more than a limited number of) prior to connection, the operating system can suspend the maximum number of connections. n can be seen as "the number of queued"

Question 2:
Since there is no error, why did not print the address of the user 3?

Because: the server is processing the users 1 and 2, there is no free go to reception 3 user, so the user 3 to queue up.

Question 3:
Why the server can handle simultaneous users 1 and 2?
because:

This uses multithreading and semaphore, the semaphore is set to 2, i.e. allowing concurrent 2, the server opens the two threads can be simultaneously processed user 1 and user 2.

总结
socket.listen(n)
简单来说,这里的nt表示socket的”排队个数“

一般情况下,一个进程只有一个主线程(也就是单线程),那么socket允许的最大连接数为: n + 1
如果服务器是多线程,比如上面的代码例子是开了2个线程,那么socket允许的最大连接数就是: n + 2
换句话说:排队的人数(就是那个n) + 正在就餐的人数(服务器正在处理的socket连接数) = 允许接待的总人数(socket允许的最大连接数)

Guess you like

Origin www.cnblogs.com/LiuYanYGZ/p/12227447.html