Click here to Skip to main content
13,898,949 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I Created One Pipe Server and Client Application and Checked the connection between Client And Server,
Server is connecting but it is not receiving the data from Client,

I need to communicate with the Server and Client to display Messages,

Can you please Explain me how to hold the data in m_buffer, variable

What I have tried:

Here My Server code is


#include "StdAfx.h"
#include "PipeServer.h"
#include "process.h"
#include<Windows.h>
#include<tchar.h>


CPipeServer::CPipeServer(void)
{

}
CPipeServer::CPipeServer(std::wstring& sName):m_sPipeName(sName), 
                                                m_hThread(NULL), 
                                                m_nEvent(AU_INIT)
{
	 m_buffer = (wchar_t*)calloc(AU_DATA_BUF, sizeof(wchar_t));
    Init();
}

int CPipeServer::GetEvent() const
{
	return m_nEvent;
}
void CPipeServer::SetEvent(int nEventID)
{
	m_nEvent = nEventID;
}
HANDLE CPipeServer:: GetThreadHandle()
{
	return m_hThread;
}
HANDLE CPipeServer::GetPipeHandle()
{
	return m_hPipe;
}
void CPipeServer::SetData(std::wstring&  sData)
{
	 memset(&m_buffer[0], 0, AU_DATA_BUF);
	 wcsncpy(&m_buffer[0], sData.c_str(), __min(AU_DATA_BUF, sData.size()));
}
void CPipeServer::GetData(std::wstring& sData)
{
	sData.clear();
	sData.append(m_buffer);
}
void CPipeServer::Init()
{
	 if(m_sPipeName.empty())
    {
        LOG << "Error: Invalid pipe name" << std::endl;
        return;
    }
	 m_hPipe = CreateNamedPipe( m_sPipeName.c_str(),PIPE_ACCESS_DUPLEX,PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
								PIPE_UNLIMITED_INSTANCES,1024,1024, NMPWAIT_USE_DEFAULT_WAIT, NULL);
	 if(INVALID_HANDLE_VALUE == m_hPipe)
	 {
		 LOG<<"Error Could not created namedPipe"<<std::endl;
		 OnEvent(AU_ERROR);
	 }
	 else
	 {
		 OnEvent(AU_SERV_RUN);
	 }
	 Run();

}
void CPipeServer::OnBuffer(wchar_t* m_bufferID)
{

	m_buffer = m_bufferID;
}
void CPipeServer::Run()
{
	
    UINT uiThreadId = 0;
    m_hThread = (HANDLE)_beginthreadex(NULL,NULL,PipeThreadProc,this,CREATE_SUSPENDED,&uiThreadId);
	 if(NULL == m_hThread)
    {
        OnEvent(AU_ERROR);
    }
    else
    {
        SetEvent(AU_INIT);
        ::ResumeThread(m_hThread);
    }
}
UINT32 __stdcall CPipeServer::PipeThreadProc(void* pParam)
{
	CPipeServer* pPipe = reinterpret_cast<CPipeServer*>(pParam);
    if(pPipe == NULL)
        return 1L;
	pPipe->OnEvent(AU_THRD_RUN);
	while(true)
    {
        int nEventID = pPipe->GetEvent();
        if(nEventID == AU_ERROR || nEventID == AU_TERMINATE)
        {
           
            pPipe->Close();
            break;
        }

        switch(nEventID)
        {
        case AU_INIT:
            {
                pPipe->WaitForClient();
                break;
            }

        case AU_IOREAD:
            {
                if(pPipe->Read())
                    pPipe->OnEvent(AU_READ);
                else
                    pPipe->OnEvent(AU_ERROR);

                break;
            }

        case AU_IOWRITE:
            {
                if(pPipe->Write())
                    pPipe->OnEvent(AU_WRITE);
                else
                    pPipe->OnEvent(AU_ERROR);
            }
            break;

        case AU_CLOSE:
            {
                pPipe->OnEvent(AU_CLOSE);
                break;
            }

        case AU_IOWRITECLOSE:
            {
                if(pPipe->Write())
                    pPipe->OnEvent(AU_CLOSE);
                else
                    pPipe->OnEvent(AU_ERROR);

                break;
            }

        case AU_IOPENDING:
        default:
            Sleep(10);
            continue;
        };

        Sleep(10);
    };

    return 0;
}
void CPipeServer::OnEvent(int nEventID)
{
    switch(nEventID)
    {
    case AU_THRD_RUN:
        LOG << "Thread running" << std::endl;
        break;

    case AU_INIT:
        LOG << "Initializing pipe comm" << std::endl;
        break;

    case AU_SERV_RUN:
        LOG << "Pipe server running" << std::endl;
        break;

    case AU_CLNT_WAIT:
        LOG << "Waiting for client" << std::endl;
        break;

    case AU_CLNT_CONN:
        {
        std::wstring sData(_T("W\x03B5lc\x03c6me pipe client!"));
        SetData(sData);
        SetEvent(AU_IOWRITE);
        break;
        }

    case AU_READ:
        {
        std::wstring sData;
        GetData(sData);
        LOG << "Message from client: " << sData << std::endl;

        if(wcscmp(sData.c_str(), _T("cl\x03c6se")) == 0)
            SetEvent(AU_CLOSE);
        else
            SetEvent(AU_IOREAD);
        break;
        }
    case AU_WRITE:
        LOG << "Wrote data to pipe" << std::endl;
        SetEvent(AU_IOREAD);
        break;

    case AU_ERROR:
        LOG << "ERROR: Pipe error" << std::endl;
        SetEvent(AU_ERROR);
        break;

    case AU_CLOSE:
        LOG << "Closing pipe" << std::endl;
        SetEvent(AU_TERMINATE);
        break;
    };
}
void CPipeServer::WaitForClient()
{
    OnEvent(AU_CLNT_WAIT);
    if(FALSE == ConnectNamedPipe(m_hPipe, NULL)) 
    {
       
        OnEvent(AU_ERROR);
    }
    else
    {
        OnEvent(AU_CLNT_CONN);
    }
}

void CPipeServer::Close()
{
    ::CloseHandle(m_hPipe);
    m_hPipe = NULL;
}
bool CPipeServer::Read()
{
    DWORD drBytes = 0;
    BOOL bFinishedRead = FALSE;
    int read = 0;
    do
    {
        bFinishedRead = ::ReadFile( 
            m_hPipe,            
            &m_buffer[read],    
            AU_DATA_BUF,        
            &drBytes,           
            NULL);             

        if(!bFinishedRead && ERROR_MORE_DATA != GetLastError())
        {
            bFinishedRead = FALSE;
            break;
        }
        read += drBytes;

    }while(!bFinishedRead);

    if(FALSE == bFinishedRead || 0 == drBytes)
    {
        
        return false;
    }

    return true;
}
bool CPipeServer::Write()
{
    DWORD dwBytes;
    BOOL bResult = ::WriteFile(
        m_hPipe,                  
        m_buffer,              
        ::wcslen(m_buffer)*sizeof(wchar_t) + 1,   
        &dwBytes,            
        NULL);           

    if(FALSE == bResult || wcslen(m_buffer)*sizeof(wchar_t) + 1 != dwBytes)
    {
      
        return false;
    }

    return true;
}

CPipeServer::~CPipeServer(void)
{
	delete m_buffer;
    m_buffer = NULL;
}
Posted
Updated 18-Feb-19 22:01pm
Comments
Stefan_Lang 19-Feb-19 3:09am
   
You have failed to provide the Server class definition, the entire client side, an explanation of the data flow that you intend, and the purpose of m_buffer, i. e. what data you are talking about that is supposed to be stored there.

Also, this is the _Quick_ answers section, and even though it's far from complete, the amount of code you posted so far doesn't allow for a quick answer, simply because of the time it would take to analyse that code.

You should:
1. Specify what data, exactly, are supposed to be stored in m_buffer
2. Specify the data flow that is supposed to transfer this data from its origin to the PipeServer class
3. Determine which functionality you can use to achieve this data flow
4. Look up documentation for the functions
5. Use the debugger to make sure that each step in the transfer process actually does what you thought it should.
6. If any of the above steps leads to problems that you are unable to solve by yourself, come back, describe the steps you've taken up to that point in sufficient detail, and tell us, exactly, what your problem is.
Stefan_Lang 20-Feb-19 4:47am
   
Probably not related to the problem, but you are using calloc to allocate the buffer but delete to release it in the destructor. This will (or in fact should) cause a runtime exception. You must use the appropriate release function, in this case free().

If you had used new[], the proper way would be to call delete[] to indicate that you're releasing an array rather than a single object!
Richard MacCutchan 20-Feb-19 5:46am
   
Well spotted. And your previous comment deserved a 5.
Stefan_Lang 20-Feb-19 7:27am
   
Thank you for that virtual 5. Even if it doesn't affect my profile, it's good to know that my advice makes sense and is appreciated.

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

This function is going to cause problems:
void CPipeServer::OnBuffer(wchar_t* m_bufferID)
{
    m_buffer = m_bufferID;
}

You are overwriting the m_buffer pointer so you will lose the data that was read into the previously allocated buffer.
   
Comments
CPallini 19-Feb-19 4:12am
   
Well spotted. However, if I am not wrong, the OP is not using it.
Richard MacCutchan 19-Feb-19 4:21am
   
I have no idea when or even if that function is being used. But if not then why is it there?
CPallini 19-Feb-19 11:40am
   
Good point, indeed :-)
Stefan_Lang 20-Feb-19 4:32am
   
Good catch, 5
Whether or not that's the culprit, it's at the very least very fishy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190306.1 | Last Updated 19 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100