Questions tagged [networkstream]

A .NET class that provides the underlying stream of data for network access.

A .NET class that "provides the underlying stream of data for network access."

594 questions
37
votes
10 answers

How to check if TcpClient Connection is closed?

I'm playing around with the TcpClient and I'm trying to figure out how to make the Connected property say false when a connection is dropped. I tried doing NetworkStream ns = client.GetStream(); ns.Write(new byte[1], 0, 0); But it still will not…
RoboDev
  • 4,003
  • 11
  • 42
  • 51
21
votes
3 answers

NetworkStream.Write vs. Socket.Send

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…
Brian Tacker
  • 1,091
  • 2
  • 18
  • 37
20
votes
2 answers

TPL TaskFactory.FromAsync vs Tasks with blocking methods

I was wondering if there were any performance implications between using TPL TaskFactory.FromAsync and using TaskFactory.StartNew on blocking versions of the methods. I'm writing a TCP server that will support no more than 100 concurrent…
19
votes
2 answers

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

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…
Pol
  • 3,848
  • 1
  • 38
  • 55
18
votes
4 answers

Does one need to close both NetworkStream and TcpClient, or just TcpClient?

I'm reading the documentation on TcpClient.Close() and noticed this: Calling this method will eventually result in the close of the associated Socket and will also close the associated NetworkStream that is used to send and receive data if one was…
Ryan Peschel
  • 11,087
  • 19
  • 74
  • 136
18
votes
5 answers

C#: Implementing NetworkStream.Peek?

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?
Lopper
  • 3,499
  • 7
  • 38
  • 57
17
votes
8 answers

How to get all data from NetworkStream

I am trying to read all data present in the buffer of the Machine connected through TCP/IP but i don't know why i am not getting all data ,some data is getting Missed. Here is the code that i am using .. using (NetworkStream stream =…
shubham Hegdey
  • 507
  • 5
  • 8
  • 15
16
votes
2 answers

BeginReceive / BeginRead timeouts

I'm using a NetworkStream & TcpClient to asynchronously receive data using BeginRead. I need to apply a time-out to this operation, such that after a specified amount of time the read will be aborted. As far as I'm able to tell, this isn't…
Barg
  • 2,968
  • 7
  • 26
  • 28
16
votes
3 answers

How to moq a NetworkStream in a unit test?

I'm using Moq & NUnit as a unit test framework. I've written a method that is given a NetworkStream object as a parameter: public static void ReadDataIntoBuffer(NetworkStream networkStream, Queue dataBuffer) { if ((networkStream != null) &&…
Timo Kosig
  • 845
  • 10
  • 23
14
votes
3 answers

How to cancel NetworkStream.ReadAsync without closing stream

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…
Swampie
  • 165
  • 1
  • 1
  • 9
12
votes
8 answers

TcpClient.GetStream().DataAvailable returns false, but stream has more data

So, it would seem that a blocking Read() can return before it is done receiving all of the data being sent to it. In turn we wrap the Read() with a loop that is controlled by the DataAvailable value from the stream in question. The problem is that…
James
  • 1,651
  • 2
  • 18
  • 24
11
votes
4 answers

What are some reasons NetworkStream.Read would hang/block?

MSDN documentation seems to suggest that NetworkStream.Read will always return immediately. If no data is found it returns 0. However, I have some code that is currently deployed, that only in some cases (and I haven't figured out which ones yet),…
Mark
  • 5,223
  • 11
  • 51
  • 81
10
votes
1 answer

Replacing Socket.ReceiveAsync with NetworkStream.ReadAsync (awaitable)

I have an application that makes a couple hundred TCP connections at the same time, and receives a constant stream of data from them. private void startReceive() { SocketAsyncEventArgs e = new SocketAsyncEventArgs(); e.Completed…
Josh
  • 2,083
  • 5
  • 23
  • 28
10
votes
5 answers

Where can I find high resolution financial data

I'm writing some Machine Learning software for equity and would like to find some tick data or at least 3 or 5 minute data. I would like to have a year or two for testing. I don't really care about what exchange the data is from, as long as its…
Martin Kristiansen
  • 9,875
  • 10
  • 51
  • 83
10
votes
2 answers

Why would BufferedStream.Write throw "This stream does not support seek operations"?

This one puzzles me. I get an error about seek when I'm not even calling it? I have code that looks something like this: // send 42 uint value = 42; byte[] msg = BitConverter.GetBytes(value); stream.Write(msg, 0, sizeof(uint)); and I get this…
redtuna
  • 4,586
  • 21
  • 35
1
2 3
39 40