Embodiment
In order to make purpose of the present invention, technical scheme and advantage clearer,, the present invention is further elaborated below in conjunction with drawings and Examples.Should be appreciated that specific embodiment described herein only in order to explanation the present invention, and be not used in qualification the present invention.
The embodiment of the invention is by general inquiry mechanism, socket is set up state machine, socket carries out the data circulation according to state machine, and socket set up an informing mechanism, realize the state of notice socket data circulation promptly and accurately, under the very big situation of network concurrency, effectively handled procotol and network and connected like this.
In embodiments of the present invention, the state of socket (socket) is defined as follows:
Input/output interface io opens: after a socket was set up, its state just became the io open mode;
Io monitors: after a socket was monitored successfully, its state just became io and monitors;
Io etc. are to be connected: the state before this socket request connects;
The io free time: what input/output interface i/o temporarily do not have do;
The io idle waiting: general because the i/o that speed restriction causes does not go to read and write data, after lowering, uniform velocity just can read and write;
Io etc. continue: this generally is owing to temporarily do not read the data that data or we such as send after the data at the other side to be read, perhaps because the inbound pacing of restricting data and do not read data;
Io etc. are to be written: this generally is not have the state of write data owing to temporary transient write data failure or in order to control transmission speed;
Io test: be used for testing the i/o state;
Io waits for and closing: this i/o will close at once;
The io mistake: i/o makes a mistake;
Io closes: i/o is carrying out shutoff operation;
Io closes end: i/o closes;
Io is placed into the release formation: for recycling this i/o, after it finishes, put it into formation, wait for that others takes once more.
And the circulation state of protocol data is defined as follows:
Initialization: protocol initializing stage;
Read: socket has read the network data that is over;
Read: socket is reading network data;
The read data user waits for: when after network reads data, will preserve data to buffering or written document, this state generally is used for speed restriction, when speed greater than some threshold values the time, just allow written document be in wait state, till speed lowers;
Socket reads to wait for: socket does not temporarily read data, the socket that is in this state can join work queue, when the another one thread inquires the readable incident of this socket, send a notification message to this socket, notify this socket to go reading of data;
Write: socket has write the network data that is over;
Write: socket is writing network data;
Write data user waits for: this state is for speed restriction, when write data, at first will reach data from file or buffering, when transmission speed is too fast, for speed restriction, will allow the user temporarily get less than data, till speed lowers;
Socket writes wait: this generally causes owing to generation socket writes the i/o obstruction, the socket that is in this state will join the service area, the another one thread can be inquired about this socket, sends when this socket can write and can write notification message, and this socket goes write data more then;
Handle: the protocol data analyzing and processing finishes;
Handle: just at the analyzing and processing protocol data;
Handle and wait for: in general, we are to be submitted to go in the new thread to handle when handling a request msg, and at this moment main thread just returns the processing wait state;
Mistake: this state follows the socket mistake to handle together;
Preparation is closed: after protocol data all disposes, state is changed to the preparation closed condition;
Close: agreement is closed, and prepares to close socket afterwards.
Fig. 1 shows the realization flow of the sending method of the network data that the embodiment of the invention provides, and its detailed step is as described below:
In step S101, set up a socket, wherein the state of this socket is an open mode, the data protocol state is an init state.
In step S102, transmitting terminal sends connection request to receiving terminal, and this moment, the state of socket was etc. to be connected.
In step S103, whether the connection of determining step S102 success, is execution in step S105 then, otherwise execution in step S104.
In step S104, close above-mentioned socket, program is directly returned.
In step S105, the notice slot slot of registration socket.
In embodiments of the present invention, it is Isocket and IsocketSlot that 2 classes are arranged in program, Isocket has encapsulated the socket operation specially, IsocketSlot is used to provide notified function specially, and Isocket remains with IsocketSlot, and IsocketSlot also remains with Isocket, when sending request, if the socket successful connection so just associates corresponding IsocketSlot and this Isocket, the process of registration that Here it is.
In step S106, generate protocol data, and store this protocol data into buffering area, be ready for sending protocol data, the state of protocol data is for writing state at this moment, and wherein this protocol data comprises statusline, head and inclusion.
In step S107, by socket to the receiving terminal write data.
In step S108, judge the whether success of socket write data, be execution in step S109 then; Otherwise execution in step S110.Wherein, write data is that piecemeal is write on network, and for example every 4K is one and sends.
In step S109, the socket write data is finished, and promptly data file sends and finishes, and the protocol data state is the write data state, and then, socket reads the response of receiving terminal, and the state of socket is for reading at this moment.
In embodiments of the present invention, after socket ran through the response of protocol data, the presentation protocol data sent and finish, and promptly connected and finished; Get nowhere if read to respond, so this socket is joined formation, and whether constantly inquire about this formation readable, read response when readable again, close socket then, whole protocol interaction finishes.
In step S110, socket write data failure, this moment, the socket state was the socket error condition, and the protocol data state to be socket write wait state, simultaneously socket is joined certain work queue.
In step S111, new thread is constantly inquired about the state of the socket in the above-mentioned work queue, judges whether the state of socket becomes and can write, and is execution in step S112 then, otherwise execution in step S113.
In step S112, transmitting terminal sends to above-mentioned socket can write message, and returns execution in step S108, and in embodiments of the present invention, this cyclic process exists always, sends up to data file to finish.
In step S113, wait for, can write state up to socket, and execution in step S111 once more.
In embodiments of the present invention, data transmission procedure is to adopt based on transmission control protocol (TransmissionControl Protocol, TCP) agreement, this Transmission Control Protocol mainly is HTML (Hypertext Markup Language) (Hyper TextTransfer Protocol, HTTP), real-time streaming protocol (Real Time Streaming Protocol, RTSP) and custom protocol.
In embodiments of the present invention, socket is assigned in the service area, wherein the processing for I/O is divided into a lot of service areas, the I/O that handle each service area has certain restriction, for example 1024 sockets can only be handled in each service area at every turn, are handled by first service area at present and monitor and all the other remaining I/O, if socket number to be processed is many, just increase the service area, the service area of increase can be withdrawed from after work disposal is finished; If the socket number has lacked, just reduce the service area.Each service area is by a thread process, the number of the each inquiry of general windows system default is 64, the linux number of acquiescence down is 1024,130 sockets are handled in each service area under windows, 1024 sockets are handled in each service area under linux, the thread of handling each service area is always all in operation, and each socket of this service area of circular treatment is adjusted like this and made that the network concurrent processing is very capable.Speed and efficient have been guaranteed.Wherein, socket always the placement in non-plug patterns.
As one embodiment of the present of invention, there are two kinds of situations in the response that socket reads receiving terminal, be protocol interaction and file transfer, in embodiments of the present invention, protocol interaction comprises request, handles and responds, and these three steps can represent that this protocol interaction process finishes, and the process of file transfer is the process of a circulation, file transfer is finished by piecemeal, and the transmission of each block file all is the process of a complete protocol interaction.The following detailed process that provides is described:
In step S201, judge the whether success of response bag read receiving terminal, be execution in step S202 then, otherwise execution in step S203.
In step S202, read the response bag of receiving terminal, this moment, the protocol data state was a read states, handled the response of receiving terminal simultaneously.
In embodiments of the present invention, program is divided into application layer and bottom, after bottom reads response, response is submitted to application layer process, for example registers a kind of transmission means, send request and after service end responds to service end in client, client has read the response bag of service end, to respond bag then and be submitted to the assembly of registering transmission means just now, and, then not need to have registered again if succeed in registration, if registration is unsuccessful, then can constantly register, till success, certain different response process mode difference, business demand is also different, the response that has does not need to handle, but as the reciprocal process of an agreement, whole flow process will finish.
After disposing, the socket state is for closing, and the protocol data state also is a closed condition, and whole protocol interaction process finishes.
In step S203, do not read the response bag of receiving terminal, the socket state variation is the socket mistake, the protocol data state variation is that socket reads to wait for, at this moment, socket is joined work queue.
In step S204, new thread is constantly inquired about the state of this socket, judges promptly whether socket is readable, is execution in step S205 then, otherwise execution in step S206.
In step S205, notice socket reads and responds bag, returns execution in step S201 simultaneously.
In step S206, continue inquiry, readable up to socket.
If it is normal protocol interaction that above-mentioned network connects, socket only reads the response bag of a receiving terminal so.
If above-mentioned connection is a file transfer, then repeatedly to read between transmitting terminal and the receiving terminal and respond bag, i.e. circulation is read.
In embodiments of the present invention, be connected to the situation of file transfer for network, as described in Figure 3, whether the flow that reads network surpassed set point detect, its detailed step is as described below:
In step S301, judge whether read the Network Transmission flow surpasses set point, be execution in step S302 then, otherwise finish, in embodiments of the present invention, this judgement promptly is to judge that whether the file flow that transmits in the current network surpasses preset value, transmits because file transfer is a piecemeal, when minute block size surpasses preset value, be and exceed the predetermined network delivery flow rate.
In step S302, in the written document process, to return and write interruption, this moment, the protocol data state variation waited for that for the read data user socket state temporarily becomes the idle waiting state.
In step S303, socket is joined in the work queue.
In step S304, new thread is constantly inquired about the state of the socket in the above-mentioned work queue, judges promptly whether above-mentioned socket is readable, is execution in step S305 then, continues inquiry otherwise carry out.
In step S305, notice socket reads file data.
In step S306, constantly read in the process of file data at socket, judge whether reading of data is successful, is then to continue to read, otherwise execution in step S308.
In step S307, the socket state becomes error condition, and the protocol data state variation is that socket reads to wait for, this socket is joined work queue.
In step S308, the another one new thread is constantly inquired about the state of this socket, judges promptly whether socket is readable, is execution in step S309 then, otherwise continues inquiry.
In step S309, notify this socket reading of data, so repeatedly till file runs through.
Just effectively control network traffics by above-mentioned realization flow, realized the transmission of file data.
Fig. 4 shows the realization flow of the method for reseptance of the network data that the embodiment of the invention provides, and its detailed step is as described below:
In step S401, set up one and monitor socket.
In step S402, judge that socket monitors whether success, whether the connection request of some ip address and port is promptly arranged, be execution in step S404 then, otherwise execution in step S403.
In step S403, close this socket and return.
In step S404, will monitor socket and join work queue, the state of this socket is a listening state.
In step S405, new thread is constantly inquired about the monitoring socket in the service area.
In step S406, constantly inquire about according to above-mentioned new thread, judge whether to listen to the connection request of transmitting terminal, be execution in step S407 then, otherwise finish, close this monitoring socket.
In step S407, monitor socket new socket of accept again.
In embodiments of the present invention, the connection that listens to is submitted in the linear formation, receiving terminal is opened up this connection that a thread process listens to specially.
In step S408, judge again the whether success of new socket reading of data of accept, this moment, the protocol data state was just at read states, was execution in step S413 then, otherwise execution in step S409.
In step S409, new socket reading of data is unsuccessful, and this moment, this socket state variation was the socket mistake, and the protocol data state variation is the socket read error, shows temporarily to read less than data, simultaneously this socket is joined in the work queue.
In step S410, new thread is constantly inquired about the state of new socket.
In step S411, it is readable whether the state of judging new socket becomes, and is execution in step S412 then, otherwise continues inquiry, and return this step.
In step S412, receiving terminal sends readable notification message to new socket, and returns execution in step S408.
In step S413, after reading of data finished, the protocol data state variation was a read states, beginning analyzing and processing protocol data, and this moment, the protocol data state variation was for just at treatment state.
In step S414, receiving terminal is write the response data, and this moment, socket state limit was for writing.
As another embodiment of the present invention, receiving terminal is write the response packet and is drawn together two kinds of situations, it is the mutual and file transfer of normal protocol, in embodiments of the present invention, protocol interaction comprises request, handles and responds, and these three steps can represent that this protocol interaction process finishes, and the process of file transfer is the process of a circulation, file transfer is finished by piecemeal, and the transmission of each block file all is the process of a complete protocol interaction.Followingly provide detailed description:
In step S501, judge to write and respond whether success of bag, be execution in step S502 then, otherwise execution in step S503.
In step S502, respond bag and write success, the protocol data state is closed socket for writing state at this moment.
After disposing, the socket state is for closing, and the protocol data state also is a closed condition, and the protocol interaction process finishes, if file transfer then can only just can be closed this socket after the file transfer of all piecemeals is finished.
In step S503, write failure if respond bag, the socket state variation is the socket mistake, the protocol data state variation is that socket writes wait, at this moment, socket is joined work queue.
In step S504, new thread is constantly inquired about the state of this socket.
In step S505, judge whether socket can write, be execution in step S506 then, otherwise execution in step S507.
In step S506, notice socket writes and responds bag, returns execution in step S501 simultaneously.
In step S507, continue inquiry, can write up to socket.
If it is normal protocol interaction that above-mentioned network connects, then writes and once respond bag.
If above-mentioned connection is the transmission of file, then receiving terminal is repeatedly write to data sending terminal and is responded bag, and the piecemeal of a file of every transmission all will be write response, and it is as described below wherein whether the flow of network to be surpassed the detailed process of detection of set point:
In step S601, judge that whether the flow of the network of file data surpasses set point, be execution in step S602 then, otherwise finish.
In step S602, in reading the file process, to return and read to interrupt, this moment, the protocol data state variation waited for that for the write data user socket state temporarily becomes the idle waiting state.
In step S603, socket is joined in the work queue.
In step S604, new thread is constantly inquired about the state of the socket in the above-mentioned work queue, judges promptly whether above-mentioned socket can write, and is execution in step S605 then, continues inquiry otherwise carry out.
In step S605, notice socket file data.
In step S606, in the process of the continuous file data of socket, judge whether write data is successful, is then to continue to read, otherwise execution in step S607.
In step S607, the socket state becomes error condition, and the protocol data state variation is that socket writes wait, and this socket is joined work queue.
In step S608, the another one new thread is constantly inquired about the state of this socket, judges promptly whether socket can write, and is execution in step S609 then, otherwise continues inquiry.
In step S609, notify this socket write data, so repeatedly till file runs through.
Fig. 7 shows the structure chart of the data transmission system that the embodiment of the invention provides, the part relevant with the embodiment of the invention only is shown for convenience of explanation, wherein, data Transmission system 1 and data receiving system 2 place software unit, hardware cell or the software and hardware unit of terminal.
Data Transmission system 1 comprises that socket sets up module 11, connection request sending module 12, notice slot Registering modules 13 and socket write data judging treatmenting module 14, wherein:
Socket is set up module 11 and is set up socket; Connection request sending module 12 sends network connecting request; When judging this successful connection, the notice slot of notice slot Registering modules 13 these sockets of registration; Socket write data judging treatmenting module 14 judges by the whether success of this socket write data, when the write data success, and the response bag of reading of data receiving terminal then; When write data is unsuccessful, this socket is added work queue, and constantly detect the state of this socket, when this socket can be write, but notify this socket write data.
As one embodiment of the present of invention, the socket write data is handled judge module 14 and is comprised that also reading response wraps judge module 141, reads and respond packet handing module 142 and read response bag judging treatmenting module 143, wherein:
Read response bag judge module 141 and judge whether the response bag that reads receiving terminal is successful; When the response of reading receiving terminal is bundled into merit, reads and respond 142 processing responses of packet handing module bag; When the response bag that reads receiving terminal is unsuccessful, read response bag judging treatmenting module 143 this socket is joined work queue, and constantly detect the state of socket, when socket is readable, the response bag of reading of data receiving terminal.
In embodiments of the present invention, socket write data judging treatmenting module 14 also comprises to be read network traffics judge module 144 and reads flow processing module 145, reads network traffics judge module 144 and judges whether the Network Transmission flow of file data has surpassed preset value; When the Network Transmission flow of this document data has surpassed preset value, read flow processing module 145 and return and write interruption, socket is joined in the work queue, and judge the state of inquiry socket, when socket was readable, notice socket read file data.
Data receiving system 2 comprises that monitoring socket sets up judge module 21, connection request judge module 22, new socket selection module 23 and socket reading of data judging treatmenting module 24, wherein:
The monitoring socket is set up judge module 21 and is set up a monitoring socket, and judges whether this socket monitors successful; Connection request judge module 22 judges whether monitor socket listens to network connecting request; When successful connection, the socket that new socket selection module 23 is monitored is new socket of accept again; Socket reading of data judging treatmenting module 24 judges by the whether success of new socket reading of data, when reading of data is successful, and the request package of reading of data transmitting terminal then; When reading of data is unsuccessful, new socket is added work queue, and constantly detect the state of new socket, when new socket is readable, notify new socket readable data.
As another embodiment of the present invention, socket reading of data judging treatmenting module 24 comprises writing to be responded bag judge module 241, write response packet handing module 242 and writes response bag judging treatmenting module 243, wherein:
Write response bag judge module 241 and judge whether successful write response wraps; Respond when being bundled into merit when writing, write and respond packet handing module 242 and close this socket; Write and respond bag judging treatmenting module 243 and respond bag when unsuccessful, should join work queue by new socket, and constantly detect the state of this new socket, when this new socket can be write, write response and wrap when writing.
In embodiments of the present invention, socket reading of data judging treatmenting module 24 also comprises to be write network traffics judge module 244 and writes flow processing module 245, wherein:
Write network traffics judge module 244 and judge whether the Network Transmission flow of file data has surpassed preset value; When the Network Transmission flow of file data has surpassed predefined value, write the flow processing module and return and read to interrupt, socket is joined in the work queue, and judge the state of socket, when socket can write, notice socket sent file data.
The embodiment of the invention is by general inquiry mechanism, socket is set up state machine, socket carries out the data circulation according to state machine, and socket set up an informing mechanism, realize the state of notice socket data circulation promptly and accurately, under the very big situation of network concurrency, effectively handled procotol and network and connected like this.
The above only is preferred embodiment of the present invention, not in order to restriction the present invention, all any modifications of being done within the spirit and principles in the present invention, is equal to and replaces and improvement etc., all should be included within protection scope of the present invention.