S
S
Sergey Lysogor2016-04-15 18:35:42
.NET
Sergey Lysogor, 2016-04-15 18:35:42

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

2 answer(s)
T
tex0, 2016-04-15
@serhioli

there is a buffer, everything is OK, we do BeginRecieve, we write to the buffer, and there are zeros in it

First, make sure that the device still connects to your server.
Second, read about Asynchronous Programming Patterns
Link to Second

S
Sergey Lysogor, 2016-04-15
@serhioli

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

Brought out an array of 255 elements 00-00-00-00-00-etc.
Launched another program - the craftsmen did it in C. They have everything.
It is worth noting: the port is free, the device is being reached, the data is definitely there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question