监视套接字以获取新数据然后处理该数据的最佳方法是什么? [英] What's the best way to monitor a socket for new data and then process that data?

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

问题描述

请原谅我的 C#.Net 新手状态.如果这很明显并且我从文档中遗漏了它,那么将不胜感激提供指向相关页面或示例代码的链接.

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 方法和 dataAvailablelengthCanRead 属性,但我不是完全确定如何确定我何时读取了一个数据包,通常约为 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 应用程序 null 在写入时终止数据,那会有帮助吗?够了吗?

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[] 缓冲区,然后依靠我们自己的数据包结构知道,例如,前两个字节每个都被解释为单独的字段,然后字符串字段以 字符终止(如果你在家里得分,则为 0x0A).然后,通过读取 8 个连续字节等来处理长数据字段.这对我们来说似乎工作得很好,但这显然是一种解决方案,用于控制套接字的两端,而不是只控制套接字的两端能够控制一端.希望这对其他人也有帮助.

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 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天全站免登陆