int ACE_TMAIN (int /* argc */, ACE_TCHAR * /* argv */ []) { ACE_SPIPE_Acceptor acceptor; ACE_SPIPE_Stream new_stream; char buf[BUFSIZ]; int n; const ACE_TCHAR *rendezvous = MAKE_PIPE_NAME ("acepipe"); // Initialize named pipe listener. if (acceptor.open (ACE_SPIPE_Addr (rendezvous)) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("open")), 1); for (;;) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("waiting for connection\n"))); // Accept a client connection. if (acceptor.accept (new_stream, 0) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("accept")), 1); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Accepted connection\n"))); while ((n = new_stream.recv (buf, sizeof buf)) > 0) { ACE_OS::fprintf (stderr, "%s\n", buf); ACE_OS::write (ACE_STDOUT, buf, n); } if (n == -1) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("End of connection. Closing handle\n"))); new_stream.close (); } } ACE_NOTREACHED(return 0); }
static void * server (void *) { ACE_SPIPE_Acceptor acceptor; ACE_SPIPE_Stream new_stream; char buf[BUFSIZ]; const char *t = ACE_ALPHABET; const ACE_TCHAR *rendezvous = PIPE_NAME; // Initialize named pipe listener. if (acceptor.open (ACE_SPIPE_Addr (rendezvous)) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n%a"), ACE_TEXT ("open"), 1)); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("waiting for connection\n"))); // Accept a client connection if (acceptor.accept (new_stream, 0) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n%a"), ACE_TEXT ("accept"), 1)); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Accepted connection\n"))); while (new_stream.recv (buf, 1) > 0) { ACE_TEST_ASSERT (*t == buf[0]); t++; } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("End of connection. Closing handle\n"))); new_stream.close (); acceptor.close (); #if defined (ACE_HAS_WIN32_NAMED_PIPES) // Initialize an NT bytestream named pipe listener. if (acceptor.open (ACE_SPIPE_Addr (rendezvous), 1, 0, 0, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n%a"), ACE_TEXT ("open"), 1)); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("waiting for connection\n"))); // Accept a client connection if (acceptor.accept (new_stream, 0) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n%a"), ACE_TEXT ("accept"), 1)); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Accepted connection\n"))); // The client will write the entire buffer at once, verify that we // can stream it in one byte at a time. for (t = ACE_ALPHABET; *t; t++) { if (new_stream.recv (buf, 1) <= 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n%a"), ACE_TEXT ("recv"), 1)); break; } ACE_TEST_ASSERT (*t == buf[0]); } // Wait for the client to stream in the buffer one byte at a time. ACE_OS::sleep (1); // Verify that we can read the stream of individual bytes all at // once. if (new_stream.recv (buf, sizeof(buf)) != (ssize_t) ACE_OS::strlen (ACE_ALPHABET)) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n%a"), ACE_TEXT ("recv"), 1)); else ACE_TEST_ASSERT(ACE_OS::memcmp(ACE_ALPHABET, buf, ACE_OS::strlen (ACE_ALPHABET)) == 0); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("End of connection. Closing handle\n"))); new_stream.close (); acceptor.close (); #endif /* defined (ACE_HAS_WIN32NAMED_PIPES) */ return 0; }