Article View: borland.public.cppbuilder.nativeapi
Article #2189Re: Named Pipes problem
From: Martin
Date: Tue, 24 Apr 2007 15:08
Date: Tue, 24 Apr 2007 15:08
174 lines
6439 bytes
6439 bytes
Remy Lebeau (TeamB) wrote:
> "Martin" <dev@no.comppromed.spam.com> wrote in message
> news:462d0a59@newsgroups.borland.com...
>
>> Yes, DisconnectNamedPipe is being called.
>
> Then please show your actual code, and indicate where the error is
> occuring.
>
As I said before, I can exhibit this problem using the server side of
the multithreaded pipes example in the Microsoft SDK. Here is my
modified example program.
Marty
//---------------------------------
#include <vcl.h>
#include <process.h>
#include <stdio.h>
#include <tchar.h>
#define BUFSIZE 4096
//DWORD WINAPI InstanceThread(LPVOID);
static void _USERENTRY InstanceThread( LPVOID);
VOID GetAnswerToRequest(LPTSTR, LPTSTR, LPDWORD);
#define THREAD_STACK 0
int _tmain(VOID)
{
BOOL fConnected;
DWORD dwThreadId;
HANDLE hPipe, hThread;
LPTSTR lpszPipename = TEXT("\\\\.\\pipe\\mynamedpipe");
// The main loop creates an instance of the named pipe and
// then waits for a client to connect to it. When the client
// connects, a thread is created to handle communications
// with that client, and the loop is repeated.
for (;;)
{
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
NMPWAIT_USE_DEFAULT_WAIT, // client time-out
NULL); // default security attribute
if (hPipe == INVALID_HANDLE_VALUE)
{
printf("CreatePipe failed");
return 0;
}
else
printf( "waiting for client connect to pipe %x\n", hPipe);
// 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)
{
// Create a thread for this client.
hThread =(HANDLE) _beginthread( InstanceThread,
THREAD_STACK, hPipe);
// hThread = CreateThread(
// NULL, // no security attribute
// 0, // default stack size
// InstanceThread, // thread proc
// (LPVOID) hPipe, // thread parameter
// 0, // not suspended
// &dwThreadId); // returns thread ID
if (hThread == NULL)
{
printf("CreateThread failed");
return 0;
}
else
{
printf( "pipe %x passed to thread\n", hPipe);
CloseHandle(hThread);
}
}
else
{
printf( "client couldn't connect to pipe, closing %x\n", hPipe);
// The client could not connect, so close the pipe.
CloseHandle(hPipe);
}
}
}
//DWORD WINAPI InstanceThread(LPVOID lpvParam)
static void _USERENTRY InstanceThread(LPVOID lpvParam)
{
TCHAR chRequest[BUFSIZE];
TCHAR chReply[BUFSIZE];
DWORD cbBytesRead, cbReplyBytes, cbWritten;
BOOL fSuccess;
HANDLE hPipe;
// The thread's parameter is a handle to a pipe instance.
hPipe = (HANDLE) lpvParam;
printf( "T%x, using pipe %x\n", GetCurrentThreadId(), hPipe);
while (1)
{
// Read client requests from the pipe.
fSuccess = ReadFile(
hPipe, // handle to pipe
chRequest, // buffer to receive data
BUFSIZE*sizeof(TCHAR), // size of buffer
&cbBytesRead, // number of bytes read
NULL); // not overlapped I/O
DWORD err = GetLastError();
if (! fSuccess || cbBytesRead == 0)
{
printf( "T%x, Error reading from pipe, err %d\n",
GetCurrentThreadId(), err);
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
err = GetLastError();
if (! fSuccess || cbReplyBytes != cbWritten)
{
printf( "T%x error writing to pipe %x, success %d, wrote %d,
err %d\n", GetCurrentThreadId(), fSuccess, cbWritten, err);
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);
return;
}
VOID GetAnswerToRequest(LPTSTR chRequest,
LPTSTR chReply, LPDWORD pchBytes)
{
_tprintf( TEXT("%s\n"), chRequest );
lstrcpy( chReply, TEXT("Default answer from server") );
*pchBytes = (lstrlen(chReply)+1)*sizeof(TCHAR);
}
Technical Details
Message-ID:
<462e1d6f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<462cc3e6$1@newsgroups.borland.com> <462cf7ad$1@newsgroups.borland.com> <462d0a59@newsgroups.borland.com> <462d2679$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6ec9191177bf375b X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Tue, 24 Apr 2007 08:08:26 -0700 From: Martin <dev@no.comppromed.spam.com> User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Re: Named Pipes problem References: <462cc3e6$1@newsgroups.borland.com> <462cf7ad$1@newsgroups.borland.com> <462d0a59@newsgroups.borland.com> <462d2679$1@newsgroups.borland.com> In-Reply-To: <462d2679$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 208.201.233.20 Message-ID: <462e1d6f$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1177427311 208.201.233.20 (24 Apr 2007 08:08:31 -0700) Lines: 174 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail