C# TCP Client listening for data

二次信任 提交于 2019-12-25 07:25:04

问题


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#.

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.

The point is, the barcode reader just accept a TCP connection and then send the data with my data.

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.

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).

Thanks


回答1:


TCP doesn't sound right, but if it is it might be acting as a http server.If it is then you'd use a HttpClient. If it is just TCP, you would do something like:Async Client Socket




回答2:


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);

Then there is an event available called DataReceived (g_Client.DataReceived += l_Client_DataReceived;) which allow me to do exactly what I want.

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.



来源:https://stackoverflow.com/questions/37620952/c-sharp-tcp-client-listening-for-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!