Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.

C#
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
Posted

1 solution

This[^] should give you a good start
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900