0

I am writing a windows service and a separate GUI program and i want them communicating with named pipes. The windows service is the pipe server and the GUI will be the client.

The problem is that when my client connects to the pipeServer my BeginRead async-callback keeps firing. The client hasn't send any data.

    const int buffersize= 255;
    byte[] buffer = new byte[buffersize];

    private void newConnection(IAsyncResult iar) {
       //stop waiting for connection
       pipeServer.EndWaitForConnection(iar);

        //register callback
       beginreadResult = server.BeginRead(buffer, 0, buffFormaat, newMessage, pipeServer);

    }


    public void newMessage(IAsyncResult r)
    {

            try
            {
                pipeServer.EndRead(beginreadResult);
            }catch(Exception e){
                Console.WriteLine(e.Message);
                System.Environment.Exit(0);
            }
                string message = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                Console.WriteLine("new message: " + message);


            beginreadResult= server.BeginRead(buffer, 0, buffersize, newMessage, server);

    }

I can't figure out why the newMessage() callback keeps being called, anybody ideas?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What does `pipeServer.EndRead` return. Is it `<= 0` ? – EZI Jul 12 '14 at 17:44
  • `pipeServer.EndRead` returns 0, but why is the callback called if there is no data? Isn't that the point of async reading? – Christiaan Ven Jul 12 '14 at 17:50
  • 1
    It clearly says to you *other end closed the connection,there is no more data to read* but you keep on reading... – EZI Jul 12 '14 at 17:59
  • What value does `buffFormaat` have? 0? – usr Jul 12 '14 at 18:01
  • Thanks guy's found out my pipe was closed on the client side. This because I used a 'using' block where my stream closed (http://stackoverflow.com/questions/9451429/how-to-continue-sending-reading-messages-from-named-pipes-streams) – Christiaan Ven Jul 12 '14 at 18:14

0 Answers0