networkstream

Understanding the NetworkStream.EndRead()-example from MSDN

一曲冷凌霜 提交于 2019-12-21 09:07:30
问题 I tried to understand the MSDN example for NetworkStream.EndRead(). There are some parts that i do not understand. So here is the example (copied from MSDN): // Example of EndRead, DataAvailable and BeginRead. public static void myReadCallBack(IAsyncResult ar ){ NetworkStream myNetworkStream = (NetworkStream)ar.AsyncState; byte[] myReadBuffer = new byte[1024]; String myCompleteMessage = ""; int numberOfBytesRead; numberOfBytesRead = myNetworkStream.EndRead(ar); myCompleteMessage = String

How to cancel NetworkStream.ReadAsync without closing stream

偶尔善良 提交于 2019-12-21 07:25:29
问题 I am trying to use NetworkStream.ReadAsync() to read data but I cannot find how to cancel the ReadAsync() once called. For background, the NetworkStream is provided to me by a connected BluetoothClient object (from 32Feet.NET Bluetooth Library). The current get-it-working code I'm trying is below. int bytesRead; while (this.continueReading) { bytesRead = await this.stream.ReadAsync(this.buffer, 0, (int)this.buffer.Length); Console.WriteLine("Received {0} bytes", bytesRead); } Console

How to (repeatedly) read from .NET SslStream with a timeout?

耗尽温柔 提交于 2019-12-21 03:42:12
问题 I just need to read up to N bytes from a SslStream but if no byte has been received before a timeout, cancel, while leaving the stream in a valid state in order to try again later. (*) This can be done easily for non-SSL streams i.e. NetworkStream simply by using its ReadTimeout property which will make the stream throw an exception on timeout. Unfortunately this approach doesn't work on SslStream per the official docs: SslStream assumes that a timeout along with any other IOException when

NetworkStream.Write vs. Socket.Send

杀马特。学长 韩版系。学妹 提交于 2019-12-20 09:55:10
问题 I have a c# application that I use a custom FTP library for. Right now Im using Socket.Send to send the data but I was wondering if it would be better to initiate a NetworkStream with the socket and use NetworkStream.Write instead. Are there any advantages to using one over the other? 回答1: The advantage of a NetworkStream derives primarily from the fact that it is a Stream . The disadvantage of a Socket is that common code that reads and writes from abstract I/O sources like a Stream cannot

TCP read and write from client and server

偶尔善良 提交于 2019-12-20 07:48:57
问题 I'm trying to make a client and server which can read and write info back and forth between each other. I can write from the client and server reads it but not vise versa and I have no idea why. Nobody on here seems to know when I asked before and I cant find anything online that works. If you know please tell me and not tell me to go read an article about TCP because it doesn't help at all. Client: namespace ExampleClient { public partial class Form1 : Form { public static bool IsConnected;

.NET blocking socket read until X bytes are available?

狂风中的少年 提交于 2019-12-20 01:52:14
问题 Assume I have simple protocol implemented over TCP, where each message is made up of: An int indicating data length. Binary data, of the length specified in 1. Reading such a message I would like something like: int length = input.ReadInt(); byte[] data = input.ReadBytes(length); Using Socket.Receive or NetworkStream.Read the available number of bytes is read. I want the call to ReadBytes to block until length bytes are available. Is there a simple way to do this, without having to loop over

.NET NetworkStream.EndWrite() bytes written

无人久伴 提交于 2019-12-20 01:38:39
问题 The MSDN documentation clearly states that: After obtaining the NetworkStream, you can call the EndWrite method to successfully complete the send operation and return the number of bytes sent. Emphasis mine. However, it returns nothing (void): public override void EndWrite( IAsyncResult asyncResult ) Am I missing something, or is this a typo ( EndRead() does return bytes read).? 回答1: You are not missing anything, it is a doc bug. Probably induced by copy/pasting the EndRead article. Where it

How do you wait for a Network Stream to have data to read?

妖精的绣舞 提交于 2019-12-18 16:47:06
问题 I have a worker thread in my application that is responsible for three different things. Requests for two of the jobs turn up in Queues that I have written, the other job is activated when a request turns up on a Network stream. I would like my worker thread to wait when there is no work to be done. This is easy with the two Queues as they expose a ManualResetEvent that is set when they have items, however the NetworkStream does not seem to have this. The NetworkStream has been retrieved from

C#: Implementing NetworkStream.Peek?

折月煮酒 提交于 2019-12-18 14:49:53
问题 Currently, there isn't a NetworkStream.Peek method in C#. What is the best way of implementing such a method which functions just like NetworkStream.ReadByte except that the returned byte is not actually removed from the Stream ? 回答1: If you don't need to actually retrieve the byte, you can refer to the DataAvailable property. Otherwise, you can wrap it with a StreamReader and invoke its Peek method. Note that neither of these are particularly reliable for reading from a network stream, due

Get Length of Data Available in NetworkStream

橙三吉。 提交于 2019-12-17 16:58:21
问题 I would like to be able to get the length of the data available from a TCP network stream in C# to set the size of the buffer before reading from the network stream. There is a NetworkStream.Length property but it isn't implemented yet, and I don't want to allocate an enormous size for the buffer as it would take up too much space. The only way I though of doing it would be to precede the data transfer with another telling the size, but this seems a little messy. What would be the best way