[TCP/IP 11] An example of Socket communication based on TCP protocol

I. Introduction

Generally, the interface connection is mostly in the way of http/https or webservice, and the socket connection is less and there will be some difficulties. Just some time ago, we completed the docking requirements of a socket interface, and now we will sort out the ideas for implementation.

2. Overview of requirements

1. A socket server needs to be provided to receive the data passed by the three parties in real time

2. Specification of real-time messages

3. Communication and interface format description

communication method:

Communication adopts TCP protocol and SOCKET synchronous short connection mode.

Message structure:

The message is a variable-length message, in the form of fixed-length message header + variable-length message body

The basic structure of the message is shown in the figure below:

6-digit transaction message length + transaction message. The length of the 6-digit transaction message is expressed as an ASCII code string (6 bytes), right-justified, with 0 to the left, excluding its own length, and it means the length of the message body. For example, "000036fbced3fe-7025-4b5c-9cef-2421cd981f39", 000036 is the length, and "fbced3fe-7025-4b5c-9cef-2421cd981f39" is the message content.

The message structure conforms to the message format of the XML standard, and the message is coded in GBK without BOM format. The root node of the message is the Transaction node. Unless there are special instructions in the message, the fields defined in the message are all child nodes of the Transaction node. For the message format, refer to the example in the next section.

The point of this article is to explain that when using sockets to transmit data, the basic structure of the message usually includes [fixed-length message header + variable-length message body]

4. Message example

request:

000410<?xml version="1.0" encoding="GBK"?><req:request xmlns:req="http://chimera.intele.com/gw/xsd/SMSGateway/Request/2013/02"><serviceId>29</serviceId><username>gre</username><password>erg</password><message><recipient>+4741414141</recipient><content>test</content><price>0</price><settings><sendWindow><startDate>2018-06-15</startDate><startTime>16:15:00</startTime></sendWindow></settings></message></req:request>

response:

000683<?xml version="1.0" encoding="GBK"?><rsl:result xmlns:rsl=http://chimera.intele.com/gw/xsd/TCPGateway/Result/2015/10 xmlns:sms=http://chimera.intele.com/gw/xsd/SMSGateway/Response/2013/02 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><status><code>1</code><deion>OK</deion></status><sms:response><batchReference>0c2c002f-ccc6-4c7b-86e1-c7871b1c98b3</batchReference><messageStatus><statusCode>1</statusCode><statusMessage>Message enqueued for sending</statusMessage><clientReference>SMS-AFFS-000000100</clientReference><recipient>+4741915590</recipient><messageId>6y06b02hdo00</messageId><sequenceIndex>1</sequenceIndex></messageStatus></sms:response></rsl:result>

 

Highlights from previous issues:

Summary of Java knowledge system (2021 version)

Summary of basic knowledge of Java multithreading (absolutely classic)

Super detailed springBoot study notes

Summary of common data structures and algorithms

Java design patterns: a comprehensive analysis of 23 design patterns (super detailed)

Summary of Java interview questions (with answers)

 

Reprinted from: https://www.sohu.com/a/259845146_575744

Guess you like

Origin blog.csdn.net/guorui_java/article/details/114645601