943,877 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1463
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 30th, 2009
0

How to send a process list over tcp connection

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
echo off is offline Offline
19 posts
since Jul 2009
Jul 30th, 2009
0

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.
Reputation Points: 344
Solved Threads: 116
Practically a Master Poster
Murtan is offline Offline
670 posts
since May 2008
Jul 30th, 2009
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
echo off is offline Offline
19 posts
since Jul 2009
Jul 30th, 2009
0

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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Jul 30th, 2009
0

Re: How to send a process list over tcp connection

I warn you, the code is extremely messy, its a work in progress:
Client:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
echo off is offline Offline
19 posts
since Jul 2009
Jul 30th, 2009
0

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?
Last edited by echo off; Jul 30th, 2009 at 10:05 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
echo off is offline Offline
19 posts
since Jul 2009
Jul 30th, 2009
0

Re: How to send a process list over tcp connection

no, I should make it listen until there is no response
Reputation Points: 10
Solved Threads: 0
Newbie Poster
echo off is offline Offline
19 posts
since Jul 2009
Jul 31st, 2009
0

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.
Last edited by echo off; Jul 31st, 2009 at 12:58 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
echo off is offline Offline
19 posts
since Jul 2009
Jul 31st, 2009
0

Re: How to send a process list over tcp connection

Why do my posts disappear and then reappear?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
echo off is offline Offline
19 posts
since Jul 2009
Jul 31st, 2009
0

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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: how to link Toolstrip buttons to listbox items.
Next Thread in C# Forum Timeline: Formatting data in Crystal reports





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC