We help IT Professionals succeed at work.

redirect a console app's input/output to a listening socket.

campid
campid asked
on
hi guys,
i would like to make a console app which will make an exsisting console app available over the network by redirect its input/output handles to a listening socket on another machine.

for example

c:\redirect.exe -a:myoldapp.exe -h:173.8.63.201 -p:9999

redirect.exe = console app i would like to make
-a = console app to redirect
-h = IP of listening socket(host)
-p = port of listening socket

I've have seen similar things done with anonymous pipes. I'm not sure if it would be better to run cmd.exe first instead of myoldapp.exe, that way it would make the application cross platform. If this can be done I guess it would be better.

cheers guys


Comment
Watch Question

The following code shows how to redirect the output into a file. You can use it as the basis for your application, i.e. read from the file and send to the socket.

     // Open the target file. Specify the ability to inherit the handle.
     SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
     HANDLE hOutputFile = CreateFile("OutputFile", GENERIC_ALL, 0, &sa, CREATE_ALWAYS, 0, 0);

     // Tell the new process to use the file as the standard output.
     STARTUPINFO si = {0};
     si.dwFlags = STARTF_USESTDHANDLES;
     si.hStdOutput = hOutputFile;

     // Start the process.
     PROCESSINFORMATION pi = {0};
     HANDLE hProcess = CreateProcess("file.exe", NULL, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);

     // Don't forget.
     CloseHandle(hOutputFile);
     CloseHandle(hProcess);


HTH

Commented:
The solution above is essentially equivalent to UNIX fork(); I don't know what extra flexibility you can achieve if you run "CMD.EXE -c myoldapp.exe".

But you should be aware that a consol application may override the default stdin and stdout it recieves, and connect to the true consol window.

Author

Commented:
alexcohn, i didnt mean run:

c:\redirect.exe cmd.exe -a:myoldapp.exe -h:173.8.63.201 -p:9999

just

c:\redirect.exe -a:cmd.exe -h:173.8.63.201 -p:9999

What I ment was to just redirect cmd.exe and then execute myoldapp from the redirected shell. this way the program should use its parents handles.

Author

Commented:
AssafLavie,

I think your on the right lines but i would need two memory streams rather then using files which would be messy. i.e one for input and the other for the output.

Once the programs handles have been redirected to memory streams it would be easy to feed this into sockets.

cheers

Commented:
Oh, I see - you want to start the session, and send the string "myoldapp args" from 173.8.63.201 server via port 9999.

Yes, you can use the sockets and file handles interchangeably with WINNT API calls (one layer below WIN32 API), if redirect.exe is run on Windows NT.

What you want to achieve, is essentially a telnet daemon, isn't it? Maybe, it will be easier for you just to install some existing software, e.g. http://www.midasoft.com/koala98/koalal_d.shtml

Author

Commented:
alex
do you know of any products which supply source code that I can look at? could be 9x and nt
No problem. The only reason I posted code that showed how to direct the output into a file was, well ,becuase I had it ready.
You can just as easy redirect the output into any other HANDLE, such as a pipe, mailslot, etc.

Author

Commented:
AssafLavie,

Could you show me how to code your example using two pipe handles. one for input and the other for the output.
I'm very new to C++, just trying to move over from Delphi. I am currently trialing MS Visual C++ and Dev C++.

cheers

Commented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to refund the points and PAQ at zero points since nobody had a satisfying answer for you.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner

Commented:
I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. Unless there is objection or further activity,  I will suggest to refund the points and PAQ at zero points since nobody had a satisfying answer for you.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Per recommendation, points refunded and question closed by
Netminder
CS Moderator

Explore More ContentExplore courses, solutions, and other research materials related to this topic.