How to talk with an external console?

  • Thread starter Thread starter ~toki
  • Start date Start date
T

~toki

I have a full work app (server and client) that talk between two executables
trought Console.WriteLine(); & Console.ReadLine();

I have the source code of the server.

I need to make the client. To do this i have

Console.WriteLine("Hi server");

while (true)
{
String command = "";
try
{
command = Console.ReadLine();
if (command != null)
if(command.Length > 0)
System.Console.WriteLine("Server response: " + command);
}
catch (IOException ex)
{
Console.WriteLine(ex.StackTrace);
outWriter.WriteLine(ex.StackTrace);
}
}

But never can read anything.

I know good how to make a client server with TCP. But with Console appears
to be different.
When is "established" the connection?
 
~toki said:
I have a full work app (server and client) that talk between two executables
trought Console.WriteLine(); & Console.ReadLine(); ....
But never can read anything.

I know good how to make a client server with TCP. But with Console appears
to be different.
When is "established" the connection?

As far as I know, different applications cannot communicate through a
console. Do you have some information indicating that they can?
 
I have the server source in C# and the client is an .exe (made in C)
I see how talk with a "debug windows" of the client.
The client send two tokens to the server.
And the server response with a token (that exists in the source code) with
Console.Write(returnToken);
It works fine.
I can't run the C# server from the MS IDE to stop and debug when the client
send the token.

*The client run the server when it start.

I have tried do it with System.Diagnostic.Process.Start("server.exe") but it
don't work.
 
Ah! Creo que lo que Ud. intenta es que quiere comunicar con otro proceso
por "standard input" y "standard output". No se' como se hace.

Anyone -- How do you get a process to read and write to another process's
stdin and stdout? If you can?
 
Michael,

Both the Process and Console classes have properties and methods to
work with standard input/output/error. You could try some match making
between the two classes. I'm too tired tonight to give it a spin´, but let
us
know how it works out.

HTH,

//Andreas
 
Pienso que si, consegui el codigo fuente pero no lo puedo compilar.

Here some relevant lines of the client source code. (i don't know C)
/*
* The steps for redirecting child's STDIN:
* 1. Create anonymous pipe to be STDIN for child.
* 2. Create a noninheritable duplicate of write handle,
* and close the inheritable write handle.
*/
/* Create a pipe for the child's STDIN. */
if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0)) {
return GetLastError();
}
....
fSuccess = CreateProcess(NULL,
cmdLine, /* command line */
NULL, /* process security attributes */
NULL, /* primary thread security attrs */
TRUE, /* handles are inherited */
DETACHED_PROCESS|CREATE_NEW_PROCESS_GROUP,
NULL, /* use parent's environment */
NULL,
&siStartInfo, /* STARTUPINFO pointer */
&piProcInfo); /* receives PROCESS_INFORMATION */
....
 
Thanks, it don't work (yet) but i understand where start.

Something like this..

System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.UseShellExecute = false;
info.CreateNoWindow = false;
info.FileName = app;
info.RedirectStandardOutput = true;
p.StartInfo = info;
p.Start();
p.WaitForExit();
Console.WriteLine( p.StandardOutput.ReadToEnd() );

I will investigate it...
 
It works. Here's the code...

System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.UseShellExecute = false;
info.CreateNoWindow = false;
info.FileName = app; // App filename
info.RedirectStandardOutput = true;
p.StartInfo = info;
p.Start();

p.StandardInput.WriteLine("Hi server. Lets start!!");

while (true)
{
String command = "";
try
{
command = p.StandardOutput.ReadLine();
if (command != null)
if(command.Length > 0)
System.Console.WriteLine(command);
}
catch (IOException ex)
{
// Print an exception to command line and log file
Console.WriteLine(ex.StackTrace);
}
}

"~toki" <pedorro77.hotmail.com> escribió en el mensaje Thanks, it don't work (yet) but i understand where start.
Something like this..
System.Diagnostics.Process p = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.UseShellExecute = false;
info.CreateNoWindow = false;
info.FileName = app;
info.RedirectStandardOutput = true;
p.StartInfo = info;
p.Start();
p.WaitForExit();
Console.WriteLine( p.StandardOutput.ReadToEnd() );
I will investigate it...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top