How to send a process list over tcp connection

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 19
Reputation: echo off is an unknown quantity at this point 
Solved Threads: 0
echo off echo off is offline Offline
Newbie Poster

How to send a process list over tcp connection

 
0
  #1
Jul 30th, 2009
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:
  1. case "procls":
  2. Process[] allProcs = Process.GetProcesses();
  3. foreach (Process thisProc in allProcs)
  4. {
  5. string procName = thisProc.ProcessName;
  6. int procID = thisProc.Id;
  7. Console.WriteLine("Process: {0}, ID: {1}", procName, procID);
  8. string proc = ("Process: {0}, ID: {1}" + procName + procID);
  9. byte[] lst = System.Text.Encoding.ASCII.GetBytes(proc);
  10. stream.Write(lst,0,lst.Length);
  11. }
  12. break;
Listen:
  1. stm.Write(ba, 0, ba.Length);
  2. byte[] bb = new byte[100];
  3. int k = stm.Read(bb, 0, 100);
  4. for (int i = 0; i < k; i++)
  5. Console.WriteLine(Convert.ToChar(bb[i]));

any suggestions?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 538
Reputation: Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough Murtan is a jewel in the rough 
Solved Threads: 86
Murtan Murtan is offline Offline
Posting Pro

Re: How to send a process list over tcp connection

 
0
  #2
Jul 30th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: echo off is an unknown quantity at this point 
Solved Threads: 0
echo off echo off is offline Offline
Newbie Poster

Re: How to send a process list over tcp connection

 
0
  #3
Jul 30th, 2009
Could you explain that a little more? Im still very new to C# and Im not sure I fully understand.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: How to send a process list over tcp connection

 
0
  #4
Jul 30th, 2009
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: echo off is an unknown quantity at this point 
Solved Threads: 0
echo off echo off is offline Offline
Newbie Poster

Re: How to send a process list over tcp connection

 
0
  #5
Jul 30th, 2009
I warn you, the code is extremely messy, its a work in progress:
Client:
  1. while (true)
  2. {
  3. try
  4. {
  5.  
  6. TcpClient tcpclnt = new TcpClient();
  7. //Bad connection time out
  8. try
  9. {
  10.  
  11. {
  12. tcpclnt.Connect(des, 811); // use the ipaddress as in the server program
  13. //Console.WriteLine("Connected");
  14.  
  15. }
  16.  
  17. }
  18. catch (Exception c)
  19. {//goes back to manual address input
  20. if (c == null)
  21. goto Input;
  22. else
  23. continue;
  24. }
  25. //text prompt
  26. Console.Write("./terminal|");
  27. String str = Console.ReadLine();
  28.  
  29. Stream stm = tcpclnt.GetStream();
  30. ASCIIEncoding asen = new ASCIIEncoding();
  31. byte[] ba = asen.GetBytes(str

Server:
  1. TcpListener server = null;
  2. try
  3. {
  4. // Set the TcpListener on port 13000.
  5. Int32 port = 811;
  6. IPAddress localAddr = IPAddress.Any;
  7.  
  8. // TcpListener server = new TcpListener(port);
  9. server = new TcpListener(localAddr, port);
  10.  
  11. // Start listening for client requests.
  12. server.Start();
  13.  
  14. // Buffer for reading data
  15. Byte[] bytes = new Byte[256];
  16. String data = null;
  17.  
  18. // Enter the listening loop.
  19. while (true)
  20. {
  21. //Console.Write("Waiting for a connection... ");
  22.  
  23. // Perform a blocking call to accept requests.
  24. TcpClient client = server.AcceptTcpClient();
  25. Console.WriteLine("Connected!");
  26.  
  27. data = null;
  28.  
  29. // Get a stream object for reading and writing
  30. NetworkStream stream = client.GetStream();
  31.  
  32. int i;
  33.  
  34. // Loop to receive all the data sent by the client.
  35. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
  36. {
  37. // Translate data bytes to a ASCII string.
  38. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
  39.  
  40. switch (data.Substring(0, 6))
  41. {
  42. case "send /":
  43. MessageBox.Show(data.Substring(6), " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  44. break;
  45. case "errmsg":
  46. MessageBox.Show(data.Substring(8), "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  47. break;
  48. case "procls":
  49. Process[] allProcs = Process.GetProcesses();
  50. foreach (Process thisProc in allProcs)
  51.  
  52. {
  53. string procName = thisProc.ProcessName;
  54. int procID = thisProc.Id;
  55. Console.WriteLine("Process: {0}, ID: {1}", procName, procID);
  56. string proc = ("Process: {0}, ID: {1}" + procName + procID);
  57. byte[] lst = System.Text.Encoding.ASCII.GetBytes(proc);
  58. stream.Write(lst, 0, lst.Length);
  59. }
  60. break;
  61.  
  62.  
  63.  
  64.  
  65. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: echo off is an unknown quantity at this point 
Solved Threads: 0
echo off echo off is offline Offline
Newbie Poster

Re: How to send a process list over tcp connection

 
0
  #6
Jul 30th, 2009
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?
Last edited by echo off; Jul 30th, 2009 at 10:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: echo off is an unknown quantity at this point 
Solved Threads: 0
echo off echo off is offline Offline
Newbie Poster

Re: How to send a process list over tcp connection

 
0
  #7
Jul 30th, 2009
no, I should make it listen until there is no response
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: echo off is an unknown quantity at this point 
Solved Threads: 0
echo off echo off is offline Offline
Newbie Poster

Re: How to send a process list over tcp connection

 
0
  #8
Jul 31st, 2009
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.
Last edited by echo off; Jul 31st, 2009 at 12:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 19
Reputation: echo off is an unknown quantity at this point 
Solved Threads: 0
echo off echo off is offline Offline
Newbie Poster

Re: How to send a process list over tcp connection

 
0
  #9
Jul 31st, 2009
Why do my posts disappear and then reappear?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: How to send a process list over tcp connection

 
0
  #10
Jul 31st, 2009
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.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC