How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

huangapple go评论86阅读模式
英文:

How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

问题

案例:

我有一个TCP客户端,它发送数据,我希望从我的服务器收到数据。所以,在这里,一切都好。

我决定在端口9876上创建一个套接字服务器来监听这个设备(使用Java 8)。我接收到的数据很奇怪,当我打印它时像这样:"慳慳慳",但从理论上讲,接收到的数据应该是十六进制的。这是设备发送的数据包示例(登录):

40407F000431303031313132353239393837000000000000001001C1F06952FDF069529C91110000000000698300000
C0000000000036401014C00030001190A0D04121A1480D60488C5721800000000AF4944445F3231364730325F53205
6312E322E31004944445F3231364730325F482056312E322E31000000DF640D0A

我想以一种简单的方式(如果可能的话,仅使用一个类)复制通过Castel连接到设备的连接,几乎包括以下6个方法:

  • 连接
  • 断开连接
  • 编码
  • 解码
  • 发送数据包
  • 接收数据包

我的目标是使用PHP复制这种行为。我尝试过使用WebSocket/HTTP协议,结果是我无法连接它。我无法从设备那里收到任何数据包。

所以,另一个问题可能是:我如何编写自己的自定义协议(Castel)来连接服务器与这些设备?

我查阅了Traccar服务器的代码,但它非常庞大。我用它测试了设备,它可以工作,但是我无法理解它是如何在代码层面上实现的。

文档:

  • Castel通信流程

How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

  • 用于下载上传的协议包格式

How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

  • 登录包示例

How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

  • 我使用PHP创建了一个简单的套接字,这是我收到的内容

How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

英文:
  1. THE CASE:

I have a TCP client what send a data and I want to hear the data from my server. So, here, everything okay.

I decided make an socket server on port 9876 to listen this device (using Java 8). The data which I receive is weird, when I print it is like: "慳慳慳", but in theory, the data coming should be a hex. This is an example of the package what the device sends (login):

40407F000431303031313132353239393837000000000000001001C1F06952FDF069529C91110000000000698300000
C0000000000036401014C00030001190A0D04121A1480D60488C5721800000000AF4944445F3231364730325F53205
6312E322E31004944445F3231364730325F482056312E322E31000000DF640D0A

I want to replicate in a easy way (one class if its possible) the connection by castel to the device (port 5086 for Traccar server app) in almost 6 methods:

  • Connect
  • Disconnect
  • Encode
  • Decode
  • Send package
  • Receive package

My goal is replicate this behavior with PHP. I tried with Websocket/HTTP protocols and the result is that I can't connect with it. I cant hear any package from the device.

So, other question could be: how I can code my own custom protocol (Castel) to connect server with these devices?

I reviewed the Traccar Server code, but its very extensive. I proved the device with it and it works, but I can't understand how (at the code level).

  1. Documentation
  • Castel communication flow

How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

  • Protocol package format for download upload

How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

  • Login package example
    How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

  • I did a simple socket with PHP and this is what I listened
    How to replicate the Traccar Sever connection to a device (castel protocol by TCP) in a single Java class? (to connect, disconnect encode and decode)

答案1

得分: 1

以下是 Castel 协议的解码器部分,你可以在 Traccar 服务器中找到很多不同的内容,但如果你只想要协议解析部分的话,可以使用以下代码:

github.com/traccar/.../CastelProtocolDecoder.java

这个解码器是基于 Netty 构建的,所以如果你想使用它,你需要实现服务器的其余部分来监听传入的连接。我建议查阅 Netty 的指南,以获取有关如何实现的更多详细信息:

https://netty.io/wiki/user-guide-for-4.x.html

英文:

Traccar server includes a lot of different things, but you can just take the protocol parsing part if you want. Here's the decoder for the Castel protocol:

github.com/traccar/.../CastelProtocolDecoder.java

The decoder is based on Netty, so if you want to use it, you need to implement the rest of the server to listen to incoming connections. I would recommend checking Netty guide for more details on how to do it:

https://netty.io/wiki/user-guide-for-4.x.html

huangapple
  • 本文由 发表于 2020年4月8日 10:13:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/61092305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定