C# 通过网络应用程序发送和接收 tcp 数据包
你好 我们正在编写一个独特的项目。我想知道是否有人可以为我指出正确的方向,因为我不熟悉 TCP 编程。
我们与外部系统交谈。我发送一个 tcp 数据包并接收 tcp 响应。这需要在 Web 应用程序的页面上工作,每 x 毫秒连续轮询
建议的消息长度为 8 个字节,不传输有关消息开始或结束的信息。消息结构: |字节 0 |字节 1 |字节 2 |字节 3 |字节 4 |字节 5 |字节 6 |字节 7 |
消息分隔符是: |字节 0 | = 0x02(stx) 和 |字节 7 | = 0x03 (etx)
我的发送消息: | sx |的 | 'b' | '0'| '0'| '0' |'0' |etx |
我的接收消息: | sx | 'S'| 'B'| '0'| '1'| '0'| '0'|等
问题:
我应该使用 tcpCLient/Listener 类还是 Sockets 类?
让网页每 x 毫秒连续轮询和发送/接收这些数据包的最佳方法是什么?
Hi
We are writing a unique project. I was wondering if someone could point me us in the right direction, as I am not familiar with tcp programming.
We talk to an external system. I send a tcp packet and receive the tcp response. This needs to work from a page in a web application, continuously polling every x msecs
The proposed message length is 8 bytes, no information about the start or end of message is transmitted. Message structure:
| byte 0 | byte 1 | byte 2 | byte 3 | byte 4 | byte 5 | byte 6 | byte 7 |
Message delimiters are:
| byte 0 | = 0x02 (stx)
and
| byte 7 | = 0x03 (etx)
My send message:
| stx | ‘s’ | ‘b’ | ‘0’ | ‘0’ | ‘0’ |‘0’ |etx |
My recv message:
| stx | ‘S’ | ‘B’ | ‘0’ | '1' | ‘0’ | ‘0’ | etx |
Questions:
Should I use the tcpCLient/Listener classes or Sockets classes?
Whats the best way to get the web page to continuously poll and send/receive these packets every x ms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你所提议的基本上无法完成,因为Javascript不支持直接的TCP/IP。您必须在服务器上运行 TCP 进程,并使用异步更新方法在网页上显示结果。
要在页面上异步显示结果,您必须使用 Ajax 和 Comet。 Comet 基本上为您提供了三种选择:
网上有很多关于 Comet 和 Ajax 的信息。
What you are proposing basically can't be done, because Javascript does not support direct TCP/IP. You would have to run the TCP process on the server, and use an asynchronous update method to display the results on your web page.
To display the results asynchronously on your page you would have to use Ajax and Comet. Comet offers you basically three options:
There is lots of information on Comet and Ajax on the web.
您可能需要研究 WebSockets 来进行客户端通信,尽管目前支持非常有限。
另一种选择是使用
.swf
文件作为TCP
通信的中介。请参阅 http://help.adobe.com/ en_US/FlashPlatform/reference/actionscript/3/flash/net/Socket.html。每次收到感兴趣的数据包时,您都可以使用ExternalInterface
将数据从 Flash 影片传递到热切等待的 javascript!You might want to look into WebSockets for your client side communication, though support is very limited at the moment.
Another option would be to use a
.swf
file as an intermediary for yourTCP
communication. See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/Socket.html. Every time you receive a packet of interest you could useExternalInterface
to pass data out of the flash movie and to eagerly waiting javascript!