Answer the question
In order to leave comments, you need to log in
How to get data via TCP C#?
There is a device. It connects via TCP and sends an array of bytes reporting its status. She needs to respond like ACK / NAK. Once every 20 seconds, it pings the PC with a special byte sequence. The connection after receiving is prohibited by the terms of the protocol. And of course, everything should be asynchronous.
The essence of the problem:
1) All examples on the Internet that I found do not receive data (there is a buffer, everything is OK, we do BeginRecieve, we write to the buffer, and there are zeros in it). CHADNT??
2) What is the best way to do this, given the fact that several hundred devices will subsequently be connected to one PC?
Tell me examples of similar implementations, or poke into the manual (:
Thank you for your attention.
UPD:
The question is closed. Why am I a sucker? Because first check that socket.CanRead && socket.DataAvilable, and then read until the counter of bytes read is equal (or less) to zero. profit (:
Answer the question
In order to leave comments, you need to log in
there is a buffer, everything is OK, we do BeginRecieve, we write to the buffer, and there are zeros in it
Thanks for the link. But even in synchronous mode I have troubles:
_tcpServer = new TcpListener(IPAddress.Any, TCP.Default.port);
_tcpServer.Start();
byte[] bytes = new byte[255];
TcpClient client = _tcpServer.AcceptTcpClient();
NetworkStream stream = client.GetStream();
int i;
i = stream.Read(bytes, 0, bytes.Length);
MessageBox.Show(BitConverter.ToString(bytes));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question