Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 命名管道在后台等待客户端,直到客户端连接_C++_Multithreading_Named Pipes - Fatal编程技术网

C++ 命名管道在后台等待客户端,直到客户端连接

C++ 命名管道在后台等待客户端,直到客户端连接,c++,multithreading,named-pipes,C++,Multithreading,Named Pipes,我正在尝试使命名管道服务器跳过ConnectNamedPipe函数的块等待,直到我的客户端尝试连接。所以我想让我的代码继续通过ConnetNamedPipe线,直到结束,但在背景中保持管道的连接打开。如果我在创建管道时使用PIPE_NOWAIT模式,它只会立即返回ime,并且在客户端可以连接之前管道关闭 我知道我曾尝试使用线程来执行此操作,但即使在创建线程并在线程中执行代码的ConnectNamedPipe部分时,它仍会在这一行等待,而不是继续执行代码。一旦它到达我的服务器cpp文件代码的末尾,

我正在尝试使命名管道服务器跳过ConnectNamedPipe函数的块等待,直到我的客户端尝试连接。所以我想让我的代码继续通过ConnetNamedPipe线,直到结束,但在背景中保持管道的连接打开。如果我在创建管道时使用PIPE_NOWAIT模式,它只会立即返回ime,并且在客户端可以连接之前管道关闭

我知道我曾尝试使用线程来执行此操作,但即使在创建线程并在线程中执行代码的ConnectNamedPipe部分时,它仍会在这一行等待,而不是继续执行代码。一旦它到达我的服务器cpp文件代码的末尾,我想用我的客户端连接到管道

我的烟斗:

hPipe = CreateNamedPipe(
    lpszPipename,             
    PIPE_ACCESS_OUTBOUND,       // one way access, only send data
    PIPE_TYPE_MESSAGE |       // message type pipe
    PIPE_READMODE_MESSAGE |   // message-read mode
    PIPE_WAIT,                
    PIPE_UNLIMITED_INSTANCES, 
    512,
    512,                  
    0,                        
    NULL);                    

if (hPipe == INVALID_HANDLE_VALUE)
{
    Out->msg(ERR,"Creating the pipe failed.");
}

 // Create a thread for this client.
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)
{
    Out->msg(ERR,"Creating the thread failed.");
}
else CloseHandle(hThread);

// Close the pipe.
 CloseHandle(hPipe);
我的帖子:

DWORD WINAPI InstanceThread(LPVOID lpvParam)
{
    Out->msg(output::SEV_INFO,"Waiting for client to connect.");
    fConnected = ConnectNamedPipe(hPipe, NULL) ? //This is where the execution hangs
    TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

    HANDLE hHeap      = GetProcessHeap();
    TCHAR* pchReply   = (TCHAR*)HeapAlloc(hHeap, 0, BUFSIZE*sizeof(TCHAR));

    DWORD cbBytesRead = 0, cbReplyBytes = 0, cbWritten = 0;
    BOOL fSuccess = FALSE;
    HANDLE hPipe  = NULL;

// The thread's parameter is a handle to a pipe object instance.

    hPipe = (HANDLE) lpvParam;
    uint32_t startTime = time(NULL);
    uint32_t elapsedTime;
// Loop until done reading
    while ((elapsedTime < 60))
    {
        elapsedTime = difftime(time(NULL), startTime);
        // Write to the pipe.
        fSuccess = WriteFile(
            hPipe,        // handle to pipe
            pchReply,     // buffer to write from
            cbReplyBytes, // number of bytes to write
            &cbWritten,   // number of bytes written
            NULL);        // not overlapped I/O

        if (!fSuccess || cbReplyBytes != cbWritten)
        {
            Out->msg(ERR,"InstanceThread WriteFile failed.");
            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);

    HeapFree(hHeap, 0, pchReply);

    Out->msg(output::SEV_INFO,"InstanceThread exitting.\n");
    return 1;
}
DWORD WINAPI InstanceThread(LPVOID lpvParam)
{
Out->msg(输出::SEV_INFO,“等待客户端连接”);
fConnected=ConnectNamedPipe(hPipe,NULL)?//这是执行挂起的地方
TRUE:(GetLastError()==错误\u管道\u已连接);
HANDLE hHeap=GetProcessHeap();
TCHAR*pchReply=(TCHAR*)HeapAlloc(hHeap,0,BUFSIZE*sizeof(TCHAR));
DWORD cbBytesRead=0,CBREPLYBETS=0,CBWRITED=0;
boolfsuccess=FALSE;
句柄hPipe=NULL;
//线程的参数是管道对象实例的句柄。
hPipe=(HANDLE)lpvParam;
uint32\u t startTime=时间(空);
uint32_t延时;
//循环直到完成读取
而((持续时间<60))
{
elapsedTime=difftime(time(NULL),startTime);
//写信给管道。
fSuccess=WriteFile(
hPipe,//句柄到管道
pchReply,//要从中写入的缓冲区
cbReplyBytes,//要写入的字节数
&cbwrited,//写入的字节数
NULL);//不重叠的I/O
如果(!fsucces | | cbReplyBytes!=cbwrite)
{
Out->msg(错误,“InstanceThread WriteFile失败”);
打破
}
}
//冲洗管道以允许客户端读取管道内容
//断开之前。然后断开管道,并关闭
//此管道实例的句柄。
FlushFileBuffers(hPipe);
断开命名管道(hPipe);
闭柄(hPipe);
HeapFree(hHeap,0,pchReply);
Out->msg(输出::SEV_INFO,“InstanceThread exiting.\n”);
返回1;
}
我正在尝试使命名管道服务器跳过ConnectNamedPipe函数的块等待,直到我的客户端尝试连接

你说的是服务器

因此,我希望我的代码继续通过
ConnectNamedPipe
行,同时仍然能够连接到我的服务器

所以你说的是客户


这没有道理
ConnectNamedPipe()
是一个服务器端函数,在客户端连接之前,在专用于它的线程中,除了块之外,没有任何有用的操作。那就这么做吧。

真是抱歉。我的服务器文件中有ConnectNamedPipe代码。它执行并运行,直到到达包含ConnectNamedPipe的线程。然后它等待我的客户端连接。我想让它一直运行我的代码直到结束,然后当它到达结束时,我的客户端将连接到管道。一直运行到什么结束?在客户端连接之前,它如何处理管道?没错,哈哈,在客户端连接之前,我想在代码中先做一些其他事情,所以我希望代码继续并执行这些事情,然后在完成后,我将执行客户端代码,它将连接到此管道。因此,移动ConnectNamedPipe()调用到您准备阻止的位置。我很想这样做,但我不能,因为我需要在执行客户端cpp文件之前完成整个服务器cpp代码。如果可能的话,我只需要在后台运行代码的这个线程部分