N
N
Nevea2021-10-08 11:28:21
C++ / C#
Nevea, 2021-10-08 11:28:21

Redirecting new Process() OutputDataReceived to socket?

Good afternoon, I'm making a client-server application.
The client starts the program and the command output is redirected to the server via socket.

Process run = new Process();
                p.StartInfo.FileName = "cmd.exe"
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true; 
                DataReceivedEventHandler Fun= (o, e) => {
                     StringBuilder strOutput = new StringBuilder();
                    if (!String.IsNullOrEmpty(e.Data))
                    {      
                           try
                            {   
                            strOutput.Append('\n'+e.Data);        
                            }
                            catch (Exception e) { 
                                    socket.Send(Encoding.UTF8.GetBytes(e_aplication.ToString());
                            }
                            
                    }   
                    else {
                         socket.Send(Encoding.UTF8.GetBytes(strOutput.ToString())); 
                         strOutput.Clear();
                        
                    }       
                };
                p.OutputDataReceived += Fun;
                p.Start();
                p.BeginOutputReadLine();

So the data is successfully transferred, but sometimes situations arise that the DataReceivedEventHandler is called several times, and accordingly socket.Send() is called several times; for example, if you execute the DIR command Accordingly, the essence of the question is how to collect all the transmitted data into one and then send socket.Send(fulldata) ? I apologize in advance for the stupid question, I just started learning C#.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
d-stream, 2021-10-08
@d-stream

Actually, do it as it is written:
collect in the buffer, and then send the buffer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question