πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #17419

Re: Named Pipe

#17419
From: Evariste HORNECKER
Date: Thu, 27 Mar 2003 09:59
131 lines
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;
}
Technical Details
Message-ID: <3e82cb8c$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail
References: <3e82b16c$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 10cc67,e29d7d1f5f25464b
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-03-27 02:03:20 PST
From: "Evariste HORNECKER" <evariste.hornecker@neosys.fr>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <3e82b16c$1@newsgroups.borland.com>
Subject: Re: Named Pipe
Date: Thu, 27 Mar 2003 10:59:39 +0100
Lines: 133
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1123
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1123
NNTP-Posting-Host: 80.13.46.69
Message-ID: <3e82cb8c$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1048759180 80.13.46.69 (27 Mar 2003 01:59:40 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-08!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail