DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   How to send a process list over tcp connection (http://www.daniweb.com/forums/thread207993.html)

echo off Jul 30th, 2009 1:45 pm
How to send a process list over tcp connection
 
i can get it to write one line, but then it is "forcibly stopped" probably because of the way that the client listens for a response.

Send:
case "procls":
                                Process[] allProcs = Process.GetProcesses();
                                foreach (Process thisProc in allProcs)
                                {
                                    string procName = thisProc.ProcessName;
                                    int procID = thisProc.Id;
                                    Console.WriteLine("Process: {0}, ID: {1}", procName, procID);
                                    string proc = ("Process: {0}, ID: {1}" + procName + procID);
                                    byte[] lst = System.Text.Encoding.ASCII.GetBytes(proc);
                                    stream.Write(lst,0,lst.Length);
                                }
                                break;
Listen:
stm.Write(ba, 0, ba.Length);
                            byte[] bb = new byte[100];
                            int k = stm.Read(bb, 0, 100);
                            for (int i = 0; i < k; i++)
                                Console.WriteLine(Convert.ToChar(bb[i]));

any suggestions?

Murtan Jul 30th, 2009 4:30 pm
Re: How to send a process list over tcp connection
 
I think the forcibly stopped comes from the reader application reading one line and exiting.

Can you put the read into a loop of some form?

If you're going to write to the stream more than once, you should probably read more than once.

You'll probably want to add detection for the stream closing to the reader after you start the loop.

And the read loop should block somewhere, either for the stream to be ready or for a time period. You don't want it running in a tight loop burning cpu cycles without a purpose.

echo off Jul 30th, 2009 5:21 pm
Re: How to send a process list over tcp connection
 
Could you explain that a little more? Im still very new to C# and Im not sure I fully understand.

sknake Jul 30th, 2009 5:58 pm
Re: How to send a process list over tcp connection
 
I think Murtan is right. Why don't you take a look in to "remoting" in C#. It basically allows you to call methods running on another computer. IE you have a 'server' application running on a remote machine and you can call methods and pass values. In this case you could make a serialized process list and transmit that across the pipe which would abstract away having to deal with sockets.

If you want to do sockets then please upload a sample project so we can take a look at what is causing the error.

echo off Jul 30th, 2009 6:45 pm
Re: How to send a process list over tcp connection
 
I warn you, the code is extremely messy, its a work in progress:
Client:
 while (true)
            {
                try
                {
                 
                    TcpClient tcpclnt = new TcpClient();
                    //Bad connection time out
                    try
                    {
                     
                        {
                            tcpclnt.Connect(des, 811); // use the ipaddress as in the server program
                            //Console.WriteLine("Connected");
                           
                        }
                     
                    }
                    catch (Exception c)
                    {//goes back to manual address input
                        if (c == null)
                            goto Input;
                        else
                            continue;
                    }
                    //text prompt
                    Console.Write("./terminal|");
                    String str = Console.ReadLine();
                 
                    Stream stm = tcpclnt.GetStream();
                    ASCIIEncoding asen = new ASCIIEncoding();
                    byte[] ba = asen.GetBytes(str

Server:
 TcpListener server = null;
            try
            {
                // Set the TcpListener on port 13000.
                Int32 port = 811;
                IPAddress localAddr = IPAddress.Any;

                // TcpListener server = new TcpListener(port);
                server = new TcpListener(localAddr, port);

                // Start listening for client requests.
                server.Start();

                // Buffer for reading data
                Byte[] bytes = new Byte[256];
                String data = null;

                // Enter the listening loop.
                while (true)
                {
                    //Console.Write("Waiting for a connection... ");

                    // Perform a blocking call to accept requests.
                    TcpClient client = server.AcceptTcpClient();
                    Console.WriteLine("Connected!");

                    data = null;

                    // Get a stream object for reading and writing
                    NetworkStream stream = client.GetStream();

                    int i;

                    // Loop to receive all the data sent by the client.
                    while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        // Translate data bytes to a ASCII string.
                        data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

                            switch (data.Substring(0, 6))
                        {
                            case "send /":
                                MessageBox.Show(data.Substring(6), " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                break;
                            case "errmsg":
                                MessageBox.Show(data.Substring(8), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                break;
case "procls":
                                Process[] allProcs = Process.GetProcesses();
                                foreach (Process thisProc in allProcs)
                                   
                                        {
                                            string procName = thisProc.ProcessName;
                                            int procID = thisProc.Id;
                                            Console.WriteLine("Process: {0}, ID: {1}", procName, procID);
                                            string proc = ("Process: {0}, ID: {1}" + procName + procID);
                                            byte[] lst = System.Text.Encoding.ASCII.GetBytes(proc);
                                            stream.Write(lst, 0, lst.Length);
                                        }
                                        break;
                                 
                                     
                                 
                               
                        }

echo off Jul 30th, 2009 9:05 pm
Re: How to send a process list over tcp connection
 
I was messing around with the code that listens for a response and made it send commands...somehow. i had to restore to a backup of my program so Im not sure whats different. I do have an idea though. What if, I requested the total number of processes from the server computer, and with that number, my program knew how many responses to listen for?

echo off Jul 30th, 2009 9:32 pm
Re: How to send a process list over tcp connection
 
no, I should make it listen until there is no response

echo off Jul 31st, 2009 11:58 am
Re: How to send a process list over tcp connection
 
This is weird, I made a post last night and its gone. i was messing around with the tcp connection and nothing was being sent so i had to restore to a backup I made. Im thinking that their should be a way to make it so that it stays open until there is no response.

echo off Jul 31st, 2009 11:59 am
Re: How to send a process list over tcp connection
 
Why do my posts disappear and then reappear?

sknake Jul 31st, 2009 12:08 pm
Re: How to send a process list over tcp connection
 
This sounds like a browser error to me. Maybe you closed your browser before the submit? I don't know.

If a moderator deletes your post you will receive a private message in the forum stating that it has been deleted. Check your message box.


All times are GMT -4. The time now is 4:44 pm.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC