In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use socket's select model". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use socket's select model.
The Select model can be used under both windows and linux, while the more advanced epoll model can only be used under linux.
Before you start select, correct an error. The _ _ check_head function in MsgContainer should be changed to look like this:
Def _ check_head (self):
If self.msg_len = = 0 and len (self.msgpond) > 5:
Self.__get_msg_len ()
Self.__get_msg ()
Unexpectedly, there is no feedback on this mistake, so we can see that we are just taking a look at it, but there is no actual verification or use.
Select model is easy to understand, we give it three arrays, the array stores socket, each select, the model will pick out readable, writable, abnormal socket from these three arrays, and put them into three arrays, so that the application layer traverses the three arrays and does the corresponding operations. Look at the code:
# coding=utf-8
Import select
Import socket
Import sys
From MsgContainer import MsgContainer
Def start_server (port):
HOST='0.0.0.0'
PORT=port
Server = socket.socket (socket.AF_INET,socket.SOCK_STREAM)
# server.setblocking (False)
Server.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Server.bind ((HOST,PORT)) # socket bound IP and port
Server.listen (10) # start TCP snooping
Inputs = [server] # stores the socket that needs to be detected and readable
Outputs = [] # store writable socket that needs to be detected
Message_queues = {} # stores data that can be sent
While inputs:
Readable, writable, exceptional = select.select (inputs, outputs, inputs)
# readable
For s in readable:
If s is server:
Connection, client_address = s.accept ()
Inputs.append (connection)
Message_ queues [connection] = MsgContainer ()
Else:
Data = s.recv (3) # deliberately set so small
If data:
Message_ queues [s] .add _ data (data)
# data has been received from this socket, if you want to write it, add it to the outputs and wait for the select model to check whether it is writable
If s not in outputs:
Outputs.append (s)
Else:
# receipt of empty data means that the other party has been disconnected and needs to be cleaned up
If s in outputs:
Outputs.remove (s)
Inputs.remove (s)
S.close ()
Del message_ queues[s]
# writable
For w in writable:
# be sure to determine whether w is really writable here. It is possible that w is in both readable and writable, and the data read is empty, which actually closes the connection.
If not w in message_queues:
Continue
Mc = message_ queues [w]
Msgs = mc.get_all_msg ()
Print msgs
For msg in msgs:
Msg = mc.pack_msg (msg)
W.send (msg)
Mc.clear_msg ()
Outputs.remove (w)
# exception
For s in exceptional:
Inputs.remove (s)
If s in outputs:
Outputs.remove (s)
S.close ()
Del message_ queues[s]
If _ _ name__ = ='_ _ main__':
If len (sys.argv) = = 2:
Port = int (sys.argv [1])
Start_server (port)
Else:
Print u 'Please enter the port number'
Here are a few questions that you may be confused about.
How do you receive data when you find a readable socket? The examples I provided before are constantly read in the wihle loop, which can still be used in the select model, but note that the socket needs to be set to non-blocking in order to get out of the while loop. The example in this article does not use a while loop, but reads 3 bytes of data at a time. If the length of the data sent by the other party is 9, then there are still 6 bytes of data to read in the receive buffer after the first read, and the socket will still be put into the readtable during the next select operation, so that you can continue to read the rest of the data.
When traversing the writable array, be sure to determine whether the socket is really writable. I found such a situation in the actual test, a socket is both in readtable and writable, but when reading, the data is empty, the other party closes the connection, and an error occurs when writing data, so I do a simple check on message_queues to determine whether socket is really writable.
After each write operation, the socket is deleted from the writable. The reason for this is very simple. The data to be written has already been written. If it is not deleted, the next select operation will put it into the writable, but now there is no data to write. This is meaningless, it will only waste the time of the select operation, because it has to traverse every socket in the outputs. Determine whether they can write to decide whether to put it in the writtable.
In exceptional, errors and exceptions occur in the socket, with this array, do not have to worry about errors and exceptions, otherwise the program is very complex to write, with a unified management, cleaning up after errors will become very simple.
At this point, I believe you have a deeper understanding of "how to use socket's select model". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.