Thread View: borland.public.cppbuilder.nativeapi
2 messages
2 total messages
Started by PaoloItaly
Thu, 27 Mar 2003 08:08
Named Pipe
Author: PaoloItaly
Date: Thu, 27 Mar 2003 08:08
Date: Thu, 27 Mar 2003 08:08
4 lines
952 bytes
952 bytes
hallo, anyone have a sample about NamedPipe client-server ? Thanks in advance have a nice working day
Re: Named Pipe
Author: Evariste HORNECKER
Date: Thu, 27 Mar 2003 09:59
Date: Thu, 27 Mar 2003 09:59
131 lines
4905 bytes
4905 bytes
> "PaoloItaly" <paolo@sirius.tecnoparco.it> a οΏ½crit dans le message de news:
3e82b16c$1@newsgroups.borland.com...
> hallo,
> anyone have a sample about NamedPipe client-server ?
> Thanks in advance
> have a nice working day
>
>
Sample code (WIN32 console)
//------------------------------- server
const int BUFSIZE = 1024;
VOID GetAnswerToRequest(LPTSTR input, LPTSTR output, LPDWORD nbchar)
{
strcpy(output,input);
strcat(output,"----OK\n");
*nbchar = strlen(output);
}
int main(int argc, char* argv[])
{
BOOL fConnected;
BOOL fSuccess;
CHAR chRequest[BUFSIZE];
CHAR chReply[BUFSIZE];
DWORD cbBytesRead, cbReplyBytes, cbWritten;
HANDLE hPipe;
LPTSTR lpszPipename = \\\\.\\pipe\\mypipe;
hPipe = CreateNamedPipe( lpszPipename, // pipe name
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
1000, // client time-out
NULL); // no security attribute
if (hPipe == INVALID_HANDLE_VALUE)
return 10; //MyErrExit("CreatePipe");
// 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)
{
while (1)
{
// Read client requests from the pipe.
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
if (! fSuccess || cbBytesRead == 0)
break;
GetAnswerToRequest(chRequest, chReply, &cbReplyBytes);
// Write the reply to the pipe.
fSuccess = WriteFile( hPipe, // handle to pipe
chReply, // buffer to write from
cbReplyBytes, // number of bytes to write
&cbWritten, // number of bytes written
NULL); // not overlapped I/O
if (! fSuccess || cbReplyBytes != cbWritten) break;
}
// Flush the pipe to allow the client to read the pipe's contents
// before disconnecting. Then disconnect the pipe, and close the
// handle to this pipe instance.
FlushFileBuffers(hPipe);
DisconnectNamedPipe(hPipe);
CloseHandle(hPipe);
}
else
// The client could not connect, so close the pipe.
CloseHandle(hPipe);
}
return 1;
}
//------------------------------- client
#include <windows.h>
void connect(const char *mcName )
{
HANDLE hPipe; // Pipe connection handle
DWORD nBytes;
// First merely try to connect to the remote machine
hPipe = CreateFile( mcName, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if( hPipe == INVALID_HANDLE_VALUE )
return;
char str[100];
sprintf( str, "Hello %x\n",hPipe);
BOOL success = WriteFile( hPipe, str, strlen( str ) + 1, &nBytes,
NULL );
if( success == FALSE || nBytes != strlen( str ) + 1 )
{
CloseHandle( hPipe );
return ; // Couldn't write pipe
}
// Now try and read
success = ReadFile( hPipe, str, sizeof(str), &nBytes, NULL );
if( success == FALSE )
{
CloseHandle( hPipe );
return; // Couldn't read pipe
}
str[nBytes] = 0;
printf("Server sent %d chars:\n%s",nBytes,str);
}
int main(int argc, char* argv[])
{
connect("\\\\.\\pipe\\mypipe");
return 0;
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads