Oliver_FF
New Member
- Joined
- Oct 15, 2006
- Messages
- 544 (0.08/day)
Processor | Intel q9400 @ stock |
---|---|
Motherboard | Lanparty P45-T2RS |
Cooling | Zalman CNPS-9500 |
Memory | 8GB OCZ PC2-6400 |
Video Card(s) | BFG Nvidia GTX285 OC |
Storage | 1TB, 500GB, 500GB |
Display(s) | 20" Samsung T200HD |
Case | Antec Mini P180 |
Audio Device(s) | Sound Blaster X-Fi Elite Pro |
Power Supply | 700w Hiper |
Software | Ubuntu x64 virtualising Vista |
Here's another interesting thing that you can do with sockets in C or C++. If any of you are familiar with the concepts of SSH or Telnet you'll get this straight away. Every console application has three IO paths...
1. stdIn is the standard input - aka when you type something into the console and it goes to your program.
2. stdOut is the standard output - aka when your program outputs feedback the data is written to stdOut, then its contents are written to the console you are working in.
3. stdErr is the standard error - aka if your program crashes and burns you can tell the OS roughly what happened (in an ideal world...)
Well with C or C++ in Windows you can redirect stdIn and stdOut of any console application to go through a network socket. To write to stdIn you simply send data over the socket. To read from stdOut you simply read data off of the socket. Pretty cool, eh? The most obvious use of something like this is for remote access to your computer - create a host program that sits around waiting for you to connect to it and redirect output to, say, cmd.exe so you have "complete" control over your PC remotely. "Complete" because cmd.exe sucks
MSDN on PROCESS_INFORMATION - http://msdn.microsoft.com/en-us/library/ms684873(VS.85).aspx
MSDN on STARTUPINFO - http://msdn.microsoft.com/en-us/library/ms686331(VS.85).aspx
The same effect can be done using C#. The code is easier to read and follow in C# but also it's trickier because you have to transfer the data between the socket yourself... At least that's what i'm assuming when i attempted it...
1. stdIn is the standard input - aka when you type something into the console and it goes to your program.
2. stdOut is the standard output - aka when your program outputs feedback the data is written to stdOut, then its contents are written to the console you are working in.
3. stdErr is the standard error - aka if your program crashes and burns you can tell the OS roughly what happened (in an ideal world...)
Well with C or C++ in Windows you can redirect stdIn and stdOut of any console application to go through a network socket. To write to stdIn you simply send data over the socket. To read from stdOut you simply read data off of the socket. Pretty cool, eh? The most obvious use of something like this is for remote access to your computer - create a host program that sits around waiting for you to connect to it and redirect output to, say, cmd.exe so you have "complete" control over your PC remotely. "Complete" because cmd.exe sucks
Code:
#using <windows.h> //sorry not entirely sure where inside there the specific headers are :(
//The socket to use
Socket thisSocket;
//Information about the process we are redirecting.
PROCESS_INFORMATION pi;
//Information about where the IO goes ;)
STARTUPINFO si;
/* Initialise the socket somewhere here */
//cb is the size of the STARTUPINFO structure
si.cb = sizeof( si );
//Tell it we do not want to see the application we are redirecting to/from
si.wShowWindow = SW_HIDE;
//Tell it we want to be able to use .wShowWinwow, .hStdInput, .hstdOutput, .hstdError members.
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
//Tell it we want the standard input to come from thisSocket
si.hStdInput = (HANDLE)thisSocket;
//Tell it we want the standard output to go to thisSocket
si.hStdOutput = (HANDLE)thisSocket;
//Tell it we want the stardard error to appear on thisSocket
si.hStdError = (HANDLE)thisSocket;
//Spawn the process we want to redirect using the information we just declared.
CreateProcess( NULL, "cmd.exe", NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi );
/* From now everything is set up and ticking over. */
//Wait until the process we started ends - then we can quit.
WaitForSingleObject( pi.hProcess, INFINITE );
//Close the process handles before exiting.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
MSDN on STARTUPINFO - http://msdn.microsoft.com/en-us/library/ms686331(VS.85).aspx
The same effect can be done using C#. The code is easier to read and follow in C# but also it's trickier because you have to transfer the data between the socket yourself... At least that's what i'm assuming when i attempted it...
Code:
using System.Diagnostics;
process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "cmd.exe";
process.Start();
//use process.StandardInput.WriteLine(); to write to the applications stdIn
//use process.StandardOutput.ReadLine(); to read from the applications stdOut