Something really odd is happening to me.
I have ssh command which executes python script on my remote server.
When I execute it through PuTTY it works like it should and desired output is shown. But when executed through my C# app using ssh.net it always returns some old data that's long time gone from the server (which is freebsd 9.3).
Here's how I execute my command and catch output:
var task = new Task(() => {
var ret = ssh_instance.CreateCommand("cd /home/" + "python python_script.py -param");
ret.Execute();
FlexibleMessageBox.Show(ret.Result);
});
task.Start();
It seems literally unexplainable to me so I would be really thankful if someone could enlighten me what am I doing wrong.
Please remember.
Connection will closed after calling "CreateCommand()" method.
You need to read all of received data from your stream against your each command.
--> In my opinion, this is a reason why you've got old result from your connection.
So, I try to use Renci.SshNet.ShellStream class. in my code was,
private Renci.SshNet.SshClient _client = null;
private Renci.SshNet.ShellStream shellStream = null;
private void connect()
{
try
{
this.disConnect();
_client = new Renci.SshNet.SshClient(cboUri.Text, tbxSrvUsr.Text, tbxSrvPW.Text);
//_client.ErrorOccurred += new EventHandler<Renci.SshNet.Common.ExceptionEventArgs>(_client_ErrorOccurred);
//_client.HostKeyReceived += new EventHandler<Renci.SshNet.Common.HostKeyEventArgs>(_client_HostKeyReceived);
_client.Connect();
byte[] buffer = new byte[1000];
shellStream = _client.CreateShellStream("Jornathan", 80, 24, 800, 800, 1024);
shellStream.BeginRead(buffer, 0, buffer.Length, null, null);
shellStream.DataReceived += new EventHandler<Renci.SshNet.Common.ShellDataEventArgs>(shellStream_DataReceived);
tbxStatus.Text = "Connected";
this.btnConnect.Enabled = false;
this.cboServerList.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
and add method to read all text from server.
Please remember, you should read all text until encounter received data size is "0". refer as below.
private void readOutput()
{
do
{
string strOutput = shellStream.ReadLine();
if (strOutput != null && strOutput.Length > 0)
{
System.Console.WriteLine(strOutput);
this.SetStatusText(strOutput);
}
else
{
break;
}
} while (true);
}
And then add event handler, that's it!.
private System.Threading.Thread Thread_OutputHandle = null;
private void shellStream_DataReceived(object sender, Renci.SshNet.Common.ShellDataEventArgs e)
{
if (Thread_OutputHandle == null)
{
Thread_OutputHandle = new System.Threading.Thread(new System.Threading.ThreadStart(readOutput));
Thread_OutputHandle.Start();
}
else if (Thread_OutputHandle.ThreadState == System.Threading.ThreadState.Stopped)
{
Thread_OutputHandle.Abort();
Thread_OutputHandle = new System.Threading.Thread(new System.Threading.ThreadStart(readOutput));
Thread_OutputHandle.Start();
}
}
Others:
I hope to remember where I've got help to make this code.
but I can't.
Please let me know when you find location of code that I've got help.
I'll add its location to here.
Related
I got stuck in solving a problem in my current project. I'm coding a simulation in WPF/C# and included a Unity-Application in the window using the window-handles/hwnd pretty much like "Programmer" did in this answer. He also suggested namedpipes for the communication.
But the communication just works in one way - The server (WPF-App) is able to send messages to the client (the embedded Unity-App). If I try it the other way around, the whole Unity-App hungs up and stops working. As far as I was able to figure it out, as soon as I flush the writer or add the AutoFlush and then write something Unity gets stuck.
It is interesting, that the InBufferSize of the server is 0. Might this be the Problem?
This is my C#-Script in Unity assigned to a GameObject:
public class pipeCommunicationScript : MonoBehaviour
{
NamedPipeClientStream client;
public GameObject console;
static Text consoleText;
StreamReader reader;
StreamWriter writer;
int counter = 0;
static bool _threadRunning;
Thread _thread;
void Start()
{
consoleText = console.GetComponent<Text>();
consoleText.text = "trying to set up client";
_thread = new Thread(StartClient);
_thread.Start();
}
void StartClient()
{
_threadRunning = true;
//Client
consoleText.text = "Attempting to connect to pipe...";
client = new NamedPipeClientStream(".","UnitySimulationPipe", PipeDirection.InOut);
if (!client.IsConnected) { client.Connect(); }
consoleText.text = "Connected to pipe.";
writer = new StreamWriter(client);
reader = new StreamReader(client);
writer.AutoFlush = true;
InvokeRepeating("sendThroughPipe", 5.5f, 5.5f);
while (_threadRunning)
{
consoleText.text = "IN:" + reader.ReadLine();
}
_threadRunning = false;
}
void sendThroughPipe()
{
try
{
client.WaitForPipeDrain();
writer.WriteLine("test" + counter);
}
catch(Exception e)
{
consoleText.text = "Exception while sending: " + e;
}
consoleText.text = "OUT: test" + counter;
counter++;
}
void OnDisable()
{
if (_threadRunning)
{
_threadRunning = false;
_thread.Join();
}
}
}
And this my Server-Script:
void StartServer()
{
Task.Factory.StartNew(() =>
{
Console.WriteLine("Connection initialisation started");
var server = new NamedPipeServerStream("UnitySimulationPipe", PipeDirection.InOut);
server.WaitForConnection();
while (!server.IsConnected)
{
Console.WriteLine("Connection not initialized");
server.WaitForConnection();
}
Console.WriteLine("Connection initialized");
reader = new StreamReader(server);
writer = new StreamWriter(server);
writer.AutoFlush = true;
writer.WriteLine("startConnection");
printOnStatusBar("OUT: startConnection");
connected = true;
Console.WriteLine("Current pipe readable: " + server.CanRead);
Console.WriteLine("Pipe InBufferSize: " + server.InBufferSize);
while (true)
{
Console.WriteLine("start while again");
if (reader.Peek() > 0)
{
var l = reader.ReadLine();
Console.WriteLine(reader.ReadLine());
Console.WriteLine("read finished");
server.WaitForPipeDrain();
}
Thread.Sleep(500);
}
});
}
The most probable answer is, in fact, what you mentionned (bufferSize).
You should check for the initialyzers parameters of every object you use.
Don't worry, I'm sure you'll find the answer just by searching there. I used to create such code in the same way, it should work well.
Okay, I changed my initilisation of the server-object to (explanation of different parameters here):
var server = new NamedPipeServerStream("UnitySimulationPipe", PipeDirection.InOut, 1, 0, 0, 1024, 1024);
But I personally think, that this was just a minor mistake. The mainreason why my code didn't worked was, that I missed to call
server.WaitForPipeDrain();
after
writer.WriteLine("startConnection");
printOnStatusBar("OUT: startConnection");
in my server-code.
i do have an embedded board (mini/tiny 210)
and i did try building an application which uses system.socket and system.socket.net in C# ( vs2005 - wince5 device application but i do have wince 6 on my device!! )
my purpose is to send and receive data bytes using Ethernet. for sending , well there is no problem but receive does need multitasking.
i should start a task so it can listen to the port if there is any data upcoming but once i add thread part to my code the whole program crashes (dose not do that in pc x64 core i5) but in embedded board it will crash.
if i eliminate multi tasking i might lose some data when networkstream.read is not online or when networkstream.read is online and i'm waiting for data,which ends in program will not respond up until the timeout. here is some of my codes . hope it helps.
this code does work :
private void button_connect_Click(object sender, EventArgs e)
{
try
{
//Thread TH = new Thread(new ThreadStart(con));
//TH.Start();
con();
}
catch
{
MessageBox.Show("THREAD ERROR");
}
}
public void con()
{
try
{
cli = null;
cli = new TcpClient();
cli.Connect(IPAddress.Parse(textBox_ip.Text),Convert.ToInt32(textBox_port.Text));
st = null;
st = cli.GetStream();
button_connect.Enabled = false;
button_Close.Enabled = false;
button_disconnect.Enabled = true;
statusBar1.Text = "Connected to server";
//Thread rec = new Thread(new ThreadStart(recf));
//rec.Start();
}
catch
{
MessageBox.Show("Connection faild","Error");
statusBar1.Text = "Faild to connect to server";
}
this code dose not work :
private void button_connect_Click(object sender, EventArgs e)
{
try
{
Thread TH = new Thread(new ThreadStart(con));
TH.Start();
//con();
}
catch
{
MessageBox.Show("THREAD ERROR");
}
}
public void con()
{
try
{
cli = null;
cli = new TcpClient();
cli.Connect(IPAddress.Parse(textBox_ip.Text),Convert.ToInt32(textBox_port.Text));
st = null;
st = cli.GetStream();
button_connect.Enabled = false;
button_Close.Enabled = false;
button_disconnect.Enabled = true;
statusBar1.Text = "Connected to server";
//Thread rec = new Thread(new ThreadStart(recf));
//rec.Start();
}
catch
{
MessageBox.Show("Connection faild","Error");
statusBar1.Text = "Faild to connect to server";
}
}
as you can see i'm getting this crash while just one of my task is in the code the second task has been commented
i appreciate your help in advance
The crash is probably caused by updating UI elements from a secondary thread.
The con method reads a textbox and updates several buttons as well as a status bar, and this should only happen on the main application thread.
To verify whether this is the issue, you could simply comment out the lines in the con method that access UI elements (copy the textbox value to a member string before starting the thread, or just hard-code the IP address) and re-run the application.
Then to actually fix the problem you'll have to look into Control.Invoke.
I build a quick little script to take data from an app I created for my phone and put it in a textbox and it works perfectly with this code:
private void workerVoid()
{
string receivedDataString = null;
listenSock.Listen(3);
acceptedSock = listenSock.Accept();
listenSock.Bind(new IPEndPoint(IPAddress.Any, 5665));
while(shouldRun)
{
while (listenSock.SendBufferSize != 0)
{
Buffer = new byte[listenSock.SendBufferSize];
int byteCount = acceptedSock.Receive(Buffer);
byte[] byteFormatted = new byte[byteCount];
for (int byteCounter = 0; byteCounter < byteCount; byteCounter++)
{
byteFormatted[byteCounter] = Buffer[byteCounter];
}
receivedDataString = Encoding.ASCII.GetString(byteFormatted);
setTextDelegate sTD = new setTextDelegate(setText);
reqFormVar.Invoke(sTD, new object[] { receivedDataString });
}
}
}
With workerVoid being a thread. Again this works perfectly however, when the device that is connected (my phone) disconnects, the thread stops running altogether (I get the thread .. has exited in visual studio) and my device is unable to connect after that, even though I have a while loop that should run forever.
How can I make it so my device can reconnect again after disconnecting without the thread exiting?
Thanks
Your code might throw an exception , causing the while loop to be ignored, to avoid this .
Surround your internal while loop in a try catch , and move deceleration inside the try catch block like so :
var shouldRun = true;
while(shouldRun)
{
try
{
string receivedDataString = null;
listenSock.Listen(3);
acceptedSock = listenSock.Accept();
listenSock.Bind(new IPEndPoint(IPAddress.Any, 5665));
while (listenSock.SendBufferSize != 0)
{
...
}
}
catch
{
}
}
I have an issue about the server-client communication.
I googled around but I did not find a solution to this.
Right now I am using 32feet in order to get in touch 2 or more (till 7) BT clients to 1 BT server.
I need to broadcast a message from the server to every device in the same time, but I don't know how to do it.
The only way I figured out was to use the list of connection in order to send the message one per time, but it means a delay between each message sent (around 100 ms per device). Unfortunately it means to have a large delay on the last one.
Can someone please give me an advice on how to solve this problem?
Is there a way to broadcast the message to all devices in the same time?
If it can be helpfull, here there is the handle of connection and reading from devices.
Thanks for your help
private void btnStartServer_Click(object sender, EventArgs e)
{
btnStartClient.Enabled = false;
ConnectAsServer();
}
private void ConnectAsServer()
{
connessioniServer = new List<BluetoothClient>();
// thread handshake
Thread bluetoothConnectionControlThread = new Thread(new ThreadStart(ServerControlThread));
bluetoothConnectionControlThread.IsBackground = true;
bluetoothConnectionControlThread.Start();
// thread connessione
Thread bluetoothServerThread = new Thread(new ThreadStart(ServerConnectThread));
bluetoothServerThread.IsBackground = true;
bluetoothServerThread.Start();
}
private void ServerControlThread()
{
while (true)
{
foreach (BluetoothClient cc in connessioniServer)
{
if (!cc.Connected)
{
connessioniServer.Remove(cc);
break;
}
}
updateConnList();
Thread.Sleep(0);
}
}
Guid mUUID = new Guid("fc5ffc49-00e3-4c8b-9cf1-6b72aad1001a");
private void ServerConnectThread()
{
updateUI("server started");
BluetoothListener blueListener = new BluetoothListener(mUUID);
blueListener.Start();
while (true)
{
BluetoothClient conn = blueListener.AcceptBluetoothClient();
connessioniServer.Add(conn);
Thread appoggio = new Thread(new ParameterizedThreadStart(ThreadAscoltoClient));
appoggio.IsBackground = true;
appoggio.Start(conn);
updateUI(conn.RemoteMachineName+" has connected");
}
}
private void ThreadAscoltoClient(object obj)
{
BluetoothClient clientServer = (BluetoothClient)obj;
Stream streamServer = clientServer.GetStream();
streamServer.ReadTimeout=1000;
while (clientServer.Connected)
{
try
{
int bytesDaLeggere = clientServer.Available;
if (bytesDaLeggere > 0)
{
byte[] bytesLetti = new byte[bytesDaLeggere];
int byteLetti = 0;
while (bytesDaLeggere > 0)
{
int bytesDavveroLetti = streamServer.Read(bytesLetti, byteLetti, bytesDaLeggere);
bytesDaLeggere -= bytesDavveroLetti;
byteLetti += bytesDavveroLetti;
}
updateUI("message sent from "+clientServer.RemoteMachineName+": " + System.Text.Encoding.Default.GetString(bytesLetti));
}
}
catch { }
Thread.Sleep(0);
}
updateUI(clientServer.RemoteMachineName + " has gone");
}
private void updateUI(string message)
{
Func<int> del = delegate()
{
textBox1.AppendText(message + System.Environment.NewLine);
return 0;
};
Invoke(del);
}
private void updateConnList()
{
Func<int> del = delegate()
{
listaSensori.Items.Clear();
foreach (BluetoothClient d in connessioniServer)
{
listaSensori.Items.Add(d.RemoteMachineName);
}
return 0;
};
try
{
Invoke(del);
}
catch { }
}
I don't exactly understand how you do it right now (the italian names are not helping...) but maybe my solution can help you.
first of all, bluetooth classic does not support broadcast. so you have to deliver at one at a time.
i do connect to 7 serial port devices at a time, using 7 threads. then i tell every thread to send data. this is very close to same time, but of course not exactly.
let me know if that helps or if you need a code example.
It's a lot of irrelevant code to look through.. but pretty much it sends a packet and listens for a packet in return
if i comment the part out where it calls the ReceiveAuthPacket() method at the end of sending a packet, it will work and the label will turn blue.. but otherwise it will never activate turning the label blue and will instead turn the label red or green (depending on the returned packet).
basically im just using the label as an indicator of the status.. and no matter what i try i can't get it to turn blue because it seems to be waiting for all the code to be finished executing and it just won't work..
i even tried using data triggers in WPF and it still won't work.
any work arounds? i just don't get it..
private readonly UdpMessageAuthentication _msgAuth;
private void Button_Authenticate_OnClick(object sender, RoutedEventArgs e)
{
Label_Authentication.Content = "Attempting Authentication";
Label_Authentication.Foreground = new SolidColorBrush(Colors.Blue);
_msgAuth.SendAuthPacket(IPAddress.Parse(TextBox_IP.Text), TextBox_ClientID.Text);
}
public void SendAuthPacket(IPAddress ip, string userID)
{
_ip = ip;
_userID = userID;
if (_udpClient.Client == null)
_udpClient = new UdpClient();
//GSISClockRegRequest,<Client Id>,,1
string msg = string.Format("GSISClockRegRequest,{0},,1", _userID);
byte[] sendBytes = Encoding.ASCII.GetBytes(msg);
bool sent = false;
try
{
_label.Content = "Attempting Authentication";
_label.Foreground = new SolidColorBrush(Colors.Blue);
while (_label.Content != "Attempting Authentication")
{
//loop
}
_udpClient.Connect(_ip, 5001);
_udpClient.Send(sendBytes, sendBytes.Length);
Console.WriteLine("Sending {0} bytes. Message: {1}", sendBytes.Length, msg);
sent = true;
}
catch (Exception)
{
Console.WriteLine("UDP Auth Packet Failed to Send");
}
_udpClient.Close();
if (sent)
ReceiveAuthPacket(); //IF I COMMENT THIS OUT IT'LL WORK
}
private void ReceiveAuthPacket()
{
IPEndPoint e = new IPEndPoint(IPAddress.Any, 5001);
UdpClient u = new UdpClient(e);
u.Client.ReceiveTimeout = 3000;
Console.WriteLine("Listening for Messages: ");
try
{
Byte[] receiveBytes = u.Receive(ref e);
string receiveString = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine("Received: {0}", receiveString);
string errMsg = "";
if (AuthMessageParser.ParseMessage(receiveString, ref errMsg))
{
_label.Content = "Authentication Successful!";
_label.Foreground = new SolidColorBrush(Colors.Green);
}
else
{
_label.Content = "Authentication Unsuccessful: " + errMsg;
_label.Foreground = new SolidColorBrush(Colors.Red);
}
}
catch (Exception)
{
_label.Content = "Authentication Unsuccessful";
_label.Foreground = new SolidColorBrush(Colors.Red);
Console.WriteLine("UDP Auth Packet was NOT Received.");
}
u.Close();
}
Your UI thread is blocked by calls to things like _udpClient.Connect() and _udpClient.Send() (and the receives, too)
A workaround would be to leverage the task parallel library and perform communications asynchronously to avoid blocking the UI thread.
It will manage threads for you as long as you define tasks properly. Holler if you need an example.
protected void SomeButton_Click(Object sender, EventArgs e)
{
// Task off the work and do not wait, no blocking here.
Task.Run(PerformConnection);
}
private async Task PerformConnection()
{
// This method acts the way a thread should. We await the result of async comms.
// This will not block the UI but also may or may not run on its own thread.
// You don't need to care about the threading much.
var conn = await ListenerOrSomething.AwaitConnectionsAsync( /* ... */ );
// Now you have your result because it awaited.
using(var newClient = conn.Client())
{
var buffer = new byte[];
var recv = await newClient.ReceiveAsyncOrSomething(out buffer);
// Data received is not zero, process it or return
if(recv > 0)
newClient.Response = await ProcessRequest(buffer);
else
return;
}
}