Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending data from Windows application to console application

Tags:

c#

In my application I have created a GUI using a Windows form where I have one ListBox with values and one button named sendto. The user selects from the ListBox and clicks on the sendto button. On the click of this button the selected value from the ListBox should be displayed on the console application. Here the GUI that is developed in the Windows form acts as server and the console application acts as client. How can I send data from the Windows form to the console application in C#? I'm new to C#.

like image 510
Deepu Avatar asked May 28 '26 13:05

Deepu


2 Answers

I was responding your question :socket programming using c# ... but some uncomprehensive people close your question...

I know that your probably a new programmer. But i find that your good to ask questions to make you develop your self to become a better programmer. I'll going to vote you up ! :D

See following code, it would help you to have fun with a mini client server application.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PostSharp.Aspects;
using System.Diagnostics;
using System.IO;

namespace TestCode
{
    public class Program
    {
        public static StreamReader ServerReader;

        public static StreamWriter ServerWriter;

        static void Main(string[] args)
        {
            // here are all information to start your mini server
            ProcessStartInfo startServerInformation = new ProcessStartInfo(@"c:\path\to\serverApp.exe");

            // this value put the server process invisible. put it to false in while debuging to see what happen
            startServerInformation.CreateNoWindow = true;

            // this avoid you problem
            startServerInformation.ErrorDialog = false;

            // this tells that you whant to get all connections from the server
            startServerInformation.RedirectStandardInput = true;
            startServerInformation.RedirectStandardOutput = true;

            // this tells that you whant to be able to use special caracter that are not define in ASCII like "é" or "ï"
            startServerInformation.StandardErrorEncoding = Encoding.UTF8;
            startServerInformation.StandardOutputEncoding = Encoding.UTF8;

            // start the server app here
            Process serverProcess = Process.Start(startServerInformation);

            // get the control of the output and input connection
            Program.ServerReader = serverProcess.StandardOutput;
            Program.ServerWriter = serverProcess.StandardInput;

            // write information to the server
            Program.ServerWriter.WriteLine("Hi server im the client app :D");

            // wait the server responce
            string serverResponce = Program.ServerReader.ReadLine();

            // close the server application if needed
            serverProcess.Kill();
        }
    }
}

note that in the server application, you can receive client information by using:

string clientRequest = Console.ReadLine();
Console.WriteLine("Hi client i'm the server :) !");
like image 102
Nicolas Charette-Naud Avatar answered Jun 04 '26 18:06

Nicolas Charette-Naud


You can use pipes, please glance over MSDN article for a local communication or for a network communcation MSDN article.

like image 45
user854301 Avatar answered Jun 04 '26 17:06

user854301



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!