I thought about sending a packet with one byte to the server and to write on the serverside the following code:
int a= stream.ReadByte()
if(a==-1){
//client received data
}
The stream is a NetworkStream
I want to do this because after this I start a timer on the server. The timer should only start if the clients have updated their values.
A Stream
only has limited options:
ReadByte()
, except the second point becomes "negative" for no data)What it can't do is say "I currently do not have data, but I might in the future". As such, you can't use the Stream
API to check whether there is data. Instead, it is typical to use an async API (usually on the Socket
itself, or another abstraction that isn't Stream
), to allow you to process a callback when data becomes available. There is also the .Available
property on a .Socket
which tells you the amount of data currently buffered and available for consumption (note: this API is typically only used to choose between sync vs async IO).