C#TCP客户端侦听数据 [英] C# TCP Client listening for data
问题描述
我想在工作时连接到特定的条形码阅读器,但目前在理解TCP流如何与C#结合使用方面遇到一些问题.
I would like to connect to a specific barcode reader at my work but currently I'm facing some problems to understand how TCP stream reading works with C#.
我在stackoverflow上查找了许多代码示例,正如我所看到的,人们经常打开连接,发送命令,然后要求答复.
I've looked for many code samples on stackoverflow and as I can see, people oftenly open a connection, send a command and then ask for the reply.
重点是,条形码读取器只接受TCP连接,然后将数据与我的数据一起发送.
The point is, the barcode reader just accept a TCP connection and then send the data with my data.
我想找到使用TcpClient或NetworkStream连续读取数据的最有效方法.我的意思是,我想在传入数据时监听数据,但同时我还要监听许多其他事情.
I would like find the most efficient way to use TcpClient or NetworkStream to read in a continuous way the data. I mean, I would like to listen for the data when it's incoming but also process many other things at the same time I'm listening.
我想知道简单的TCP库是否像带有Connect函数的库一样存在,并且在接收数据(使用分隔符或长度)时触发事件.
I was wonderning if simple TCP libraries exists like a one with a Connect function, and an event firing when you receive data (with a delimiter or with a length).
谢谢
推荐答案
我终于找到了一个名为 SimpleTCP .我可以使用以下代码行连接到我的网络条形码阅读器:g_Client.Connect(ip, port);
I finally found a simple TCP library called SimpleTCP. I'm able to connect to my network barcode reader with the following line of code: g_Client.Connect(ip, port);
然后有一个名为DataReceived(g_Client.DataReceived += l_Client_DataReceived;)
的事件,它使我可以完全按照自己的意愿进行操作.
Then there is an event available called DataReceived (g_Client.DataReceived += l_Client_DataReceived;)
which allow me to do exactly what I want.
该库还提供对正在使用的TcpClient的访问,而我目前正在寻找是否可以看到特殊字符(例如< STX>(当前返回给我一个特殊的ASCII字符),但我认为我必须为此另外做一个线程.
This library also gives access to the TcpClient it's using and I'm currently searching for the possibility to see the special characters such as < STX >(currently returning me a special char in ASCII) but I will have to do another thread for this I think.
这篇关于C#TCP客户端侦听数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!