开发者

C# send and receive tcp packet via web app

开发者 https://www.devze.com 2023-02-10 07:37 出处:网络
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.

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, continuou开发者_Go百科sly 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?


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:

  1. Have your Javascript poll the server with an Ajax request called from a timer callback; this has all the disadvantages of polling,
  2. Use a "long poll" which essentially means do an asnychronous call to your web site that blocks until it has a result; this doesn't scale well since it locks an IIS thread for every client web page,
  3. Use a comet server and associated client side library, is the best route but usually involves spending money.

There is lots of information on Comet and Ajax on the web.


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 your TCP 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 use ExternalInterface to pass data out of the flash movie and to eagerly waiting javascript!

0

精彩评论

暂无评论...
验证码 换一张
取 消