networkstream

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

混江龙づ霸主 提交于 2019-12-03 10:20:14
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 one is thrown from the inner stream will be treated as fatal by its caller. Reusing a SslStream instance

TCP read and write from client and server

无人久伴 提交于 2019-12-02 14:24:21
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; public static NetworkStream Writer; public static NetworkStream Receiver; public Form1() {

StreamReader.ReadLine not working over TCP when using \r as line terminator

别来无恙 提交于 2019-12-01 22:17:24
问题 When I use only \r as a line terminator, the StreamReader.ReadLine() method doesn't work. It works if I use Environment.NewLine , \r\n or \ra ("a" being any character). Is this a bug? The same problem doesn't occurs when using MemoryStream instead of NetworkStream. What workarounds can I use if I can't change the protocol? My service Thread service = new Thread((ThreadStart)delegate { TcpListener listener = new TcpListener(IPAddress.Loopback, 2051); listener.Start(); using (TcpClient client =

.NET NetworkStream.EndWrite() bytes written

末鹿安然 提交于 2019-12-01 21:18:49
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).? You are not missing anything, it is a doc bug. Probably induced by copy/pasting the EndRead article. Where it very much does matter. You already know how many bytes were written, all of them. 来源: https://stackoverflow

StreamReader.ReadLine not working over TCP when using \\r as line terminator

こ雲淡風輕ζ 提交于 2019-12-01 20:44:59
When I use only \r as a line terminator, the StreamReader.ReadLine() method doesn't work. It works if I use Environment.NewLine , \r\n or \ra ("a" being any character). Is this a bug? The same problem doesn't occurs when using MemoryStream instead of NetworkStream. What workarounds can I use if I can't change the protocol? My service Thread service = new Thread((ThreadStart)delegate { TcpListener listener = new TcpListener(IPAddress.Loopback, 2051); listener.Start(); using (TcpClient client = listener.AcceptTcpClient()) using (NetworkStream stream = client.GetStream()) using (StreamReader

.NET blocking socket read until X bytes are available?

落花浮王杯 提交于 2019-12-01 20:38:28
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 the read, restarting at an offset waiting for the remaining data? In a real application the read should

Operation not allowed on non-connected Sockets - C# 4.0

孤街醉人 提交于 2019-12-01 17:36:07
问题 It keeps having an error "Operation not allowed on non-connected sockets" on the line var ServerStream = Connect2Server.GetStream(); And I'm not really sure why Below is rest of the code for that function var buffersize = 0; var Convert2Tet = new ASCIIEncoding(); var Connect2Server = new TcpClient(); var ServerEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8801); var ServerStream = Connect2Server.GetStream(); Console.WriteLine("Connecting to Server"); Connect2Server.Connect

Operation not allowed on non-connected Sockets - C# 4.0

有些话、适合烂在心里 提交于 2019-12-01 17:00:23
It keeps having an error "Operation not allowed on non-connected sockets" on the line var ServerStream = Connect2Server.GetStream(); And I'm not really sure why Below is rest of the code for that function var buffersize = 0; var Convert2Tet = new ASCIIEncoding(); var Connect2Server = new TcpClient(); var ServerEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8801); var ServerStream = Connect2Server.GetStream(); Console.WriteLine("Connecting to Server"); Connect2Server.Connect(ServerEndPoint); var WelcomeMessage = new byte[4096]; ServerStream.Read(WelcomeMessage, 0, 4096); Console.Write

Why does the stream not support the seek operation?

痞子三分冷 提交于 2019-12-01 16:52:28
问题 I'm currently working with tcp/ip suite. I'm writing a program to encrypt files at sender's end and decrypt at receiver's side. I came across this exception while initializing my byte array with the length that of network stream. Here's my code: if (client.Connected) { NetworkStream binarystream = client.GetStream(); byte[] receivebytes = new byte[binarystream.Length]; binarystream.Read(receivebytes, 0, receivebytes.Length); Stream file = File.OpenWrite(saveFileDialog1.FileName); file.Write

BeginReceive / BeginRead timeouts

夙愿已清 提交于 2019-12-01 15:04:59
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 supported on NetworkStream or TcpClient - there is a ReceiveTimeout property, but this appears to only apply to the synchronous equivalent - 'Read'. Even the underlying Socket class doesn't appear to support timeouts in its BeginReceive method. I've searched on this issue and the only suggested resolution I've seen is to set up another background thread to