⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.cpp

📁 < WINDOWS网络编程>>英文版,一本详细讲解WINDOWS平台下网络编程的国外经典书籍,适合英文水平高的牛人
💻 CPP
字号:
// Module Name: Server.cpp
//
// Purpose:
//
//     This program is a simple named pipe server that demonstrates
//     the API calls needed to successfully develop a basic named
//     pipe server application. When this application receives a
//     client connection, it reads the data from the pipe and
//     reports the received message.
//
//     You need five basic steps to write a named pipe server:
//
//     1. Create a named pipe instance handle using the 
//        CreateNamedPipe() API function.
//     2. Listen for a client connection on a pipe instance using
//        the ConnectNamedPipe() API function.
//     3. Receive from and send data to the client using the 
//        ReadFile() and WriteFile() API functions.
//     4. Close down the named pipe connection using the
//        DisconnectNamedPipe() API function.
//     5. Close the named pipe instance handle using the
//        CloseHandle() API function.
//
// Compile:
//     cl -o Server Server.cpp
//
// Command Line Options:
//     None
//

#include <windows.h>
#include <stdio.h>

void main(void)
{
	HANDLE PipeHandle;
	DWORD BytesRead;
	CHAR buffer[256];

	if ((PipeHandle = CreateNamedPipe("\\\\.\\Pipe\\Jim",
		PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1,
		0, 0, 1000, NULL)) == INVALID_HANDLE_VALUE)
	{
		printf("CreateNamedPipe failed with error %d\n",
			GetLastError());
		return;
	}

	printf("Server is now running\n");

	if (ConnectNamedPipe(PipeHandle, NULL) == 0)
	{
		printf("ConnectNamedPipe failed with error %d\n",
			GetLastError());
		CloseHandle(PipeHandle);
		return;
	}

	if (ReadFile(PipeHandle, buffer, sizeof(buffer),
		&BytesRead,  NULL) <= 0)
	{
		printf("ReadFile failed with error %d\n", GetLastError());
		CloseHandle(PipeHandle);
		return;
	}

	printf("%.*s\n", BytesRead, buffer);

	if (DisconnectNamedPipe(PipeHandle) == 0)
	{
		printf("DisconnectNamedPipe failed with error %d\n",
			GetLastError());
		return;
	}

	CloseHandle(PipeHandle);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -