什么是监听套接字的新数据,然后处理这些数据的最佳方式是什么? [英] What's the best way to monitor a socket for new data and then process that data?

查看:164
本文介绍了什么是监听套接字的新数据,然后处理这些数据的最佳方式是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅我的C#.NET新手状态。如果这是显而易见的,我错过了从文档,链接到相关的网页或样品code将AP preciated。

Please pardon my C#.Net newbie status. If this is obvious and I missed it from the docs, a link to the relevant page or sample code would be appreciated.

我的工作将接受来自Java应用程序TCP套接字连接的应用程序。 (是的,需要安装Java的那一部分,这是一个的Sun SPOT设备和Java是唯一的选择。)Java的应用程序会定期将新数据写入到插座上,我的应用程序的任务是采取字节[],转换它为一个字符串,处理数据(更新UI等),并有可能转发到运行一个类似C#.NET应用程序的另一台计算机中的数据。

I am working on an application that will accept a TCP socket connection from a Java application. (Yes, Java is required on that part. It's a Sun SPOT device and Java is the only option.) The Java app will be periodically writing new data to the socket, and my app's job is to take in the byte[], convert it to a string, process the data (update UI, etc), and possibly forward the data on to another computer running a similar C#.NET app.

下面是我做了什么至今:目前该应用程序旋转起来就推出一个线程中打开一个套接字。在Java应用可以成功连接到插座,以便在工作。我一直在寻找在的NetworkStream的的BeginRead 方法和 dataAvailable 长度的CanRead 属性,但我不完全知道如何确定,当我读过一个分组数据,通常约512个字节,但可能会有所不同。

Here's what I've done so far: Right now the app spins up a thread on launch that opens a socket. The Java app can successfully connect to the socket so that is working. I was looking at the NetworkStream's beginRead method and the dataAvailable, length, and CanRead properties, but I am not entirely sure how to ascertain when I've read one packet of data, usually about 512 bytes but could vary.

如果在Java应用将数据写入到流或者出现数据的积压(Java的应用程序将被传递的数据,而快。)我怎么能确保我在读数据只有一个数据包的时间?如果Java应用程序空终止数据时写入,会帮助吗?难道还不够吗?

If the Java app writes data to the stream or there is a backlog of data (The Java app will be passing data rather quickly.) how can I make sure that I am reading only one packet of data at a time? If the Java app null terminates the data when it writes, will that help? Is it enough?

最后,插座只会收到一个连接,但我需要保持打开状态,直到有错误或连接终止。什么是最优雅的方式来处理这方面的?我不认为关闭并重新打开与每个数据包会工作,因为迅速火(近实时),它在的Sun SPOT基站上运行的Java应用的方面。眼下,基站结束的时候,我的应用程序死了一个响亮而痛苦的死亡。 :)

Lastly, the socket will only receive one connection, but I need to keep it open until there is an error or the connection is terminated. What is the most graceful way to handle this aspect? I don't think close and reopen with each data packet will work because of the rapid fire (nearly realtime) aspect of the Java app that is running on the Sun SPOT base station. Right now, when the base station terminates, my app dies a loud and painful death. :)

感谢您的阅读和任何帮助,您可以提供。

Thanks for reading and for any help you can offer.

推荐答案

我们已经解决了这个问题的方法是有定点人物和固定宽度的字段的组合。头两个字节是整个分组的长度头部。然后,我读了包成一个byte []缓冲区,然后靠自己的包结构,以便知道,例如,前两个字节分别为PTED个别字段,然后字符串字段终止与国米$ P $ \ n字符(0x0A的,如果你的得分在家里)。然后,长数据字段由读取连续的8个字节,等等。它似乎是工作pretty的同时为我们处理,但它显然对这种情况的解决方案,其中一个拥有超过插座的两端,而不是在那里控制一种是仅能够控制一端。希望这可以帮助别人了。

The way we've solved this is to have a combination of sentinel characters and fixed width fields. The first two bytes are a length header for the entire packet. Then, I read the packet into a byte[] buffer and then rely on our own packet structure to know that, for instance, the first two bytes are each to be interpreted as individual fields and then string fields are terminated with the \n character (0x0A if you're scoring at home). Then, long data fields are processed by reading in 8 consecutive bytes, etc. It seems to be working pretty well for us, but it's obviously a solution for a situation where one has control over both ends of the socket and not where one is only able to control the one end. Hope this helps someone else, too.

这篇关于什么是监听套接字的新数据,然后处理这些数据的最佳方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆