networkstream

BeginReceive / BeginRead timeouts

℡╲_俬逩灬. 提交于 2019-12-01 13:55:29
问题 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

Unable to read data correctly from .Net socket in C#

两盒软妹~` 提交于 2019-12-01 13:15:57
问题 I have a client and server class in C# that uses socket communication. The Server looks like this: public class AsyncTcpServer { private Socket _server_socket; private Socket _client_socket; private byte[] _receive_buffer; private byte[] _send_buffer; private NetworkStream _ns; public void Start() { try { _server_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _server_socket.Bind(new IPEndPoint(IPAddress.Any, 17999)); _server_socket.Listen(0); _server

How reading messages from Server? (tcp)

可紊 提交于 2019-12-01 12:13:49
Client write to server - server read. and Server write to client - client not read. Server: using System; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; class SocketServer { public static void Main() { StreamReader streamReader; NetworkStream networkStream; TcpListener tcpListener = new TcpListener(5555); tcpListener.Start(); Console.WriteLine("The Server has started on port 5555"); Socket serverSocket = tcpListener.AcceptSocket(); try { if (serverSocket.Connected) { Console.WriteLine("Client connected"); networkStream = new NetworkStream(serverSocket);

How reading messages from Server? (tcp)

拈花ヽ惹草 提交于 2019-12-01 11:02:48
问题 Client write to server - server read. and Server write to client - client not read. Server: using System; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; class SocketServer { public static void Main() { StreamReader streamReader; NetworkStream networkStream; TcpListener tcpListener = new TcpListener(5555); tcpListener.Start(); Console.WriteLine("The Server has started on port 5555"); Socket serverSocket = tcpListener.AcceptSocket(); try { if (serverSocket

Does NetworkStream.DataAvailable see buffered data?

我怕爱的太早我们不能终老 提交于 2019-12-01 05:12:07
问题 Does NetworkStream.DataAvailable know whether the sender's send buffer is empty? Or does it simply indicate whether the receiver's read buffer has data? My assumption is the latter... Specifically, for some socket work involving an ongoing conversation, I currently use a length-prefix so the the receiver knows exactly how much data is in the current batch; however, I've been sent a .patch suggesting I use NetworkStream.DataAvailable instead. My concern is that this will just tell me what the

C# Why doesn't “Flush” force the bytes down the network stream?

半腔热情 提交于 2019-11-30 23:59:42
问题 I have a project where I'm trying to send a serialized object to the server, then wait for an "OK" or "ERROR" message to come back. I seem to be having a similar problem to th poster of : TcpClient send/close problem The issue is that the only way I seem to be able to send the original object is to close the connection, but then (of course) I can't wait to see if the object was processed successfully by the server. private void button4_Click(object sender, EventArgs e) { RequestPacket req =

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

旧时模样 提交于 2019-11-30 14:12:59
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 a TcpClient. What I am after is code that looks something like this: while (notDone) { WaitHandle

C# - TcpClient - Detecting end of stream?

不打扰是莪最后的温柔 提交于 2019-11-30 08:34:57
问题 I am trying to interface an ancient network camera to my computer and I am stuck at a very fundamental problem -- detecting the end of stream. I am using TcpClient to communicate with the camera and I can actually see it transmitting the command data, no problems here. List<int> incoming = new List<int>(); TcpClient clientSocket = new TcpClient(); clientSocket.Connect(txtHost.Text, Int32.Parse(txtPort.Text)); NetworkStream serverStream = clientSocket.GetStream(); serverStream.Flush(); byte[]

Get Data from Bluetooth device in C#

僤鯓⒐⒋嵵緔 提交于 2019-11-30 07:41:37
I'm trying to get data from a medical BT device that I already have pairing code and communication protocol. Looking for some code I've got this code: using System; using System.Collections.Generic; using System.Linq; using System.Text; using InTheHand.Net.Sockets; using InTheHand.Net; using InTheHand.Net.Bluetooth; using InTheHand.Windows.Forms; using System.Net.Sockets; using System.Diagnostics; using System.Threading; namespace dConsoleApp { static class Program { // My BT USB adapter private static BluetoothEndPoint EP = new BluetoothEndPoint(BluetoothAddress.Parse("00:02:72:CD:9A:33"),

Reading on a NetworkStream = 100% CPU usage

做~自己de王妃 提交于 2019-11-29 11:11:54
I am reading from a NetworkStream that is in a while loop. The issue is I am seeing 100% CPU usage. Is there any way to stop this from happening? Here is what I have so far: while (client != null && client.Connected) { NetworkStream stream = client.GetStream(); data = null; try { // Check if we are still connected. if (client.Client.Poll(0, SelectMode.SelectRead)) { byte[] checkConn = new byte[1]; if (client.Client.Receive(checkConn, SocketFlags.Peek) == 0) { throw new IOException(); } } if (stream.DataAvailable) { //Read the first command WriteToConsole("Waiting for next command"); data =