如何在unix中连接并使用TcpClient执行命令 [英] How to connect in unix and execute a command using TcpClient

查看:82
本文介绍了如何在unix中连接并使用TcpClient执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





低于我从网上获取的代码,以便从.net(c#)与unix通信

不幸的是,代码工作可以连接但返回垃圾结果。



它还显示我可以连接ip /端口。



目标是使用userid / pass连接到unix并执行命令(asnychronus)。需要帮助请咨询或指导。



以下是代码。



Hi,

Below the code that I've got from the net to communicate to unix from .net (c#)
unfortunately, the code works can connect but return a garbage result.

it also displays that I can connect with the ip/port.

The objective is to connect to unix with userid/pass and execute a command (asnychronus). Need help please advice or guide.

below is the code.

private static void doSomething(IAsyncResult result)
{
System.Threading.Thread newThread = new System.Threading.Thread((System.Threading.ThreadStart)delegate
            {
                //Do whatever you want in a new thread
                var ss = cli.Connected;
                Console.WriteLine("Connected " +ss );
                while (!cli.Connected)
                {
                    using (NetworkStream networkStream = cli.GetStream())
                    {
                        if (networkStream.CanRead)
                        {
                            // Reads NetworkStream into a byte buffer. 
                            byte[] bytes = new byte[cli.ReceiveBufferSize];

                            // Read can return anything from 0 to numBytesToRead.  
                            // This method blocks until at least one byte is read.
                            networkStream.Read(bytes, 0, (int)cli.ReceiveBufferSize);

                            // Returns the data received from the host to the console. 
                            string returndata = Encoding.UTF8.GetString(bytes);

                            Console.WriteLine("This is what the host returned to you: " + returndata);

                        }
                        else
                        {
                            Console.WriteLine("You cannot read data from this stream.");
                            cli.Close();

                            // Closing the tcpClient instance does not close the network stream.
                            networkStream.Close();
                            return;
                        }
                        networkStream.Close();

                        //if (networkStream.CanWrite)
                        //{
                        //    Byte[] sendBytes = Encoding.UTF8.GetBytes("Is anybody there?");
                        //    networkStream.Write(sendBytes, 0, sendBytes.Length);
                        //}
                        //else
                        //{
                        //    Console.WriteLine("You cannot write data to this stream.");
                        //    cli.Close();

                        //    // Closing the tcpClient instance does not close the network stream.
                        //    networkStream.Close();
                        //    return;
                        //}
                       
                    }
                }
            });
            newThread.Start(); //Start executing the code inside the thread
            //This code will still run while the newThread is running
            Console.ReadLine(); //Wait for user input
            newThread.Abort(); //Stop the thread when the user inserts any thing


           
        }
        private static void send(string msg)
        {
            AsyncCallback callBack = DoOtherOption; //Set the callback to the doSomething void
            cli.BeginConnect("10.10.10.10.", 23, callBack, cli); 
        }







谢谢




Thanks

推荐答案

这个 [ ^ ]应该给你一个良好的开端
This[^] should give you a good start


这篇关于如何在unix中连接并使用TcpClient执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆