Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of connecting remote server communication based on TCP protocol

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the method of connecting to a remote server based on TCP protocol". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what is the method of connecting to a remote server based on TCP protocol"?

1. Environment preparation hardware preparation

Development board

M26 communication module

Software preparation

QCOM Serial Port Assistant

Document preparation

M26 AT Command Manual

This document comes from the remote official! When reading this tutorial, please refer to this document for detailed instructions on the AT directive!

Changeover switch

If the switch in the upper right corner of the Bear development board is turned on to the AT-PC, the module is directly connected to the PC for easy debugging.

All tools and documentation are available on the official account of "Cubs Open Source Community" in reply to "Communication Module"!

two。 Module signal query and Network query AT instruction AT

Instruction: AT

Function: test whether the AT instruction function is normal

Example:

ATOKAT+CPIN?

Instruction: AT+CPIN?

Function: query whether the SIM card is normal. If ready is returned, the SIM card is normal.

Example:

AT+CPIN?+CPIN: READYOKAT+CSQ

Instruction: AT+CSQ

Function: query the signal strength of the module, the first value is 0-31 is normal, 99 is abnormal

Example:

AT+CSQ+CSQ: 170.OKATION CREG?

Instruction: AT+CREG?

Function: query whether the module is registered on the GSM network, + CREG:0,1 means registered on the local network, + CREG:0,5 means registered on the roaming network.

Example:

AT+CREG?+CREG: 010 KATC CGREG?

Instruction: AT+CERGE?

Function: query whether the module is registered on the GPRS network, + CGREG:0,1 means registered on the local network, + CGREG:0,5 means registered on the roaming network.

Example:

AT+CGREG?+CGREG: 0,1OK3. Activate the mobile scene and get the ip address

This lab must be carried out when querying that the GPRS network has properly registered the network!

AT+QIFGTCNT=0

Instruction: AT+QIFGTCNT=0

Function: configure the current scene

Example:

AT+QIFGCNT=0OKAT+QICSGP=1, "CMNET"

Instruction: AT+QICSGP=1, "CMNET"

Function: set APN of GPRS, Mobile CMNET, China Unicom UNINET

Example:

AT+QICSGP=1, "CMNET" OKAT+QIMODE=0

Instruction: AT+QIMODE=0

Function: set data transmission mode. 0 indicates non-transparent mode, 1 indicates transparent mode.

Example:

AT+QIMODE=0OKAT+QIDEACT

Instruction: AT+QIDEACT

Function: close the GPRS scene before activating the GPRS scene to make sure the connection is correct

Example:

AT+QIDEACTDEACT OKAT+QIREGAPP

Instruction: AT+QIREGAPP

Function: start the task and set the access point APN, user name and password

Example:

AT+QIREGAPPOKAT+QIACT

Instruction: AT+QIACT

Function: activate mobile scen

Example:

AT+QIACTOKAT+QILOCIP

Instruction: AT+QILOCIP

Function: check the IP address obtained by the module

Example:

AT+QILOCIP100.125.208.234. Based on TCP protocol to connect remote server communication example 4.1. Build a remote TCP server

First, we need to build a TCP server, which can be done in two ways:

Use Python, Java, C # and other languages to write server programs on the server.

Use the network debugging assistant to start the TCP server on the local PC

Because the M26 module registers the public network ip address directly, we use the first method to run a tcp test server written by Python on the Linux server:

The local PC uses a local area network, and the public network cannot access this PC directly according to the ip address. Private network penetration is required and is not recommended.

The Python program here is as follows:

# tcp-server.pyfrom socket import * host =''port = 800' create server socketserver_socket = socket (AF_INET,SOCK_STREAM) # bind socket listening address server_addr = (host,port) server_socket.bind (server_addr) # start listening Maximum number of allowed connections 5server_socket.listen (5) # processing connection request try: while True: print ('waiting for connect...') # blocking waiting for the client's connection client_socket, client_addr = server_socket.accept () # after the connection is successful Print client information print ('a client connnect from:', client_addr) while (True): # send data to the client client_socket.send ('Hello, clientroomroom.encode ()) # receive the client's data data = client_socket.recv (1024) print (' recv data is' Data.decode () # closes socket if "quit" in data.decode () when receiving quit: break # closes socket client_socket.close () server_socket.close () print ("socket closed.") Breakexcept: client_socket.close () server_socket.close () print ("socket closed."

Run:

Python3 tcp-server.py

The effect is as follows:

4.2. Module connects to the server

Query ip address:

AT+QILOCIP100.125.208.23

Use the AT command to connect to the TCP server. The first parameter is the protocol type, the "TCP" indicates the use of the TCP protocol, the second parameter is the ip address of the TCP server, or the domain name can be used, and the most important parameter is the port where the TCP server opens listening:

AT+QIOPEN=,/

Example:

AT+QIOPEN= "TCP", "122.51.89.94", "8000" OKCONNECT OK

After connecting, you can also see on the server side:

4.2. Module receives messages

After the module connects to the server, the server automatically sends a message, and the module prints out the received information:

Hello, client!4.3. The module actively sends messages.

You can send a message to the TCP server using the following command. First, set the number of bytes of data to be sent, wait for the module to return >, and enter the data to be sent. If the data sent exceeds the set n bytes, only the first n bytes will be sent. The following data is considered invalid and will not be sent:

AT+QISEND > hello

Example:

Hello, clientless ATC quality > helloSEND OK

The success of sending depends on whether the TCP server running on the server receives:

4.4. Close the TCP connection

After the communication is complete, you can close the TCP connection using the following command:

AT+QICLOSE

Example:

AT+QICLOSECLOSE OK so far, I believe you have a deeper understanding of "what is the method of connecting to a remote server based on TCP protocol". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report