问题
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