954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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?

echo off
Newbie Poster
19 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Murtan
Practically a Master Poster
671 posts since May 2008
Reputation Points: 344
Solved Threads: 116
 

Could you explain that a little more? Im still very new to C# and Im not sure I fully understand.

echo off
Newbie Poster
19 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

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
Newbie Poster
19 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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
Newbie Poster
19 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

no, I should make it listen until there is no response

echo off
Newbie Poster
19 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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
Newbie Poster
19 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Why do my posts disappear and then reappear?

echo off
Newbie Poster
19 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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.

sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
 

So no one has anything else?

echo off
Newbie Poster
19 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You