tcpclient

Ensure streamreader doesn't hang waiting for data

人走茶凉 提交于 2020-01-23 09:17:12
问题 The code below reads everything there is to read from tcp client stream, and on the next iteration it will just sit there on the Read() (i'm assuming waiting for data). How can I ensure it doesn't and just returns when there's nothing there to read? Do I have to set low timeout, and respond to an exception when it fails out? Or there's a better way? TcpClient tcpclnt = new TcpClient(); tcpclnt.Connect(ip, port); Stream stm = tcpclnt.GetStream(); stm.Write(cmdBuffer, 0, cmdBuffer.Length); byte

Alternative to NetworkStream.Read that indicates remote host has closed the connection?

末鹿安然 提交于 2020-01-22 06:02:12
问题 With regards to handling a TCP/IP connection using the TcpClient class, is there an alternative for checking whether the remote host has closed the connection other than waiting for the NetworkStream.Read method to return a 0? 回答1: You can use IOControlCode.KeepAliveValues on the TcpClient.Client to guarantee that a keep-alive check is made at least on the specified interval and then check the TcpClient.Client.Connected property. An example how to use it: struct tcp_keepalive { public int

Alternative to NetworkStream.Read that indicates remote host has closed the connection?

佐手、 提交于 2020-01-22 06:01:08
问题 With regards to handling a TCP/IP connection using the TcpClient class, is there an alternative for checking whether the remote host has closed the connection other than waiting for the NetworkStream.Read method to return a 0? 回答1: You can use IOControlCode.KeepAliveValues on the TcpClient.Client to guarantee that a keep-alive check is made at least on the specified interval and then check the TcpClient.Client.Connected property. An example how to use it: struct tcp_keepalive { public int

In C# how to run unix command on remote SSH using TcpClient

心不动则不痛 提交于 2020-01-21 12:45:16
问题 I am trying to run a UNIX command on remote host from my C# application. I have seen various post over internet including SO for doing this using various libraries like SmartSsh any many others. And some others using command line commands. I do not want to use any third party library. Hence I tried using TcpClient class with example shown here on MSDN. I am passing hostname as : user@servername and port 22 . When I run this code it trows an exception SocketException (0x80004005): No such host

C# TcpClient: Send serialized objects using separators?

心不动则不痛 提交于 2020-01-18 04:32:08
问题 Based on serialization (mentioned here https://stackoverflow.com/a/7849374/985798) I am trying to reengineer my small tcp application, which was using a string message until now. But I ran into a small problem and I would like to know what solution you'd recommend me to use: If I am trying to send more than one message in a very small interval, they will be merged in the "queue" and the client will receive both messages at the same time, which will end up with a single broken object. In the

Sending message from server and receive from the client end

本小妞迷上赌 提交于 2020-01-17 05:01:11
问题 Here is a sample server program written in c# which receives an id from a client. My sample code is given below. I want to send a string array to client and display is on the client console. The sample array string can be: string[] arr1 = new string[] { "one", "two", "three" }; What additional code I need to add with the following server and client code? using System; using System.Net; using System.Net.Sockets; using System.Text; namespace server { class Program { static void Main(string[]

How to know if it is memory leak or not if Mem Usage in Task Manager keep increasing

帅比萌擦擦* 提交于 2020-01-14 19:01:55
问题 I wrote a small Server class which basically is a TcpListener wrapper and ThreadPool thread spawner. The threads run Server::ProcessMessage() which does some work sending messages to and fro and then quits at the end of it. But just before exiting the function, I also call TcpClient.GetStream().Close() and then TcpClient.Close(). I don't use any Mutex or ManualResetEvent WaitHandles. Tested the Client and Server, everything works except in task manager it shows the Mem Usage keep on

TCPClient sometimes recieving empty bytes array

 ̄綄美尐妖づ 提交于 2020-01-14 05:25:27
问题 Currently developing a simple message server using sockets and sometimes the TCPClient receives the correct number of bytes but each byte is 0. Here's the sender code. try { //c.clientSocket.NoDelay = true; // Send back an OK var clientStream = c.clientSocket.GetStream(); var Response = JsonConvert.SerializeObject(new Packet("SERVER", c.ClientName, new List<Payload>() { new Payload(MessageLibrary.Commands.OK, null) })); var msg = System.Text.Encoding.ASCII.GetBytes(Response); clientStream

Sending messages and files over NetworkStream

左心房为你撑大大i 提交于 2020-01-06 08:44:07
问题 I am quite new to network programming and I have a few questions regarding this code: if (client.Connected) { ChangeLabel("Mit dem Server verbunden..."); NetworkStream stream = client.GetStream(); FileStream fs = null; try { fs = new FileStream("Mapeditor2.exe", FileMode.Create); } catch (Exception e) { MessageBox.Show(e.Message); Environment.Exit(0); } byte[] bResponse = new byte[16]; stream.Read(bResponse, 0, 16); string sResponse = System.Text.Encoding.UTF8.GetString(bResponse); int

NetworkStream ReadAsync and WriteAsync hang infinitelly when using CancellationTokenSource - Deadlock Caused by Task.Result (or Task.Wait)

烂漫一生 提交于 2020-01-06 08:07:29
问题 After reading pretty much every question on Stack Overflow and Microsoft's documentation about NetworkStream, I dont understand what is wrong with my code. The problem I see is that my method GetDataAsync() hangs very often. I call this method from Init Method like so: public MyView(string id) { InitializeComponent(); MyViewModel myViewModel = session.Resolve<MyViewModel>(); //Autofac myiewModel.Init(id); BindingContext = myViewModel; } Above, my View does its initialization, then resolves