Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Namedpipe

Namedpipe

Scheduled Pinned Locked Moved C / C++ / MFC
helpsysadminalgorithmssecuritytesting
3 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    charian0920
    wrote on last edited by
    #1

    Hi! Good day :) I have this problem about synchronization on namedpipe ... I have this namedpipe server that allows multiple instances. The algorithm I have is I'll wait for a client to connect to the namedpipe then spawn a thread which will read the message from the namedpipe. My expectation on the spawned thread is that when the client has disconnected the spawned thread will end also. But when I tried testing my code, the thread count keeps on increasing due to the spawned thread, which i think means I was not able to detect that the client had disconnected or something else :( Any help is greatly appreciated. Below are my codes: 1. Createnamedpipe, wait for a client to connect, and spawned a thread. UINT fnRcvMsgsFrmNamedpipeThread(LPVOID pParam) { DWORD dwResult = SUCCEED; CString strLog( _T( "" ) ); OVERLAPPED stClientConnectOverLap = {0}; stClientConnectOverLap.hEvent = CreateEvent( NULL, // no security attributes TRUE, // manual reset event FALSE, // not-signalled NULL); // no name if( stClientConnectOverLap.hEvent == NULL ) { return 0; } HANDLE hEvents; hEvents = stClientConnectOverLap.hEvent; CString strFormatPipeName( _T( "" ) ); strFormatPipeName.Format( "\\\\%s\pipe\mypipename", STR_PERIOD ); HANDLE hClientReqPipe; while ( TRUE ) { hClientReqPipe = CreateNamedPipe( strFormatPipeName, FILE_FLAG_OVERLAPPED | PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 0, 0, 10000, NULL ); if( hClientReqPipe == INVALID_HANDLE_VALUE ) { dwResult = GetLastError(); break; } if( !ConnectNamedPipe( hClientReqPipe, &stClientConnectOverLap ) ) { if( GetLastError() != ERROR_PIPE_CONNECTED ) { DWORD dwRet = WaitForSingleObject( hEvents, INFINITE ); if( dwRet != WAIT_OBJECT_0 ) { dwResult = GetLastError(); break; } } } CWinThread *pReadMsgThread = AfxBeginThread( fnReadMsgFrmNamedpipe, (LPVOID) hClientReqPipe ); if( NULL == pReadMsgThread ) { break; } } // Close handles ::CloseHandle( hClientReqPipe ); ::CloseHandle( stClientConnectOverLap.hEvent ); return dwResult; } 2. Spawned thread UINT fnReadMsgFrmNamedpipe(LPVOID pParam) { I32L dwResult = SUCCEED; CString strLog( _T("") ); HANDLE hClientReqPipe = (HANDLE) pParam; OVERLAPPED stClientRecOverLap

    M 1 Reply Last reply
    0
    • C charian0920

      Hi! Good day :) I have this problem about synchronization on namedpipe ... I have this namedpipe server that allows multiple instances. The algorithm I have is I'll wait for a client to connect to the namedpipe then spawn a thread which will read the message from the namedpipe. My expectation on the spawned thread is that when the client has disconnected the spawned thread will end also. But when I tried testing my code, the thread count keeps on increasing due to the spawned thread, which i think means I was not able to detect that the client had disconnected or something else :( Any help is greatly appreciated. Below are my codes: 1. Createnamedpipe, wait for a client to connect, and spawned a thread. UINT fnRcvMsgsFrmNamedpipeThread(LPVOID pParam) { DWORD dwResult = SUCCEED; CString strLog( _T( "" ) ); OVERLAPPED stClientConnectOverLap = {0}; stClientConnectOverLap.hEvent = CreateEvent( NULL, // no security attributes TRUE, // manual reset event FALSE, // not-signalled NULL); // no name if( stClientConnectOverLap.hEvent == NULL ) { return 0; } HANDLE hEvents; hEvents = stClientConnectOverLap.hEvent; CString strFormatPipeName( _T( "" ) ); strFormatPipeName.Format( "\\\\%s\pipe\mypipename", STR_PERIOD ); HANDLE hClientReqPipe; while ( TRUE ) { hClientReqPipe = CreateNamedPipe( strFormatPipeName, FILE_FLAG_OVERLAPPED | PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, 0, 0, 10000, NULL ); if( hClientReqPipe == INVALID_HANDLE_VALUE ) { dwResult = GetLastError(); break; } if( !ConnectNamedPipe( hClientReqPipe, &stClientConnectOverLap ) ) { if( GetLastError() != ERROR_PIPE_CONNECTED ) { DWORD dwRet = WaitForSingleObject( hEvents, INFINITE ); if( dwRet != WAIT_OBJECT_0 ) { dwResult = GetLastError(); break; } } } CWinThread *pReadMsgThread = AfxBeginThread( fnReadMsgFrmNamedpipe, (LPVOID) hClientReqPipe ); if( NULL == pReadMsgThread ) { break; } } // Close handles ::CloseHandle( hClientReqPipe ); ::CloseHandle( stClientConnectOverLap.hEvent ); return dwResult; } 2. Spawned thread UINT fnReadMsgFrmNamedpipe(LPVOID pParam) { I32L dwResult = SUCCEED; CString strLog( _T("") ); HANDLE hClientReqPipe = (HANDLE) pParam; OVERLAPPED stClientRecOverLap

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Have you tried putting a breakpoint (or several) in the thread proc and watching what is happening? The debugger is your friend! Mark

      Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

      C 1 Reply Last reply
      0
      • M Mark Salsbery

        Have you tried putting a breakpoint (or several) in the thread proc and watching what is happening? The debugger is your friend! Mark

        Mark Salsbery Microsoft MVP - Visual C++ "Great job team! Head back to base for debriefing and cocktails."

        C Offline
        C Offline
        charian0920
        wrote on last edited by
        #3

        hi mark, yup I already did that ... the funny thing is that the spawned thread is terminating when I run it with breakpoints and when I put messageboxes.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups