CN111711996A - Method and device for stable transmission of TCP communication - Google Patents

Method and device for stable transmission of TCP communication Download PDF

Info

Publication number
CN111711996A
CN111711996A CN202010840566.0A CN202010840566A CN111711996A CN 111711996 A CN111711996 A CN 111711996A CN 202010840566 A CN202010840566 A CN 202010840566A CN 111711996 A CN111711996 A CN 111711996A
Authority
CN
China
Prior art keywords
request
service
data
connection
response
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202010840566.0A
Other languages
Chinese (zh)
Other versions
CN111711996B (en
Inventor
陆舟
于华章
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Feitian Technologies Co Ltd
Original Assignee
Feitian Technologies Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Feitian Technologies Co Ltd filed Critical Feitian Technologies Co Ltd
Priority to CN202010840566.0A priority Critical patent/CN111711996B/en
Publication of CN111711996A publication Critical patent/CN111711996A/en
Application granted granted Critical
Publication of CN111711996B publication Critical patent/CN111711996B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04WWIRELESS COMMUNICATION NETWORKS
    • H04W76/00Connection management
    • H04W76/10Connection setup
    • H04W76/11Allocation or use of connection identifiers
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L67/00Network arrangements or protocols for supporting network services or applications
    • H04L67/01Protocols
    • H04L67/133Protocols for remote procedure calls [RPC]
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04LTRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
    • H04L69/00Network arrangements, protocols or services independent of the application payload and not provided for in the other groups of this subclass
    • H04L69/16Implementation or adaptation of Internet protocol [IP], of transmission control protocol [TCP] or of user datagram protocol [UDP]
    • H04L69/161Implementation details of TCP/IP or UDP/IP stack architecture; Specification of modified or new header fields
    • H04L69/162Implementation details of TCP/IP or UDP/IP stack architecture; Specification of modified or new header fields involving adaptations of sockets based mechanisms
    • HELECTRICITY
    • H04ELECTRIC COMMUNICATION TECHNIQUE
    • H04WWIRELESS COMMUNICATION NETWORKS
    • H04W76/00Connection management
    • H04W76/10Connection setup
    • H04W76/18Management of setup rejection or failure

Abstract

The invention provides a method and a device for stable transmission of TCP communication, comprising the following steps: the WIFI module receives a request sent by the main control module through a serial port, judges the type of the request, obtains a TCP channel identifier corresponding to an Internet protocol address and a port number of a service in the connection service request when the type of the request is the connection service request, and establishes connection with the service according to the TCP channel identifier; when the type of the request is a request for closing the service connection, closing the connection of the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for closing the service connection; when the type of the request is a request for writing data to the service, writing data to the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for writing data to the service to obtain a data writing result; and when the type of the request is a request for reading data from the service, reading the data from the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for reading the data from the service and the byte number of the data to be read.

Description

Method and device for stable transmission of TCP communication
Technical Field
The invention relates to the technical field of internet, in particular to a method and a device for stable transmission of TCP (transmission control protocol) communication.
Background
Along with the development of the internet of things, the use of the internet of things equipment is more and more popular, and the internet of things equipment is usually connected with a plurality of services in the use process.
Disclosure of Invention
The invention provides a method and a device for stable transmission of TCP communication, which solve the technical problems.
The invention provides a method for stable transmission of TCP communication, which comprises the following steps:
step 1, the WIFI module receives a request sent by the main control module through a serial port, judges the type of the request,
when the type of the request is a connection service request, executing the steps 2-3:
step 2, the WIFI module obtains a TCP channel identifier corresponding to an Internet protocol address and a port number of a service in the connection service request, connection is established with the service according to the Internet protocol address and the port number of the service in the connection service request and the TCP channel identifier, whether the connection is successful or not is judged, if the connection is successful, the step 3 is executed, if the connection is failed, resources corresponding to the TCP channel identifier are closed, an error response is returned to the main control module through a serial port, and the process is finished;
step 3, the WIFI module generates a response of the connection service request according to the internet protocol address and the port number of the service and the TCP channel identifier, and sends the response of the connection service request to the main control module through the serial port;
when the type of the request is a request for closing the service connection, executing the step 4:
step 4, the WIFI module judges whether the TCP channel identification in the service connection closing request is legal or not, if so, the connection of the service corresponding to the TCP channel identification is closed according to the TCP channel identification in the service connection closing request, a response of the service connection closing request is formed according to the TCP channel identification, the response of the connection closing request is sent to the main control module through the serial port, and if not, an error response is returned to the main control module through the serial port;
when the type of the request is a request for writing data to the service, executing step 5:
step 5, the WIFI module judges whether a TCP channel identifier in the request for writing data to the service is legal or not, and if the TCP channel identifier is illegal, an error response is returned to the main control module through the serial port; if the data writing request is legal, writing data to the service corresponding to the TCP channel identifier according to the TCP channel identifier in the data writing request for the service and the data to be written, obtaining a data writing result, forming a response of the data writing request for the service by the byte number and the TCP channel identifier of the data successfully written to the service in the data writing result when the data writing result is successful, sending the response of the data writing request for the service to the main control module through the serial port, and returning an error response to the main control module through the serial port when the data writing result is failed;
when the type of the request is a request for reading data from the service, step 6 is executed:
step 6, the WIFI module judges whether a TCP channel identifier in the request for reading data from the service is legal or not, and if the TCP channel identifier is illegal, an error response is returned to the main control module through the serial port; if the data reading result is successful, forming a response of the request for reading the data from the service according to the byte number of the successfully read data in the data reading result, the read data and the TCP channel identification, and sending the response of the request for reading the data from the service to the main control module through the serial port; and when the result of reading the data is failure, returning an error response to the main control module through the serial port.
The invention provides a device for stable transmission of TCP communication, which comprises:
the method comprises the following steps: WIFI module and host system, the WIFI module includes:
the receiving unit is used for receiving the request sent by the main control module through the serial port;
a first judging unit for judging the type of the request received by the receiving unit;
an obtaining unit, configured to obtain, when the type of the determination request of the first determining unit is a connection service request, a TCP channel identifier corresponding to an internet protocol address and a port number of a service in the connection service request;
the connection unit is used for establishing connection with the service according to the Internet protocol address and the port number of the service in the connection service request and the TCP channel identifier;
the second judging unit is used for judging whether the connection unit is successfully connected with the service;
a closing ending unit, configured to close the resource corresponding to the TCP channel identifier when the determination result of the second determining unit is a connection failure, return an error response to the main control module through the serial port, and end;
the first generation sending unit is used for generating a response of the connection service request according to the internet protocol address and the port number of the service and the TCP channel identifier when the judgment result of the first judgment unit is that the connection is successful, and sending the response of the connection service request to the main control module through the serial port;
a third judging unit, configured to judge whether a TCP channel identifier in the request for closing service connection is legal or not when the type of the judgment request of the first judging unit is a request for closing service;
a connection closing unit, configured to close, when the determination result of the third determining unit is legal, the connection with the service corresponding to the TCP channel identifier according to the TCP channel identifier in the service connection closing request;
the second generation and sending unit is used for forming a response of the service connection closing request according to the TCP channel identifier in the service connection closing request after the connection of the service corresponding to the TCP channel identifier is closed by the connection closing unit according to the TCP channel identifier in the service connection closing request, and sending the response of the service connection closing request to the main control module through the serial port; the third judging unit is used for judging whether the judgment result of the third judging unit is illegal or not;
a fourth judging unit, configured to judge whether a TCP channel identifier in the request for writing data to the service is valid when the first judging unit judges that the type of the request is the request for writing data to the service;
the data writing unit is used for writing data to the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for writing data to the service and the data to be written when the judgment result of the fourth judgment unit is legal, so as to obtain a data writing result;
the third generation sending unit is used for returning an error response to the main control module through the serial port when the judgment result of the fourth judgment unit is illegal; the data writing device is also used for forming a response of the data writing request to the service by the byte number of the successfully written data to the service and the TCP channel identifier in the data writing result when the data writing result is successful, and sending the response of the data writing request to the service to the main control module through the serial port; the main control module is also used for returning an error response to the main control module through the serial port when the data writing result is failure;
a fifth judging unit, configured to judge whether a TCP channel identifier in the request for reading data from the service is legal or not when the first judging unit judges that the type of the request is a request for reading data from the service;
a data reading unit, configured to read data to the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for reading data from the service and the byte number of the data to be read, when the result of the judgment by the fifth judging unit is legal;
a sixth judging unit, configured to judge whether the read data unit obtains a result of reading data within a predetermined time; the data reading device is also used for judging whether the data reading result is successful or not;
the fourth generation sending unit is used for returning an error response to the main control module through the serial port when the judgment result of the fifth judgment unit is illegal; the sixth judging unit is used for judging whether the data reading unit obtains the data reading result within the preset time and whether the data reading result is successful, forming a response of the request for reading the data from the service according to the byte number of the successfully read data in the data reading result, the read data and the TCP channel identifier, and sending the response of the request for reading the data from the service to the main control module through the serial port; and the sixth judging unit is used for returning an error response to the main control module through the serial port when the sixth judging unit judges that the data reading unit obtains the data reading result within the preset time and the data reading result is failure.
The invention has the beneficial effects that: the invention provides a method and a device for stable transmission of TCP communication, which realize the ordered transmission of parallel TCP transmission and ensure the correct transmission of data and the stability in the transmission process by setting a channel identifier.
Drawings
Fig. 1 is a flowchart of a method for stable transmission of TCP communication according to an embodiment of the present invention;
fig. 2, fig. 3, fig. 4, fig. 5, and fig. 6 are flowcharts of a method for stable transmission of TCP communication according to a second embodiment of the present invention;
fig. 7 is a block diagram of an apparatus for stable transmission of TCP communication according to a third embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
Example one
The embodiment provides a method for stable transmission of TCP communication, as shown in fig. 1, including the following steps:
step 101, the WIFI module receives a request sent by the main control module through a serial port, judges the type of the request,
when the type of the request is a connection service request, step 102 and step 103 are executed:
102, the WIFI module obtains a TCP channel identifier corresponding to an internet protocol address and a port number of a service in the connection service request, establishes connection with the service according to the internet protocol address and the port number of the service in the connection service request and the TCP channel identifier, judges whether the connection is successful, executes step 3 if the connection is successful, closes resources corresponding to the TCP channel identifier if the connection is failed, returns an error response to the main control module through a serial port, and ends;
103, the WIFI module generates a response of the connection service request according to the internet protocol address and the port number of the service and the TCP channel identifier, and sends the response of the connection service request to the main control module through the serial port;
when the type of the request is a request for closing service connection, step 104 is executed:
104, judging whether the TCP channel identifier in the service connection closing request is legal or not by the WIFI module, if so, closing the connection of the service corresponding to the TCP channel identifier according to the TCP channel identifier in the service connection closing request, forming a response of the service connection closing request according to the TCP channel identifier, sending the response of the connection closing request to the main control module through the serial port, and if not, returning an error response to the main control module through the serial port;
when the type of the request is a request to write data to a service, step 105 is performed:
step 105, the WIFI module judges whether a TCP channel identifier in the request for writing data to the service is legal or not, and if the TCP channel identifier is illegal, an error response is returned to the main control module through the serial port; if the data writing request is legal, writing data to the service corresponding to the TCP channel identifier according to the TCP channel identifier in the data writing request for the service and the data to be written, obtaining a data writing result, enabling the WIFI module to enable the number of bytes of the data successfully written to the service in the data writing result and the TCP channel identifier to form a response of the data writing request for the service when the data writing result is successful, sending the response of the data writing request for the service to the main control module through the serial port, and enabling the WIFI module to return an error response to the main control module through the serial port when the data writing result is failed;
when the type of the request is a request to read data from a service, step 106 is performed:
step 106, the WIFI module judges whether a TCP channel identifier in the request for reading data from the service is legal or not, and if the TCP channel identifier is illegal, an error response is returned to the main control module through the serial port; if the data reading request is legal, reading data from the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for reading the data from the service and the byte number of the data to be read, judging whether a result of reading the data is obtained within a preset time, if the result of reading the data is not obtained within the preset time, returning a response of reading overtime to the main control module through the serial port, if the result of reading the data is obtained within the preset time, and if the result of reading the data is successful, forming a response of the request for reading the data from the service by the WIFI module according to the byte number of the data successfully read in the result of reading the data, the read data and the TCP channel identifier, and sending the response of the request for reading the data from the service to the main control module through the serial port; and when the result of reading the data is failure, the WIFI module returns an error response to the main control module through the serial port.
In this embodiment, step 102 specifically includes: the WIFI module obtains a TCP channel identifier corresponding to the Internet protocol address and the port number of the service in the connection service request, judges whether the TCP channel identifier is legal or not,
if the connection is legal, establishing connection with the service according to the Internet protocol address and the port number of the service in the connection service request and the TCP channel identifier, judging whether the connection is successful, if the connection is successful, executing step 103, if the connection is failed, closing the resource corresponding to the TCP channel identifier, returning an error response to the main control module through the serial port, and ending;
if the serial port is illegal, an error response is returned to the main control module through the serial port, and the operation is finished.
In this embodiment, step 102 specifically includes:
102-1, calling a socket creating function by the WIFI module to obtain a first return value of the socket creating function, wherein the first return value of the socket creating function is a TCP channel identifier corresponding to an Internet protocol address and a port number of a service in the connection service request;
102-2, the WIFI module takes the internet protocol address and the port number of the service and a first return value of the socket creating function as input parameters, calls the connection function and obtains the return value of the connection function; the return value of the connection function is judged,
if the return value of the connect function is a connect success, step 102-3 is performed,
if the return value of the connection function is connection failure, the WIFI module calls a closing function, takes the first return value of the socket creating function as an input parameter, closes the resource corresponding to the TCP channel identifier, returns an error response to the main control module through the serial port, and then the process is finished;
and step 102-3, the WIFI module generates a response of the connection service request according to the internet protocol address and the port number of the service and the first return value of the socket creating function, and sends the response of the connection service request to the main control module through the serial port.
In this embodiment, in step 104, the step of determining, by the WIFI module, whether the TCP channel identifier in the request for closing the service connection is legal specifically includes: and the WIFI module judges whether the value of the TCP channel identifier in the service connection closing request is larger than or equal to a preset value, if so, the value is legal, and if not, the value is illegal.
In this embodiment, in step 104, closing the service connection corresponding to the TCP channel identifier according to the TCP channel identifier in the request for closing the service connection, forming a response for closing the service connection request according to the TCP channel identifier, and sending the response for closing the connection request to the main control module specifically includes: and the WIFI module takes the TCP channel identifier as an input parameter, calls the closing function to obtain a return value of the closing function, and forms a response of the service connection closing request according to the TCP channel identifier and the return value of the closing function.
In this embodiment, in step 105, if the data is legal, the data is written to the service corresponding to the TCP channel identifier according to the TCP channel identifier and the data to be written in the request for writing data to the service, so as to obtain a data writing result, when the data writing result is successful, the WIFI module composes, from the data writing result, the number of bytes of the data successfully written to the service and the TCP channel identifier into a response to the request for writing data to the service, and sends the response to the request for writing data to the service to the main control module through the serial port, and when the data writing result is unsuccessful, the WIFI module returns an error response to the main control module through the serial port, where the error response:
if the data is legal, the WIFI module takes a TCP channel identifier and data to be written in the data writing request to the service as input parameters, calls a writing function to obtain a return value of the writing function, and when the return value of the writing function is successful, the WIFI module enables the number of bytes of the data successfully written in the service in the return value of the writing function and the TCP channel identifier to form a response of the data writing request to the service, and sends the response of the data writing request to the main control module through a serial port; and when the return value of the write function is failure, the WIFI module returns an error response to the main control module through the serial port.
In this embodiment, in step 106, if legal, according to the TCP channel identifier in the request for reading data from the service and the byte number of the data to be read, reading data from the service corresponding to the TCP channel identifier, and determining whether a result of reading the data is obtained within a predetermined time, if the result of reading the data is not obtained within the predetermined time, returning a response of reading timeout to the main control module through the serial port, if the result of reading the data is obtained within the predetermined time, and when the result of reading the data is successful, the WIFI module composes a response to the request for reading data from the service according to the byte number of the data successfully read in the result of reading the data, the read data, and the TCP channel identifier, and sends the response to the request for reading data from the service to the main control module through the serial port; when the result of reading the data is failure, the WIFI module returns an error response to the main control module through the serial port, specifically:
if the request is legal, the WIFI module takes a TCP channel identifier in the request for reading data from the service and the byte number of the data to be read as input parameters, calls a read function and judges whether a return result of the read function is obtained within a preset time or not, if the return result of the read function is not obtained within the preset time, returns a response of reading overtime to the main control module through a serial port, if the return result of the read function is obtained within the preset time, and when the return result of the read function is successful, the WIFI module forms a response of the request for reading data from the service according to the byte number of the data successfully read in the return result of the read function, the read data and the TCP channel identifier, and sends the response of the request for reading data from the service to the main control module through the serial port; and when the result of the returned result of the read function is failure, the WIFI module returns an error response to the main control module through the serial port.
In this embodiment, the type of the WIFI module determination request is specifically: the WIFI module judges the type of the request according to the type identifier in the request, and when the identifier of the request is a preset connection request identifier, the type of the request is a connection service request; when the request identifier is a preset write data request identifier, the request type is a write data request; when the request identifier is a preset read data request identifier, the request type is a read data request; and when the type of the request is the service connection closing identifier, the type of the request is a service connection closing request.
Example two
The embodiment provides a method for stable transmission of TCP communication, which is applicable to an internet of things device including a 2G main control module and a WIFI module, and as shown in fig. 2 to 6, the method specifically includes:
step 201, the 2G main control module sends a first service connection request to the WIFI module through the serial port;
in this embodiment, the 2G master control module sends a first service connection request to the WIFI module, where the first service connection request includes a connection request identifier and an internet protocol address and a port number of a first service;
specifically, the 2G master control module sends the first service connection request to the WIFI module through the serial port may be: AT + TCP _ CONNECT _ REQ =192.168.1.101:6001\ r \ n.
The connection request identifier is a preset identifier, and specifically includes: AT + TCP _ CONNECT _ REQ; the internet protocol address and port number of the first service are specifically host1 and port 1.
202, the WIFI module receives a first service connection request sent by the main control module through a serial port, obtains an internet protocol address and a port number of a first service after analyzing the first service request, and calls a first network communication API function to obtain a first return value of the first network communication API function;
in this embodiment, the first network communication API function is a socket function, and the step specifically includes: the WIFI module receives a first service connection request, analyzes the first service connection request to obtain an internet protocol address host1 and a port number port1 of a first service, calls a socket function and obtains a socket function first return value socket fd 1;
in this embodiment, this step may further include: and the WIFI module judges whether the first return value of the first network communication API function is legal or not, if so, step 203 is executed, and if not, the WIFI module returns an error response to the 2G main control module through the serial port.
Step 203, the WIFI module calls a second network communication API function, sends a connection request to the first service, and obtains a second network communication API function return value returned by the first service;
in this embodiment, the second network communication API function is a connect function, and the WIFI module calls the connect function from the internet protocol address host1 and the port number port1 of the first service and the first return value socket fd1 of the first network communication API function, and sends a connection request to the first service to obtain a return value of the second network communication API function;
and the WIFI module judges whether the connection of the first service is successful or not according to the return value of the second network communication API function, if so, step 204 is executed, if not, a fifth network communication API function is called, the first return value of the first network communication API function is used as an input parameter, the resource corresponding to the first return value of the first network communication API function is closed, and step 204 is executed.
In this embodiment, the second network communication API function return value may be a return value indicating that the connection with the first service is successful, or may be a return value indicating that the connection with the first service is failed.
Specifically, the return value of the second network communication API function is: when 0, it indicates that the connection with the first service is successful, and the return value of the second network communication API function is: -1, identifying a failure to connect to the first service.
Specifically, in this embodiment, the fifth network API function is a Close function.
Step 204, the WIFI module generates a response of connecting the first service request according to the connection response identifier, the internet protocol address and the port number of the first service, the first network communication API function first return value and the second network communication API function return value, and sends the response of connecting the first service request to the 2G master control module;
wherein, the connection response mark is a preset mark.
The method comprises the following specific steps: the WIFI module generates a response of connecting the first service request according to the connection response identifier, the Internet protocol address and the port number host1/port1 of the first service, the first return value socket fd1 of the first network communication API function and the return value of the second network communication API function, and sends the response of connecting the first service request to the 2G master control module;
in this step, the WIFI module may also generate a response to connect the first service request according to the connection response identifier, the internet protocol address and the port number host1/port1 of the first service, the first return value of the first network communication API function, the return value of the second network communication API function, and the erron, and send the response to connect the first service request to the 2G master control module;
specifically, the response of the connection first service request is: TCP _ CONNECT _ RESP =192.168.1.101:6001,1,0,0, "Success" \ r \ n.
Specifically, when the value of erron is 0, the connection is successful, and when the value of erron is other values, the connection is failed.
Step 205, the 2G master control module corresponds the internet protocol address and the port number of the first service to the first return value of the first network communication API function, and uses the first return value of the first network communication API function as the first TCP channel identifier;
in this embodiment, the 2G master control module corresponds the internet protocol address and the port number host1/port1 of the first service to the first return value socket fd1 of the first network communication API function, and takes the first return value of the first network communication API function as the first TCP channel identifier;
the method also comprises the following steps: the 2G master control module determines whether the second network communication API function return value in the response to the first service connection request is a return value indicating that the first service connection is successful, if so, performs step 205, and if not, ends the operation.
Step 206, the 2G main control module sends a second service connection request to the WIFI module through the serial port;
in this embodiment, the 2G master control module sends a second service connection request to the WIFI module, where the second service connection request includes a connection request identifier and an internet protocol address and a port number of the second service;
specifically, the 2G master control module sends the connection second service request to the WIFI module through the serial port may be: AT + TCP _ CONNECT _ REQ =192.168.1.102:6002\ r \ n.
The connection request identifier is a preset identifier, and specifically includes: AT + TCP _ CONNECT _ REQ; the internet protocol address and port numbers of the second service are specifically host2 and port 2.
Step 207, the WIFI module receives a second service connection request sent by the 2G master control module through the serial port, obtains an internet protocol address and a port number of the second service after analyzing the second service request, and calls the first network communication API function to obtain a second return value of the first network communication API function;
in this embodiment, the first network communication API function is a socket function, in this step, the WIFI module receives a connection second service request, obtains an internet protocol address and a port number of the second service after analyzing the connection second service request, calls the socket function, and obtains a socket function second return value socket fd 2;
in this embodiment, this step may further include: and the WIFI module judges whether the second return value of the first network communication API function is legal or not, if so, step 208 is executed, and if not, the WIFI module returns an error response to the 2G main control module through the serial port.
Step 208, the WIFI module calls a second network communication API function, sends a connection request to the second service, and obtains a second network communication API function return value returned by the second service;
in this embodiment, the second network communication API function is a connect function, and the WIFI module calls the connect function from the internet protocol address and the port number of the second service and the second return value socket fd2 of the first network communication API function, and sends a connection request to the second service, so as to obtain a second network communication API function return value;
the second network communication API function return value may be a return value of a connection success or a return value of a connection failure.
Specifically, the return value of the second network communication API function is: when 0, it represents that the connection with the second service is successful, and the return value of the second network communication API function is: and 1, indicating that the second service is failed to be connected.
In this embodiment, the second network communication API function return value is a return value of successful connection.
Step 209, the WIFI module generates a response to connect the second service request according to the connection response identifier, the internet protocol address and the port number of the second service, the second return value of the first network communication API function, and the return value of the second network communication API function, and sends the response to connect the second service request to the 2G master control module through the serial port;
wherein, the connection response mark is a preset mark.
The method comprises the following specific steps: the WIFI module generates a response of connecting a second service request according to the connection response identifier, the Internet protocol address and the port number host2/port2 of the second service, the second return value socket fd2 of the first network communication API function and the return value of the second network communication API function, and sends the response of connecting the second service request to the 2G master control module;
specifically, the response of the connection second service request is: TCP _ CONNECT _ RESP =192.168.1.102:6002,2,0,0, "Success" \ r \ n.
In this step, the WIFI module may also generate a response to connect the second service request according to the connection response identifier, the internet protocol address and the port number host2/port2 of the second service, the second return value of the first network communication API function, the return value of the second network communication API function, and the erron, and send the response to connect the second service request to the 2G master control module;
specifically, when the value of erron is 0, the connection is successful, and when the value of erron is other values, the connection is failed.
Step 210, the 2G master control module corresponds the internet protocol address and the port number of the second service to the second return value of the first network communication API function, and takes the second return value of the first network communication API function as the second TCP channel identifier;
in this embodiment, the 2G master control module corresponds the internet protocol address and the port number host2/port2 of the second service to the first network communication API function second return value socket fd2, and uses the first network communication API function second return value as the second TCP channel identifier;
step 211, the 2G master control module sends a request for writing data to the first service to the WIFI module through the serial port;
in this embodiment, the 2G master control module sends a request for writing data to the first service to the WIFI module, where the request for writing data to the first service includes a data writing request identifier, a first TCP channel identifier socket fd1, data to be written, and the number of bytes of the data to be written.
Specifically, the sending of the data writing request to the first service by the 2G master control module to the WIFI module is: AT + TCP _ WRITE _ REQ =1,4:01020304\ r \ n, wherein the WRITE data request is identified as: AT + TCP _ WRITE _ REQ, the first TCP channel identified as socket fd1, the data to be written is: 0x01,0x02,0x03,0x04, the number of bytes of data to be written is: 4.
wherein, the writing data request mark is a preset mark.
Step 212, after receiving the request for writing data to the first service, the WIFI module calls a third network communication API function to obtain a third return value of the third network communication API function;
in this embodiment, the third network communication API function is a write function;
in this step, after receiving a request for writing data to the first service, the WIFI module calls a Write function, and the data to be written and the number of bytes of the data to be written are used as incoming parameters to obtain a third return value of the third network communication API function;
specifically, after receiving a request for writing data to the first service, the WIFI module calls a Write function, where data to be written, 0x01,0x02,0x03,0x04, and the number of bytes of data to be written, are used as incoming parameters, and a third return value of the third network communication API function is obtained: 4.
and the third network communication API function third return value is the number of bytes of the data successfully written by the first service.
The method comprises the following specific steps: the WIFI module receives a request for writing data to the first service, after the request for writing data to the first service is analyzed, whether the value of a first TCP channel identifier socket fd1 in the first service writing data is legal or not is judged, if yes, a third network communication API function is called to obtain a third return value of the third network communication API function, and if not, a response of parameter errors is returned to the 2G main control module.
In this step, determining whether the value of the first TCP channel identifier socket fd1 in the first service write data is legal specifically includes: and judging whether the value of the first TCP channel identifier fd1 is greater than or equal to a preset value 0, if so, calling a third network communication API function to obtain a third return value of the third network communication API function, and if not, returning a parameter error response to the 2G main control module.
Step 213, the WIFI module responds to the write data identifier according to the third return value of the third network communication API function, and the first TCP channel identifier socket fd1 constitutes a response to write data to the first service, and sends the response to write data to the first service to the 2G master control module through the serial port;
in this embodiment, the write data response flag is a preset flag.
The method comprises the following specific steps: the WIFI module forms a response for writing data to the first service according to the number of bytes of the successfully written data of the first service, the write data response identifier and the first TCP channel identifier socket fd1, and sends the response for writing data to the first service to the 2G master control module;
specifically, the response of writing data to the first service is: TCP _ WRITE _ RESP =1,4,0, "Success" \ r \ n, where TCP _ WRITE _ RESP identifies the WRITE data response.
In this embodiment, the step may further include: the WIFI module writes a data response identifier according to a third return value of a third network communication API function, and the first TCP channel identifiers socket fd1 and errno generate a response for writing data to the first service;
step 214, the 2G master control module sends a request for reading data from the first service to the WIFI module through the serial port;
in this embodiment, the 2G master control module sends a request for reading data from the first service to the WIFI module, where the request for reading data from the first service includes a read data request identifier, a first TCP channel identifier socket fd1, and a number of bytes of data to be read.
Wherein the read data request identification is a preset identification.
Specifically, the request for reading data from the first service is as follows: AT + TCP _ READ _ REQ =1,6,9000\ r \ n;
wherein the read data request identification is: AT + TCP _ READ _ REQ, the number of bytes of data to be READ is: 6.
in this embodiment, the sending of the data reading request to the first service by the 2G master control module to the WIFI module may further include timeout.
Step 215, after receiving the request for reading data from the first service, the WIFI module invokes a fourth network communication API function to obtain a fourth return result of the fourth network communication API function;
in this embodiment, the fourth network communication API function is a read function;
in this step, after receiving a request for reading data from the first service, the WIFI module calls a Read function, and the number of bytes of the data to be Read is used as an incoming parameter to obtain a fourth return result of the Read function;
the fourth return result of the Read function comprises the byte number of the data successfully Read by the first service and the Read data.
Specifically, the fourth returned result of the Read function is: 0xA5,0xA6,0xA7,0xA8,0xA9,0xA and 6.
In this embodiment, the fourth returned result of the fourth network communication API function may also be that data reading fails, and when the fourth returned value of the fourth network communication API function is that data reading fails, the WIFI module returns an error response to the 2G master control module through the serial port.
Specifically, when the fourth returned result of the Read function is-1, it indicates that data reading fails, and the WIFI module returns an error response to the 2G main control module through the serial port: TCP _ READ _ RESP =1, -1,104, "Connection reset by peer": r \ n.
In this embodiment, the step may further include: and after receiving the request for reading data from the first service, the WIFI module calls a fourth network communication API function, and judges whether a fourth returned result of the fourth network communication API function is obtained within a preset time, if so, step 216 is executed, and if not, the WIFI module returns a response of reading timeout to the 2G main control module through the serial port.
Step 216, the WIFI module forms a response of reading data to the first service according to a fourth returned result, the read data response identifier, and the first TCP channel identifier socket fd1 according to the fourth network communication API function, and sends the response of reading data to the first service to the 2G master control module;
in this embodiment, the read data response flag is a preset flag.
The method comprises the following specific steps: the WIFI module forms a response for reading data from the first service according to the byte number of the successfully read data of the first service, the read data response identifier and the first TCP channel identifier socket fd1, and sends the response for reading data from the first service to the 2G master control module;
specifically, the WIFI module, according to byte number 6 of the data successfully READ by the first service, READ data 0xA5,0xA6,0xA7,0xA8,0xA9,0xAA, READ data response identifier TCP _ READ _ RESP, and first TCP channel identifier socket 1, forms a response TCP _ READ _ RESP =1,6,0, "Success": A5A6A7A8A9AA \ r \ n for reading data from the first service.
In this embodiment, the step may further include: the WIFI module generates a response of reading data to the first service according to a fourth returned result of the fourth network communication API function, the read data response identifier, the first TCP channel identifier socket fd1 and the errno, and sends the response of reading data to the first service to the 2G master control module;
step 217, the 2G main control module sends a request for writing data to the second service to the WIFI module through the serial port;
in this embodiment, the 2G master control module sends a data write request to the second service to the WIFI module, where the data write request to the second service includes a data write request identifier, a second TCP channel identifier socket fd2, data to be written, and the number of bytes of the data to be written.
Specifically, the request for writing data to the second service is: AT + TCP _ WRITE _ REQ =2,4:11121314\ r \ n. The data writing request to the second service comprises a data writing request identifier AT + TCP _ WRITE _ REQ, a second TCP channel identifier socketfd2, data to be written 0x11,0x12,0x13,0x14, and the number of bytes of data to be written is 4.
Step 218, after receiving the request for writing data to the second service, the WIFI module calls a third network communication API function, the second service executes the data writing operation, and returns a data writing return value to the WIFI module, and the WIFI module obtains a third return value of the third network communication API function according to the data writing return value;
in the present embodiment, the third network communication API function is a Write function;
in this step, after receiving a request for writing data to the second service, the WIFI module calls a Write function, and the data to be written and the number of bytes of the data to be written are used as incoming parameters to obtain a third return value of the third network communication API function;
the third network communication API function third return value is the number of bytes of the data successfully written by the second service.
Specifically, the third network communication API function third return value is: 4.
step 219, the WIFI module writes a data response identifier according to a third return value of the third network communication API function, and the second TCP channel identifier socket fd2 constitutes a response for writing data to the second service, and sends the response for writing data to the second service to the 2G master control module;
the method comprises the following specific steps: the WIFI module forms a response for writing data to the second service according to the number of bytes of the data successfully written by the second service, the write data response identifier and the second TCP channel identifier socket fd2, and sends the response for writing data to the second service to the 2G main control module;
in this embodiment, the step may further include: the WIFI module writes a data response identifier, a second TCP channel identifier socket fd2 and an errno according to a third return value of a third network communication API function, generates a response for writing data to the second service, and sends the response for writing data to the second service to the 2G main control module;
specifically, the WIFI module WRITEs a data response identifier TCP _ WRITE _ RESP according to a third return value of 4 of the third network communication API function, and the second TCP channel identifiers socket fd2 and errno generate a response TCP _ WRITE _ RESP =2,4,0, "Success" \ r \ n for writing data to the second service.
Step 220, the 2G main control module sends a request for reading data to the second service to the WIFI module through the serial port;
in this embodiment, the 2G master control module sends a data read request to the second service to the WIFI module, where the data read request to the second service includes a data read request identifier, a second TCP channel identifier socket fd2, and the number of bytes of data to be read.
Specifically, the sending of the data reading request to the second service by the 2G master control module to the WIFI module is: AT + TCP _ READ _ REQ =2,6,9000\ r \ n, where the READ data request is identified as AT + TCP _ READ _ REQ and the number of bytes of data to be READ is 6.
Step 221, after receiving the request for reading data from the second service, the WIFI module calls a fourth network communication API function, and the WIFI module obtains a fourth return result of the fourth network communication API function;
in this embodiment, the fourth network communication API function is a read function;
in this step, after receiving a request for reading data from the second service, the WIFI module calls an xTaskCreate function to create a task, then calls a Read function, the number of bytes of the data to be Read is used as an incoming parameter, and the WIFI module obtains a fourth returned result of the fourth network communication API function;
the fourth network communication API function returns a fourth result including the number of bytes of the data successfully read by the second service and the read data.
Specifically, the fourth network communication API function returns a fourth result: 0xB5,0xB6,0xB7,0xB8,0xB9,0xBA and 6, wherein the number of bytes of data successfully read by the second service is 6, and the data read is 0xB5,0xB6,0xB7,0xB8,0xB9,0 xBA.
In this embodiment, the fourth returned result of the fourth network communication API function may also be that data reading fails, and when the fourth returned result of the fourth network communication API function is that data reading fails, the WIFI module returns an error response to the 2G master control module through the serial port.
Specifically, when the fourth network communication API function returns a result of-1, the identifier fails to read the data, and the error response returned by the WIFI module to the 2G master control module through the serial port is as follows: TCP _ READ _ RESP =2, -1,104, "Connectionreset by peer": r \ n.
In this embodiment, the step may further include: and after receiving the request for reading data from the first service, the WIFI module calls a fourth network communication API function, and determines whether a fourth returned result of the fourth network communication API function is obtained within a predetermined time, if so, step 222 is executed, and if not, the WIFI module returns a response of reading timeout to the 2G master control module through the serial port.
Step 222, the WIFI module reads the data response identifier and the second TCP channel identifier socket fd2 according to the fourth returned result of the fourth network communication API function, to form a response for reading data to the second service, and sends the response for reading data to the second service to the 2G master control module through the serial port;
in this embodiment, the read data response flag is a preset flag.
The method comprises the following specific steps: the WIFI module reads a response of reading data to the second service according to the byte number of the successfully read data of the second service, the read data response identifier and the second TCP channel identifier socket fd2, and sends the response of reading data to the second service to the 2G main control module through the serial port;
in this embodiment, the step may further include: the WIFI module reads the data response identifier, the second TCP channel identifier socket fd2 and the errno according to a fourth returned result of the fourth network communication API function, generates a response for reading data to the second service, and sends the response for reading data to the second service to the 2G main control module through the serial port;
specifically, the WIFI module returns a result according to a fourth network communication API function: 0xB5,0xB6,0xB7,0xB8,0xB9,0xBA, and 6, read data response identification: TCP _ READ _ RESP, second TCP channel identification socket fd2 and errno: 0 generates a response to the second service read data: TCP _ READ _ RESP =2,6,0, "Success": B5B6B7B8B9BA \ r \ n, and sends the response of reading data to the second service to the 2G master control module through the serial port.
The method comprises the following specific steps: the WIFI module performs base64 coding on the read data in the fourth return value of the fourth network communication API function to obtain coded read data, a response for reading data of the second service is formed according to the coded read data, the byte number of the successfully read data in the fourth return value of the fourth network communication API function, the read data response identifier, the second TCP channel identifier socket fd2 and the errno, the response for reading data of the second service is sent to the 2G main control module, and the vTaskDelete () function is called to delete a task.
Step 223, the 2G master control module sends a request for closing the first service connection to the WIFI module through the serial port;
in this embodiment, the 2G master control module sends a request for closing the first service connection to the WIFI module, where the request for closing the first service connection includes a connection closing request identifier and a first TCP channel identifier socket fd 1;
wherein, the connection closing request mark is a preset mark.
Specifically, the request for closing the first service connection is: AT + TCP _ DISCONNECT _ REQ =1\ r \ n, wherein the connection closing request mark is as follows: AT + TCP _ DISCONNECT _ REQ.
Step 224, the WIFI module receives the request for closing the first service connection, calls a fifth network communication API function, and sends a closing request to the first service, so as to obtain a fifth return value of the fifth network communication API function;
in this embodiment, the fifth network communication API function is a Close function;
in the step, the WIFI module receives a request for closing the first service connection, calls a close function, and obtains a fifth return value of the network communication API function;
the fifth return value of the network communication API function is closing success or failure;
specifically, when the fifth return value of the network communication API function is 0, the closing is successful; and when the fifth return value of the network communication API function is-1, indicating that the closing fails.
In this embodiment, the closing operation return value is a closing success.
Step 225, the WIFI module closes the connection response identifier according to the fifth return value of the network communication API function, the first TCP channel identifier socket fd1 constitutes a response to close the first service connection request, and the response to close the first service connection is sent to the 2G master control module through the serial port;
in this embodiment, the steps specifically include: the WIFI module closes the connection response identifier according to a fifth return value of the network communication API function, the first TCP channel identifier socket fd1 forms a response for closing the first service connection, and the response for closing the first service connection request is sent to the 2G main control module;
wherein, the closing connection response mark is a preset mark;
in this embodiment, the step may further include: and the WIFI module closes the connection response identifier according to the fifth return value of the network communication API function, the first TCP channel identifier socket fd1 and the errno form a response for closing the first service connection request, and the response for closing the first service connection request is sent to the 2G main control module.
Specifically, the WIFI module closes the connection response identifier TCP _ DISCONNECT _ RESP according to the fifth return value 0 of the network communication API function, and the first TCP channel identifier socket fd1 and errno form a response to close the first service connection request: TCP _ DISCONNECT _ RESP =1,0,0, "Success" \ r \ n.
Step 226, the 2G master control module sends a request for closing the second service connection to the WIFI module through the serial port;
in this embodiment, the 2G master control module sends a request for closing the second service connection to the WIFI module, where the request for closing the second service connection includes a connection closing request identifier and a second TCP channel identifier socket fd 2;
specifically, the request for closing the second service connection is: AT + TCP _ DISCONNECT _ REQ =2\ r \ n, including a connection close request identification AT + TCP _ DISCONNECT _ REQ and a second TCP channel identification socket fd 2.
Step 227, the WIFI module receives the request for closing the second service connection, calls a fifth network communication API function, and sends the request for closing the second service connection to the second service, so as to obtain a fifth return value of the fifth network communication API function;
in this embodiment, the fifth network communication API function is a Close function;
in the step, the WIFI module receives a request for closing the second service connection, calls a Close function, and obtains a fifth return value of the network communication API function;
the closing operation return value is closing success or closing failure;
in this embodiment, the closing operation return value is a closing success.
Specifically, the fifth return value of the network communication API function may be: 0 indicates success, and the fifth return value of the network communication API function may also be: -1 indicates a shutdown failure.
In step 228, the WIFI module closes the connection response identifier according to the fifth return value of the network communication API function, the second TCP channel identifier forms a response for closing the second service connection request, and the response for closing the second service connection request is sent to the 2G main control module through the serial port.
In this embodiment, the steps specifically include: and closing successfully, closing the connection response identifier, wherein the second TCP channel identifier socket fd2 constitutes a response for closing the second service connection request, and sends the response for closing the second service connection request to the 2G master module.
In this embodiment, the step may further include: and the WIFI module closes the connection response identifier according to the fifth return value of the network communication API function, the second TCP channel identifier socket fd2 and the errno form a response for closing the second service connection request, and the response for closing the second service connection request is sent to the 2G main control module.
Specifically, the WIFI module closes the connection response identifier TCP _ DISCONNECT _ RESP, and the second TCP channel identifiers socket fd2 and errno according to the fifth return value 0 of the network communication API function: 0 constitutes a response to close the second service connection request: TCP _ DISCONNECT _ RESP =2,0,0, "Success" \ r \ n.
EXAMPLE III
The present embodiment provides an apparatus for TCP communication stable transmission, as shown in fig. 7, including: WIFI module 31 and host system 32, wherein WIFI module 31 includes:
a receiving unit 3101, configured to receive, through a serial port, a request sent by the main control module 32;
a first judging unit 3102 for judging the type of the request received by the receiving unit 3101;
an obtaining unit 3103, configured to obtain, when the type of the determination request of the first determining unit 3102 is a connection service request, a TCP channel identifier corresponding to an internet protocol address and a port number of a service in the connection service request;
a connection unit 3104, configured to establish a connection with a service according to an internet protocol address and a port number of the service in the connection service request and a TCP tunnel identifier;
a second judging unit 3105 for judging whether the connection unit 3104 is successfully connected to the service;
a closing end unit 3106, configured to close the resource corresponding to the TCP channel identifier when the determination result of the second determination unit 3105 is that the connection fails, return an error response to the main control module 32 through the serial port, and end;
a first generating and sending unit 3107, configured to generate a response to the connection service request according to the internet protocol address and the port number of the service and the TCP channel identifier when the determination result of the first determining unit 3102 is that the connection is successful, and send the response to the connection service request to the main control module 32 through the serial port;
a third judging unit 3108, configured to, when the type of the judgment request of the first judging unit 3102 is a request for closing the service, judge whether a TCP channel identifier in the request for closing the service connection is legal;
a connection closing unit 3109, configured to close, when the determination result of the third determining unit 3108 is legal, the connection of the service corresponding to the TCP tunnel identifier according to the TCP tunnel identifier in the service connection closing request;
a second generating and sending unit 3110, configured to, after the closing connection unit 3109 closes the connection of the service corresponding to the TCP channel identifier according to the TCP channel identifier in the service connection closing request, form a response to the service connection closing request according to the TCP channel identifier in the service connection closing request, and send the response to the service connection closing request to the main control module 32 through the serial port; the third judging unit 3108 is further configured to, when the judgment result is illegal, return an error response to the main control module 32 through the serial port;
a fourth judging unit 3111, configured to judge whether or not a TCP channel identifier in the request to write data to the service is valid when the first judging unit 3102 judges that the type of the request is the request to write data to the service;
a data writing unit 3112, configured to, when the determination result of the fourth determining unit 3111 is legal, write data to the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for writing data to the service and the data to be written, to obtain a data writing result;
a third generating and transmitting unit 3113, configured to return an error response to the main control module 32 through the serial port when the determination result of the fourth determining unit 3111 is illegal; the data writing module is further configured to, when the data writing result is successful, compose a response to the service data writing request by the byte number and the TCP channel identifier of the data successfully written to the service in the data writing result, and send the response to the service data writing request to the main control module 32 through the serial port; and is also used for returning an error response to the main control module 32 through the serial port when the data writing result is failure;
a fifth judging unit 3114, configured to, when the first judging unit 3102 judges that the type of the request is a request for reading data from a service, judge whether a TCP channel identifier in the request for reading data from the service is legal;
a read data unit 3115, configured to, when the result of the determination by the fifth determining unit 3114 is legal, read data from a service corresponding to the TCP tunnel id according to the TCP tunnel id in the request for reading data from the service and the number of bytes of data to be read;
a sixth determining unit 3116, configured to determine whether the read data unit 3115 obtains the result of reading the data within a predetermined time, and further determine whether the result of reading the data is successful;
a fourth generating and sending unit 3117, configured to return an error response to the main control module 32 through the serial port when the result of the judgment by the fifth judging unit 3114 is illegal; the sixth determining unit 3116 is further configured to, when the sixth determining unit 3116 determines that the result of reading data is obtained by the data reading unit 3115 within the predetermined time and the result of reading data is successful, form a response to the request for reading data from the service according to the number of bytes of successfully read data in the result of reading data, the read data, and the TCP channel identifier, and send the response to the request for reading data from the service to the main control module 32 through the serial port; and is further configured to return an error response to the main control module 32 through the serial port when the sixth determining unit 3116 determines that the read data unit 3115 obtains the result of reading data within the predetermined time and the result of reading data is a failure.
In this embodiment, the apparatus for TCP communication stable transmission further includes a seventh determining unit, configured to determine whether a TCP channel identifier in the connection service request is legal;
a connection unit 3104, configured to, when the seventh determining unit determines that the TCP tunnel identifier in the connection service request is valid, establish a connection with the service according to the TCP tunnel identifier and the internet protocol address and the port number of the service in the connection service request;
the first generating and sending unit 3107 is further configured to, when the seventh determining unit determines that the TCP channel identifier in the connection service request is not legal, return an error response to the main control module 32 through the serial port.
In this embodiment, the obtaining unit 3103 is specifically configured to, when the type of the determination request of the first determining unit 3102 is the connection service request, invoke a socket creating function to obtain a first return value of the socket creating function, where the first return value of the socket creating function is a TCP channel identifier corresponding to an internet protocol address and a port number of a service in the connection service request;
a connection unit 3104, specifically configured to call a connection function using an internet protocol address and a port number of a service and a first return value of a socket creation function as input parameters, to obtain a return value of the connection function;
a second judgment unit 3105 for judging a return value of the connection function;
a closing end unit 3106, configured to, when the second determination unit 3105 determines that the return value of the connection function is a connection failure, call the closing function, take the first return value of the socket creation function as an input parameter, close the resource corresponding to the TCP channel identifier, return an error response to the main control module 32 through the serial port, and end;
the first generating and transmitting unit 3107 generates a response of the connection service request according to the internet protocol address and the port number of the service, the first return value of the socket creation function, and transmits the response of the connection service request to the main control module 32 through the serial port, and transmits a response of closing the connection request to the main control module 32.
In this embodiment, the third determining unit 3108 is specifically configured to, when the type of the determination request of the first determining unit 3102 is a service closing request, determine whether a TCP channel identifier in the service closing connection request is greater than or equal to a preset value, if so, determine that the TCP channel identifier is legal, and if not, determine that the TCP channel identifier is illegal.
In this embodiment, the close connection unit 3109 is specifically configured to, when the determination result of the third determination unit 3108 is legal, call the close function with the TCP channel identifier in the close service connection request as an input parameter to obtain a return value of the close function;
a second generating and sending unit 3110, configured to, after the closing unit obtains the return value of the close function, form a response to the request for closing the service connection according to the TCP channel identifier in the request for closing the service connection and the return value of the close function, and send the response to the request for closing the service connection to the main control module 32 through the serial port.
In this embodiment, the data writing unit 3112 is specifically configured to, when the determination result of the fourth determining unit 3111 is legal, call the write function by using, as input parameters, a TCP channel identifier and data to be written in the request for writing data to the service, and obtain a return value of the write function;
a third generating and sending unit 3113, configured to return an error response to the main control module 32 through the serial port when the determination result of the fourth determining unit 3111 is illegal; the host is further configured to, when the return value of the write function is successful, form a response to the service write data request by the number of bytes of data successfully written to the service in the return value of the write function and the TCP channel identifier, and send the response to the service write data request to the main control module 32 through the serial port; and is further configured to return an error response to the main control module 32 through the serial port when the return value of the write function is a failure.
In this embodiment, the read data unit 3115 is specifically configured to, when the result of the determination by the fifth determining unit 3114 is legal, call a read function by using, as input parameters, a TCP channel identifier in the request for reading data from the service and the number of bytes of the data to be read;
the sixth determining unit 3116 is specifically configured to, after the read data unit 3115 invokes the read function, determine whether the read data unit obtains a return result of the read function within a predetermined time, and further determine whether the return result of the read function is successful;
a fourth generating and sending unit 3117, configured to return an error response to the main control module 32 through the serial port when the result of the judgment by the fifth judging unit 3114 is illegal; the sixth determining unit 3116 is further configured to, when the sixth determining unit 3115 determines that the read data unit 3115 obtains the return result of the read function within the predetermined time and the return result of the read function is successful, form a response to the request for reading data from the service according to the number of bytes of the data successfully read according to the return result of the read function, the read data, and the TCP channel identifier, and send the response to the request for reading data from the service to the main control module 32 through the serial port; and is further configured to return an error response to the main control module 32 through the serial port when the sixth determining unit 3116 determines that the read data unit 3115 obtains the return result of the read function within the predetermined time and the return result of the read function is a failure.
In this embodiment, the first determining unit 3102 is specifically configured to determine the type of the request according to the type identifier in the request, and when the identifier of the request is a preset connection request identifier, determine that the type of the request is a connection service request; when the request identifier is a preset data writing request identifier, judging that the type of the request is a data writing request; when the request identifier is a preset read data request identifier, judging that the type of the request is a read data request; and when the type of the request is the service connection closing identifier, judging that the type of the request is a service connection closing request.
The above description is only for the specific embodiments of the present invention, but the scope of the present invention is not limited thereto, and any person skilled in the art can easily conceive of the changes or substitutions within the technical scope of the present invention, and all the changes or substitutions should be covered within the scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.

Claims (16)

1. A method for stable transmission of TCP communication, the method comprises the following steps:
step 1, a WIFI module receives a request sent by a main control module through a serial port, judges the type of the request,
when the type of the request is a connection service request, executing the steps 2 to 3:
step 2, the WIFI module obtains a TCP channel identifier corresponding to an Internet protocol address and a port number of a service in the connection service request, establishes connection with the service according to the Internet protocol address and the port number of the service in the connection service request and the TCP channel identifier, judges whether the connection is successful, if the connection is successful, executes step 3, if the connection is failed, closes a resource corresponding to the TCP channel identifier, returns an error response to the main control module through a serial port, and ends;
step 3, the WIFI module generates a response of the connection service request according to the internet protocol address and the port number of the service and the TCP channel identifier, and sends the response of the connection service request to the main control module through a serial port;
when the type of the request is a request for closing service connection, executing the step 4:
step 4, the WIFI module judges whether the TCP channel identification in the service connection closing request is legal or not, if so, the WIFI module closes the connection of the service corresponding to the TCP channel identification according to the TCP channel identification in the service connection closing request, forms a response of the service connection closing request according to the TCP channel identification, sends the response of the service connection closing request to the main control module through a serial port, and if not, returns an error response to the main control module through the serial port;
when the type of the request is a request for writing data to a service, executing the step 5:
step 5, the WIFI module judges whether the TCP channel identifier in the request for writing data to the service is legal or not, and if the TCP channel identifier is illegal, an error response is returned to the main control module through the serial port; if the data writing request is legal, writing data to the service corresponding to the TCP channel identifier according to the TCP channel identifier in the data writing request and the data to be written, obtaining a data writing result, forming a response of the data writing request to the service by the byte number of the data successfully written to the service in the data writing result and the TCP channel identifier when the data writing result is successful, sending the response of the data writing request to the service to the main control module through a serial port, and returning an error response to the main control module through the serial port when the data writing result is failed;
when the type of the request is a request for reading data from a service, executing step 6:
step 6, the WIFI module judges whether the TCP channel identifier in the request for reading data from the service is legal or not, and if the TCP channel identifier is illegal, an error response is returned to the main control module through a serial port; if the data reading request is legal, reading data from the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for reading the data from the service and the byte number of the data to be read, judging whether a result of reading the data is obtained within a preset time, if the result of reading the data is not obtained within the preset time, returning a response of reading overtime to the main control module through a serial port, if the result of reading the data is obtained within the preset time, and if the result of reading the data is successful, forming a response of the request for reading the data from the service according to the byte number of successfully reading the data in the result of reading the data, the read data and the TCP channel identifier, and sending the response of the request for reading the data from the service to the main control module through the serial port; and when the result of reading the data is failure, returning an error response to the main control module through the serial port.
2. The method of claim 1,
the step 2 specifically comprises the following steps: the WIFI module obtains a TCP channel identifier corresponding to the Internet protocol address and the port number of the service in the connection service request, judges whether the TCP channel identifier is legal or not,
if the connection is legal, establishing connection with the service according to the Internet protocol address and the port number of the service in the connection service request and the TCP channel identifier, judging whether the connection is successful, if the connection is successful, executing the step 3, if the connection is failed, closing the resource corresponding to the TCP channel identifier, returning an error response to the main control module through the serial port, and ending;
and if the current state is illegal, returning an error response to the main control module through the serial port, and ending.
3. The method of claim 1,
the step 2 specifically comprises the following steps:
step 2-1, the WIFI module calls a socket creating function to obtain a first return value of the socket creating function, wherein the first return value of the socket creating function is a TCP channel identifier corresponding to an Internet protocol address and a port number of a service in the connection service request;
step 2-2, the WIFI module takes the internet protocol address and the port number of the service and the first return value of the socket creating function as input parameters, calls a connection function and obtains the return value of the connection function; the return value of the join function is determined,
if the return value of the join function is a join success, step 2-3 is performed,
if the return value of the connection function is connection failure, calling a closing function, taking the first return value of the socket establishing function as an input parameter, closing the resource corresponding to the TCP channel identifier, returning an error response to the main control module through a serial port, and ending;
and 2-3, the WIFI module generates a response of a connection service request according to the internet protocol address and the port number of the service and the first return value of the socket creating function, and sends the response of the connection service request to the main control module through a serial port.
4. The method according to claim 1, wherein in step 4, the step of determining, by the WIFI module, whether the TCP tunnel id in the request for closing service connection is legal specifically includes: and the WIFI module judges whether the value of the TCP channel identifier in the service connection closing request is larger than or equal to a preset value, if so, the value is legal, and if not, the value is illegal.
5. The method according to claim 1, wherein in step 4, closing the service connection corresponding to the TCP tunnel identifier according to the TCP tunnel identifier in the request for closing the service connection, forming a response for closing the service connection request according to the TCP tunnel identifier, and sending the response for closing the service connection request to the main control module specifically includes: and the WIFI module takes the TCP channel identification as an input parameter, calls a closing function to obtain a return value of the closing function, and forms a response of the service connection closing request according to the TCP channel identification and the return value of the closing function.
6. The method according to claim 1, wherein in step 5, if the data is legal, the data is written to the service corresponding to the TCP channel identifier according to the TCP channel identifier and the data to be written in the request for writing data to the service, so as to obtain a data writing result, when the data writing result is successful, the WIFI module composes a response to the request for writing data to the service from the number of bytes of the data successfully written to the service in the data writing result and the TCP channel identifier, and sends the response to the request for writing data to the service to the main control module through a serial port, and when the data writing result is failed, the WIFI module returns an error response to the main control module through the serial port, specifically:
if the data is legal, the WIFI module takes the TCP channel identifier and the data to be written in the data writing request to the service as input parameters, calls a writing function to obtain a return value of the writing function, and when the return value of the writing function is successful, the WIFI module makes a response of the data writing request to the service by the byte number of the data successfully written to the service in the return value of the writing function and the TCP channel identifier, and sends the response of the data writing request to the service to the main control module through a serial port; and when the return value of the write function is failure, the WIFI module returns an error response to the main control module through the serial port.
7. The method of claim 1,
in step 6, if the result is legal, reading data from the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for reading data from the service and the number of bytes of data to be read, and determining whether a result of reading the data is obtained within a predetermined time, if the result of reading the data is not obtained within the predetermined time, returning a response of reading timeout to the main control module through the serial port, if the result of reading the data is obtained within the predetermined time, and when the result of reading the data is successful, forming a response of the request for reading data from the service according to the number of bytes of successfully reading the data in the result of reading the data, the read data and the TCP channel identifier, and sending the response of the request for reading data from the service to the main control module through the serial port; when the result of reading the data is failure, returning an error response to the main control module through the serial port specifically comprises the following steps:
if the request for reading the data from the service is legal, the WIFI module takes the TCP channel identifier in the request for reading the data from the service and the byte number of the data to be read as input parameters, calls a read function and judges whether a return result of the read function is obtained within a preset time, if the return result of the read function is not obtained within the preset time, a response of reading overtime is returned to the main control module through a serial port, if the return result of the read function is obtained within the preset time, and when the return result of the read function is successful, a response of the request for reading the data from the service is formed according to the byte number of the data successfully read in the return result of the read function, the read data and the TCP channel identifier, and the response of the request for reading the data from the service is sent to the main control module through the serial port; and when the result of the returned result of the read function is failure, returning an error response to the main control module through the serial port.
8. The method of claim 1,
the WIFI module receives a request sent by the main control module through a serial port, and the type of the request is judged to be specifically: the WIFI module receives a request sent by a main control module through a serial port, judges the type of the request according to a type identifier in the request, and when the identifier of the request is a preset connection request identifier, the type of the request is a connection service request; when the request identifier is a preset write data request identifier, the request type is a write data request; when the request identifier is a preset read data request identifier, the request type is a read data request; and when the type of the request is the service connection closing identifier, the type of the request is a service connection closing request.
9. An apparatus for TCP communication to stabilize transmission, the apparatus comprising: WIFI module and host system, the WIFI module includes:
the receiving unit is used for receiving the request sent by the main control module through the serial port;
a first judging unit configured to judge a type of the request received by the receiving unit;
an obtaining unit, configured to obtain, when the first determining unit determines that the type of the request is a connection service request, a TCP channel identifier corresponding to an internet protocol address and a port number of a service in the connection service request;
a connection unit, configured to establish a connection with the service according to the internet protocol address and the port number of the service in the connection service request and the TCP channel identifier;
a second judging unit, configured to judge whether the connection unit and the service are successfully connected;
a closing ending unit, configured to close the resource corresponding to the TCP channel identifier when the determination result of the second determining unit is a connection failure, return an error response to the main control module through the serial port, and end;
the first generating and sending unit is used for generating a response of the connection service request according to the internet protocol address and the port number of the service and the TCP channel identifier when the judgment result of the first judging unit is that the connection is successful, and sending the response of the connection service request to the main control module through a serial port;
a third judging unit, configured to, when the first judging unit judges that the type of the request is a service closing request, judge whether the TCP channel identifier in the service closing connection request is legal;
a connection closing unit, configured to close, when the determination result of the third determining unit is legal, the connection with the service corresponding to the TCP channel identifier according to the TCP channel identifier in the service connection closing request;
a second generation and sending unit, configured to, after the connection closing unit closes, according to the TCP channel identifier in the request for closing service connection, a connection to the service corresponding to the TCP channel identifier, form a response to the request for closing service connection according to the TCP channel identifier in the request for closing service connection, and send the response to the request for closing service connection to the main control module through a serial port; the main control module is also used for returning an error response to the main control module through a serial port when the judgment result of the third judgment unit is illegal;
a fourth judging unit, configured to, when the first judging unit judges that the type of the request is a request for writing data to a service, judge whether the TCP channel identifier in the request for writing data to the service is legal;
the data writing unit is used for writing data to the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for writing data to the service and the data to be written when the judgment result of the fourth judgment unit is legal, so as to obtain a data writing result;
the third generation sending unit is used for returning an error response to the main control module through the serial port when the judgment result of the fourth judgment unit is illegal; the data writing module is also used for forming a response of the data writing request to the service by the byte number of the successfully written data to the service in the data writing result and the TCP channel identifier when the data writing result is successful, and sending the response of the data writing request to the service to the main control module through a serial port; the main control module is also used for returning an error response to the main control module through a serial port when the data writing result is failure;
a fifth judging unit, configured to, when the first judging unit judges that the type of the request is a request for reading data from a service, judge whether the TCP channel identifier in the request for reading data from the service is legal;
a data reading unit, configured to, when the result of the judgment by the fifth judgment unit is legal, read data to the service corresponding to the TCP channel identifier according to the TCP channel identifier in the request for reading data to the service and the byte number of the data to be read;
a sixth judging unit, configured to judge whether the read data unit obtains a result of reading data within a predetermined time; the data reading device is also used for judging whether the data reading result is successful or not;
the fourth generation sending unit is used for returning an error response to the main control module through a serial port when the judgment result of the fifth judgment unit is illegal; the sixth judging unit is further configured to, when the sixth judging unit judges that the data reading unit obtains the result of reading data within the predetermined time and the result of reading data is successful, form a response to the request for reading data from the service according to the number of bytes of successfully reading data in the result of reading data, the read data and the TCP channel identifier, and send the response to the request for reading data from the service to the main control module through the serial port; and the sixth judging unit is used for returning an error response to the main control module through the serial port when the sixth judging unit judges that the data reading unit obtains the data reading result within the preset time and the data reading result is failure.
10. The apparatus of claim 9,
the device also comprises a seventh judging unit used for judging whether the TCP channel identifier in the connection service request is legal or not;
the connection unit is specifically configured to, when the seventh determining unit determines that the TCP channel identifier in the connection service request is valid, establish a connection with the service according to the internet protocol address and the port number of the service in the connection service request and the TCP channel identifier;
the first generating and sending unit is further configured to return an error response to the main control module through a serial port when the seventh determining unit determines that the TCP channel identifier in the connection service request is illegal.
11. The apparatus of claim 9,
the obtaining unit is specifically configured to, when the first determining unit determines that the type of the request is a connection service request, invoke a socket creating function to obtain a first return value of the socket creating function, where the first return value of the socket creating function is a TCP channel identifier corresponding to an internet protocol address and a port number of a service in the connection service request;
the connection unit is specifically configured to call a connection function using an internet protocol address and a port number of the service and a first return value of the socket creation function as input parameters, to obtain a return value of the connection function;
a second judgment unit configured to judge a return value of the connection function;
a closing ending unit, configured to, when the second determining unit determines that the return value of the connection function is a connection failure, call a closing function, take the first return value of the socket creation function as an input parameter, close the resource corresponding to the TCP channel identifier, return an error response to the main control module through the serial port, and end;
and the first generation sending unit is used for generating a response of a connection service request according to the Internet protocol address and the port number of the service and the first return value of the socket creating function, sending the response of the connection service request to the main control module through a serial port and sending the response of the service connection closing request to the main control module.
12. The apparatus of claim 9,
the third determining unit is specifically configured to determine, when the first determining unit determines that the type of the request is a service closing request, whether the TCP channel identifier in the service closing connection request is greater than or equal to a preset value, if so, determine that the TCP channel identifier is legal, and if not, determine that the TCP channel identifier is illegal.
13. The apparatus of claim 9,
the connection closing unit is specifically configured to, when the determination result of the third determining unit is legal, call a closing function with the TCP channel identifier in the service connection closing request as an input parameter to obtain a return value of the closing function;
and the second generation and transmission unit is used for forming a response of the service connection closing request according to the TCP channel identifier in the service connection closing request and the return value of the closing function after the closing unit obtains the return value of the closing function, and transmitting the response of the service connection closing request to the main control module through a serial port.
14. The apparatus of claim 9,
the data writing unit is specifically configured to, when the judgment result of the fourth judgment unit is legal, call a writing function by using the TCP channel identifier and the data to be written in the request for writing data to the service as input parameters, and obtain a return value of the writing function;
the third generation sending unit is specifically used for returning an error response to the main control module through a serial port when the judgment result of the fourth judgment unit is illegal; the main control module is further configured to, when the return value of the write function is successful, form a response to the service write data request by the number of bytes of data successfully written to the service in the return value of the write function and the TCP channel identifier, and send the response to the service write data request to the main control module through the serial port; and the host computer is also used for returning an error response to the main control module through the serial port when the return value of the write function is failure.
15. The apparatus of claim 9,
the read data unit is specifically configured to, when the result of the judgment by the fifth judgment unit is legal, call a read function by using the TCP channel identifier in the request for reading data from the service and the number of bytes of data to be read as input parameters;
the sixth judging unit is specifically configured to, after the read data unit calls the read function, judge whether the read data unit obtains a return result of the read function within a predetermined time, and further judge whether the return result of the read function is successful;
the fourth generation sending unit is used for returning an error response to the main control module through a serial port when the judgment result of the fifth judgment unit is illegal; the sixth judging unit is further configured to, when the sixth judging unit judges that the read data unit obtains the return result of the read function within the predetermined time and the return result of the read function is successful, form a response to the request for reading data from the service according to the number of bytes of the data successfully read by the return result of the read function, the read data and the TCP channel identifier, and send the response to the request for reading data from the service to the main control module through the serial port; and the sixth judging unit is further configured to return an error response to the main control module through the serial port when the sixth judging unit judges that the read data unit obtains the return result of the read function within the predetermined time and the return result of the read function is a failure.
16. The apparatus of claim 9,
a first determining unit, configured to determine a type of the request according to a type identifier in the request, and when the identifier of the request is a preset connection request identifier, determine that the type of the request is a connection service request; when the request identifier is a preset data writing request identifier, judging that the type of the request is a data writing request; when the request identifier is a preset read data request identifier, judging that the type of the request is a read data request; and when the type of the request is the service connection closing identifier, judging that the type of the request is a service connection closing request.
CN202010840566.0A 2020-08-20 2020-08-20 Method and device for stable transmission of TCP communication Active CN111711996B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010840566.0A CN111711996B (en) 2020-08-20 2020-08-20 Method and device for stable transmission of TCP communication

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010840566.0A CN111711996B (en) 2020-08-20 2020-08-20 Method and device for stable transmission of TCP communication

Publications (2)

Publication Number Publication Date
CN111711996A true CN111711996A (en) 2020-09-25
CN111711996B CN111711996B (en) 2020-11-24

Family

ID=72547325

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010840566.0A Active CN111711996B (en) 2020-08-20 2020-08-20 Method and device for stable transmission of TCP communication

Country Status (1)

Country Link
CN (1) CN111711996B (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112769824A (en) * 2021-01-07 2021-05-07 深圳市大富网络技术有限公司 Information transmission state updating method, terminal, device and storage medium

Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103391289A (en) * 2013-07-16 2013-11-13 中船重工(武汉)凌久高科有限公司 Multilink safety communication method based on completion port model
CN105306536A (en) * 2015-09-22 2016-02-03 上海斐讯数据通信技术有限公司 Method for intelligent terminal to remotely execute service command based on WiFi (Wireless Fidelity)
WO2016044763A1 (en) * 2014-09-19 2016-03-24 Amazon Technologies, Inc. Automated configuration of log-coordinated storage groups
CN106341266A (en) * 2016-09-12 2017-01-18 武汉理工大学 Hardware TCP/IP protocol stack possessing active and passive transmission modes
CN108234614A (en) * 2017-12-25 2018-06-29 华南理工大学 A kind of exchange method based on custom protocol and Netty frames
CN108650232A (en) * 2018-04-12 2018-10-12 上海扩博智能技术有限公司 Unmanned plane media data real-time transmission system and method

Patent Citations (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103391289A (en) * 2013-07-16 2013-11-13 中船重工(武汉)凌久高科有限公司 Multilink safety communication method based on completion port model
WO2016044763A1 (en) * 2014-09-19 2016-03-24 Amazon Technologies, Inc. Automated configuration of log-coordinated storage groups
CN105306536A (en) * 2015-09-22 2016-02-03 上海斐讯数据通信技术有限公司 Method for intelligent terminal to remotely execute service command based on WiFi (Wireless Fidelity)
CN106341266A (en) * 2016-09-12 2017-01-18 武汉理工大学 Hardware TCP/IP protocol stack possessing active and passive transmission modes
CN108234614A (en) * 2017-12-25 2018-06-29 华南理工大学 A kind of exchange method based on custom protocol and Netty frames
CN108650232A (en) * 2018-04-12 2018-10-12 上海扩博智能技术有限公司 Unmanned plane media data real-time transmission system and method

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112769824A (en) * 2021-01-07 2021-05-07 深圳市大富网络技术有限公司 Information transmission state updating method, terminal, device and storage medium
CN112769824B (en) * 2021-01-07 2023-03-07 深圳市大富网络技术有限公司 Information transmission state updating method, terminal, device and storage medium

Also Published As

Publication number Publication date
CN111711996B (en) 2020-11-24

Similar Documents

Publication Publication Date Title
EP3013086B1 (en) Method, apparatus and electronic device for connection management
CN102014416B (en) Method and system for bidirectional detection of connection
CN107147620B (en) Message interaction processing method, client and server
WO2016029595A1 (en) Method, device, and equipment for calling open platform and non-volatile computer storage medium
CN104778121A (en) Game program test method, device and system
WO2008148278A1 (en) Method for exchanging large-capacity data between mobile terminal and smart card
CN110167192B (en) Bluetooth connection management method and device, electronic equipment and non-transient storage medium
CN111225414A (en) Multimode communication device, communication method thereof and multimode communication system
CN109450912B (en) Data transmission method, device and equipment
CN111711996B (en) Method and device for stable transmission of TCP communication
CN112559146B (en) Communication method between client and data terminal server
CN110138635B (en) Protocol conversion function verification device and method supporting FC and Ethernet
CN112202739B (en) Flow monitoring method and device
CN102195943B (en) Safety information interaction method and system
CN110022332B (en) Hypertext transfer security protocol proxy method, device, equipment and medium
CN114244758A (en) Network diagnosis method, storage medium, electronic device and system of android platform
CN107078837A (en) A kind of agreement frame transmission method, device, node device and system
CN103188117A (en) Device and method for information interaction server simulation testing
CN113204492A (en) Android application testing method and device
CN111343261A (en) Mobile phone data backup method based on wireless hotspot transmission
CN110781017B (en) Message processing method, device, equipment and storage medium
CN113691978B (en) Multi-equipment token processing method and system
CN106877974B (en) Data transmission method and device
US20140315600A1 (en) Communication interface method for se equipped on mobile terminal and se using the same
CN112839084B (en) Service processing method, device, equipment and computer readable storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant