Jump to content

PHP & Named Pipes


ecarino

Recommended Posts

I keep reading on random sites on the web that PHP supports named pipes, by using popen() or proc_open()... but I just can't get it working!!!

 

I have a simple C++/Windows app that acts as the Name Pipe Server.  I also wrote a simple Name Pipe Client to verify the server works.  Everything checks out fine.

 

But when I run my php script to connect to the pipe & write to it, it doesn't quite work.  Walking the C++ server code, a connection is actually being established, but the Read() returns a "Broken Pipe" error.

 

Can anyone confirm to me that Named Pipes DO work in PHP?  My Code below:

 

Pipe Server

int main()
{

BOOL fConnected;
LPTSTR lpszPipename = "\\\\.\\pipe\\SamplePipe";
CHAR chRequest[bUFSIZE];
DWORD cbBytesRead;
BOOL fSuccess;
HANDLE hPipe;


hPipe = CreateNamedPipe ( lpszPipename,
	PIPE_ACCESS_DUPLEX, // read/write access
	PIPE_TYPE_MESSAGE | // message type pipe
	PIPE_READMODE_MESSAGE | // message-read mode
	PIPE_WAIT, // blocking mode
	PIPE_UNLIMITED_INSTANCES, // max. instances
	BUFSIZE, // output buffer size
	BUFSIZE, // input buffer size
	NMPWAIT_USE_DEFAULT_WAIT, // client time-out
	NULL); // no security attribute

if (hPipe == INVALID_HANDLE_VALUE)
	return true;

for (;
{
	// Trying connectnamedpipe in sample for CreateNamedPipe
	// Wait for the client to connect; if it succeeds,
	// the function returns a nonzero value. If the function returns
	// zero, GetLastError returns ERROR_PIPE_CONNECTED.

	fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

	if (fConnected)
	{
		fSuccess = ReadFile (hPipe, // handle to pipe
			chRequest, // buffer to receive data
			BUFSIZE, // size of buffer
			&cbBytesRead, // number of bytes read
			NULL); // not overlapped I/O

		chRequest[cbBytesRead] = '\0';
		printf("Data Received: %s\n",chRequest);

		if (! fSuccess || cbBytesRead == 0)
		{
			printf("Error: %d\n", GetLastError());
			break;
		}

		FlushFileBuffers(hPipe);
		DisconnectNamedPipe(hPipe);
	}
	else
		// The client could not connect in the CreateNamedPipe sample, so close the pipe.
		CloseHandle(hPipe);
}
CloseHandle(hPipe);
return 1;	
}

 

PHP Pipe Client

$pipe = popen('\\\\.\\pipe\\SamplePipe','w');
fwrite($pipe,'this is some text');
pclose($pipe);

Link to comment
https://forums.phpfreaks.com/topic/104949-php-named-pipes/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.