nunntb473 0 Newbie Poster

So I've always toyed around with C# code with the idea of gaming. Lately I've been dabbling in communication. Initially I tried sockets and TcpClient and that was hit and miss. So I decided to give pipes a go. Frankly I dont have much of a preference of one of the other. I just wanted something I could eventually use over the internet. Extremely long term I plan to add encryption, a chat, movement for players (probably something UDP). For the short term I'm basically just trying to achieve something TCP/IP where I can easily send something like a key value pair along the lines of command/relavent information. Ex: attack/weapon, attack/fireball, walk/north. I'll likely identify the player based off of their IP or something.

Anyway. I am having trouble getting any communication between two programs with named pipes, both on the same machine. It seems to either hang indefinitely during a connect attempt or error out saying I have an invalid name or syntax for the target pipe.

I've tried changing pipe directions to their respective In/Out instead of two way. Tried changing pipe names and path I use to reference the pipe names a couple of dozen ways now. I recall sockets don't typically have firewall issues when performing a local communication, are pipes the same?

I have the server and client each as their own program with WinForms as their base. They are each set to run as separate threads and begin waitingForConnection and Writing/Sending on the click of a button. Classes / methods are static at the moment.

One of the buttons:

new Thread(new ThreadStart(CommPipe2.Comm)).Start();

Server:

public static class CommPipe2
    {

        public static void Comm()
        {
            NamedPipeServerStream npss;
            npss = new NamedPipeServerStream("TestServer", PipeDirection.InOut);


            npss.WaitForConnection();

            try
            {
                //Next line never occurs
                MessageBox.Show("Connection accepted");
                StreamReader sr = new StreamReader(npss);


                string temp;
                //temp = sr.ToString();
                temp = sr.ReadToEnd();
                MessageBox.Show("Server: Received_ " + temp);
                //temp = npss.
            }

            catch (Exception e)
            {
                MessageBox.Show("Server: " + e.ToString());
            }
        }

Client:

public static class CommPipe2
    {


        public static void send()
        {
            try
            {
                NamedPipeClientStream npcs;
                // "."             @"\\.\pipe\TestServer"        "\\\\.\\pipe\\TestServer"    "TestServer"
                npcs = new NamedPipeClientStream(".", "TestClient", PipeDirection.InOut);

                string temp; 
                temp = "test567";
                byte[] buffer = new byte[1024];

                buffer = Encoding.ASCII.GetBytes(temp);

                //Running debug step by step shows that it hangs on the next line
                npcs.Connect();
                MessageBox.Show("Client: Sending_ " + buffer);
                npcs.Write(buffer, 0, buffer.Length);

                using (StreamWriter sw = new StreamWriter(npcs))
                {


                }

            }

            catch (Exception e)
            {
                MessageBox.Show("Client: " + e.ToString());

            }
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.