networkstream

TCP client\server - client doesn't always read

那年仲夏 提交于 2019-11-27 03:40:35
问题 Client Code: TcpClient client = new TcpClient(); NetworkStream ns; private void Form1_Load(object sender, EventArgs e) { try { client.Connect("127.0.0.1", 560); ns = client.GetStream(); byte[] buffer = ReadFully(ns, client.Available); //working with the buffer... } catch { //displaying error... } } public static byte[] ReadFully(NetworkStream stream , int initialLength) { // If we've been passed an unhelpful initial length, just // use 32K. if (initialLength < 1) { initialLength = 32768; }

Difference between NetworkStream.Read() and NetworkStream.BeginRead()?

二次信任 提交于 2019-11-27 01:30:03
问题 I need to read from NetworkStream which would send data randomly and the size of data packets also keep varying. I am implementing a multi-threaded application where each thread would have its own stream to read from. If there is no data on the stream, the application should keep waiting for the data to arrive. However, if the server is done sending data and has terminated the session, then it should exit out. Initially I had utilised the Read method to obtain the data from the stream, but it

How to check if TcpClient Connection is closed?

徘徊边缘 提交于 2019-11-26 18:36:36
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 show me if the TcpClient is disconnected. How would you go about this using a TcpClient? uriel I wouldn't recommend you to try write just for testing the socket. And don't relay on .NET's Connected property either. If you want to know if the remote end point is still active, you can use TcpConnectionInformation: TcpClient client = new TcpClient(host,

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

只愿长相守 提交于 2019-11-26 17:54:40
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), NetworkStream.Read appears to hang. Here is the stack trace which i was able to gather from a dump file 00000000705ae850 000007fef784f60d DomainBoundILStubClass.IL_STUB(IntPtr, Byte*, Int32, System.Net.Sockets.SocketFlags) 00000000705ae930 000007fef785c930 System.Net.Sockets.Socket.Receive(Byte[], Int32, Int32, System.Net.Sockets.SocketFlags, System

How to get all data from NetworkStream

孤街醉人 提交于 2019-11-26 16:28:29
问题 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 = client.GetStream()) { byte[] data = new byte[1024]; int numBytesRead = stream.Read(data, 0, data.Length); if (numBytesRead > 0) { string str= Encoding.ASCII.GetString(data, 0, numBytesRead); } } Please tell me what i am missing to get all the data from the

TPL TaskFactory.FromAsync vs Tasks with blocking methods

徘徊边缘 提交于 2019-11-26 15:47:25
问题 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 connections. After writing code with the first option & chaining multiple read & write operations with continue with, I was left with ugly, hard to debug code. I believe writing code with the synchronous version & then wrapping it with a Task would decrease

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

喜欢而已 提交于 2019-11-26 06:07:47
问题 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), NetworkStream.Read appears to hang. Here is the stack trace which i was able to gather from a dump file 00000000705ae850 000007fef784f60d DomainBoundILStubClass.IL_STUB(IntPtr, Byte*, Int32, System.Net.Sockets.SocketFlags) 00000000705ae930