| | |
How to send a process list over tcp connection
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 19
Reputation:
Solved Threads: 0
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:
Listen:
any suggestions?
Send:
C# Syntax (Toggle Plain Text)
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;
C# Syntax (Toggle Plain Text)
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?
•
•
Join Date: May 2008
Posts: 538
Reputation:
Solved Threads: 86
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.
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.
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.
If you want to do sockets then please upload a sample project so we can take a look at what is causing the error.
•
•
Join Date: Jul 2009
Posts: 19
Reputation:
Solved Threads: 0
I warn you, the code is extremely messy, its a work in progress:
Client:
Server:
Client:
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
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; }
•
•
Join Date: Jul 2009
Posts: 19
Reputation:
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?
Last edited by echo off; Jul 30th, 2009 at 10:05 pm.
•
•
Join Date: Jul 2009
Posts: 19
Reputation:
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.
Last edited by echo off; Jul 31st, 2009 at 12:58 pm.
![]() |
Similar Threads
- Hiding from Windows Process List (Pascal and Delphi)
- How To Send Data From Client To Server via Server Side (using C# TCP Socket) ? (C#)
- pop ups are back (Viruses, Spyware and other Nasties)
- This is a wierd one! (Windows NT / 2000 / XP)
- Zone Alarm Security Alerts. (Viruses, Spyware and other Nasties)
- weird remote tcp connection (Viruses, Spyware and other Nasties)
Other Threads in the C# Forum
- Previous Thread: how to link Toolstrip buttons to listbox items.
- Next Thread: Formatting data in Crystal reports
| Thread Tools | Search this Thread |
.net access algorithm animation array barchart bitmap box broadcast c# check checkbox client code combobox control conversion csharp custom database datagrid datagridview dataset datastructure datetime degrees development directrobot draganddrop drawing encryption enum event excel file form format forms function gdi+ hash httpwebrequest image index input install java label lisp list listbox mandelbrot math mouseclick mp3 mysql native operator packaging path photoshop picturebox pixelinversion post print process programming radians regex remote remoting richtextbox safari server sleep snooze socket sql statistics stream string table tables tcp text textbox thread time timer update usercontrol usercontrols validation visualstudio webbrowser wfa windows winforms wpf xml






