Example #1
0
        public static void StartConnecting()
        {
            try
            {
                pipeClient = new NamedPipeClientStream(".", "k8mkZR5Apk",
                                                       PipeDirection.InOut, PipeOptions.None,
                                                       TokenImpersonationLevel.Impersonation);

                //Console.WriteLine("Connecting to server...\n");
                pipeClient.Connect();
                while (true)
                {
                    if (pipeClient.IsConnected)
                    {
                        StreamString ss = new StreamString(pipeClient);

                        string dataReceive = ss.ReadString();
                        DataReceivedOperation(dataReceive);
                        System.Threading.Thread.Sleep(1);
                        //System.Diagnostics.Debug.WriteLine(dataReceive);
                        if (!isClose)
                        {
                            string dataToSend = LoadDataToSend();
                            ss.WriteString(dataToSend);
                            pipeClient.WaitForPipeDrain();
                            System.Threading.Thread.Sleep(1);
                        }
                        else
                        {
                            ss.WriteString("ENDSTREAMING");
                            pipeClient.WaitForPipeDrain();
                            string str = "";
                            while (str != "OK")
                            {
                                str = ss.ReadString();
                            }
                            break;
                        }
                    }
                    else
                    {
                        break;
                    }
                    System.Threading.Thread.Sleep(1);
                }
                pipeClient.Close();
                isClosed = true;
            }
            catch (Exception)
            {
            }
        }
Example #2
0
        public static void StartListening()
        {
            NamedPipeServerStream pipeServer =
                new NamedPipeServerStream("k8mkZR5Apk", PipeDirection.InOut, 1);

            try
            {
                // Wait for a client to connect
                pipeServer.WaitForConnection();


                StreamString ss = new StreamString(pipeServer);

                while (true)
                {
                    if (pipeServer.IsConnected)
                    {
                        //try
                        //{
                        string dataToSend = LoadDataToSend();
                        ss.WriteString(dataToSend);
                        System.Threading.Thread.Sleep(1);
                        //}
                        //catch { }
                        string dataReceive = ss.ReadString();
                        if (dataReceive == "ENDSTREAMING")
                        {
                            ss.WriteString("OK");
                            pipeServer.WaitForPipeDrain();
                            System.Threading.Thread.Sleep(1);
                            ConnectionClose();
                            break;
                        }
                        else
                        {
                            DataReceivedOperation(dataReceive);
                            System.Threading.Thread.Sleep(1);
                        }
                    }
                    else
                    {
                        break;
                    }
                    System.Threading.Thread.Sleep(1);
                }
            }
            catch (IOException e)
            {
                Console.WriteLine("ERROR: {0}", e.Message);
            }
            pipeServer.Close();
        }