命名管道读写(c++/c#命名管道通信问题,服务端能读,不能写)

本文目录
c++/c#命名管道通信问题,服务端能读,不能写
//
服务器可以接收客户端消息,但是不能发送消息,write函数一直阻塞,换用sw.write仍然一样的。
//服务器端:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Pipes;
using System.Threading;
namespace FrmServer
{
public partial class FrmServer : Form
{
public delegate void SetTextDelegate(string text);
NamedPipeServerStream pipeServer;
StreamWriter sw;
StreamReader sr;
public FrmServer()
{
InitializeComponent();
//process1.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
//process1.StartInfo.FileName = "MiniFetion";
//process1.Start();
Thread th = new Thread(new ThreadStart(CreateNamedPipe));
th.Start();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
textBox1.Text = "PipeServer Message";
byte buffer = Encoding.UTF8.GetBytes(textBox1.Text);
pipeServer.Write(buffer,0,buffer.Length);
label1.Invoke(new SetTextDelegate(SetTextDo), buffer);
}
catch (IOException ex)
{
label1.Text = ex.Message;
}
}
private void CreateNamedPipe()
{
pipeServer = new NamedPipeServerStream("FetionPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Byte);
pipeServer.WaitForConnection();
sw = new StreamWriter(pipeServer);
sr = new StreamReader(pipeServer);
sw.AutoFlush = true;
if (pipeServer.IsConnected)
{
label1.Invoke(new SetTextDelegate(SetTextDo), "管道客户端已连接");
}
//sw.Write("login",new UTF8Encoding(false));
if (label1.InvokeRequired)
{
label1.Invoke(new SetTextDelegate(SetTextDo), "管道客户端已连接");
}
//监听管道
while(true)
{
// Process the incoming message.
//GetAnswerToRequest(pchRequest, pchReply, &cbReplyBytes);
try
{
if (label1.InvokeRequired)
{
byte;
pipeServer.Read(buffer, 0, 255);
label1.Invoke(new SetTextDelegate(SetTextDo), Encoding.UTF8.GetString(buffer));
Thread.Sleep(100);
}
//处理消息
}
catch(IOException ex)
{
if (label1.InvokeRequired)
{
label1.Invoke(new SetTextDelegate(SetTextDo),ex.Message);
}
}
}
}
public void SetTextDo(string text) { label1.Text = text; }
}
}
//客户端
#include "stdafx.h"
#include "FetionPipe.h"
HANDLE CFetionPipe::hPipeClient = NULL;//初始化静态成员变量
HANDLE CFetionPipe::CreateClient()
{
CString lpPipeName="\\\\.\\pipe\\FetionPipe";
LPTSTR lpszPipename = new TCHAR;
_tcscpy(lpszPipename, lpPipeName);
/*if (WaitNamedPipe(lpszPipename, 5000)==false)
{
return NULL;
}*/
hPipeClient = CreateFile(
lpszPipename,
GENERIC_WRITE|GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
while(true)
{
if(hPipeClient == INVALID_HANDLE_VALUE)
{
CString strLastError;
strLastError.Format("%d",GetLastError());
AfxMessageBox("Error open pipes, the Last Error number is:"+strLastError);
Sleep(5000);
hPipeClient = CreateFile(
lpszPipename,
GENERIC_WRITE|GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
}
else
{
AfxMessageBox("Success open pipes");
break;
}
}
//设置管道状态
DWORD dwMode = PIPE_READMODE_BYTE;
BOOL fSuccess = SetNamedPipeHandleState(
hPipeClient, // pipe handle
&dwMode, // new pipe mode
NULL, // don’t set maximum bytes
NULL); // don’t set maximum time
if ( ! fSuccess)
{
AfxMessageBox("Failed SetNamedPipeHandleState");
}
return hPipeClient;
}
DWORD WINAPI CFetionPipe::Listenner(LPVOID lpParameter)
{
//创建文件
hPipeClient = CreateClient();//hPipeClient静态变量
DWORD dwRead,dwWritten;
TCHAR* Msg;
BOOL fSuccess = FALSE;
DWORD cbRead, cbToWrite, cbWritten, dwMode;
LPTSTR lpvMessage=TEXT("Msg:FetionPipe Success!");
fSuccess = MsgWrite(lpvMessage);
while(hPipeClient != INVALID_HANDLE_VALUE)
{
Msg = MsgRead();
if(Msg!=NULL)
{
AfxMessageBox(Msg);
MsgHandle(Msg);
}
}
return 0;
}
BOOL CFetionPipe::MsgHandle(TCHAR* pszMsg)
{
pszMsg = TEXT("Received Message!");
MsgWrite(pszMsg);
return 0;
}
BOOL CFetionPipe::MsgWrite(TCHAR* pszMsg)//写管道消息
{
BOOL fSuccess = FALSE;
DWORD cbToWrite, cbWritten;
LPTSTR lpvMessage = TEXT(pszMsg);
cbToWrite = (lstrlen(lpvMessage)+1)*sizeof(TCHAR);
fSuccess = WriteFile(
hPipeClient, // pipe handle
lpvMessage, // message
cbToWrite, // message length
&cbWritten, // bytes written
NULL);
return fSuccess;
}
TCHAR* CFetionPipe::MsgRead()//读管道消息
{
DWORD dwRead,dwWritten;
TCHAR* chBuf = NULL;
BOOL fSuccess = FALSE;
DWORD cbRead, cbToWrite, cbWritten, dwMode;
do
{
// Read from the pipe.
fSuccess = ReadFile(
hPipeClient, // pipe handle
chBuf, // buffer to receive reply
BUFSIZ*sizeof(TCHAR), // size of buffer
&cbRead, // number of bytes read
NULL); // not overlapped
if ( ! fSuccess && GetLastError() != ERROR_MORE_DATA )
{
break;
}
Sleep(100);
} while (!fSuccess); // repeat loop if ERROR_MORE_DATA
return chBuf;
}
在为windows客户端读取共享列表时发生如下错误:错误2114
是不是在网络属性里没有选中“文件与打印机共享”?可以先看看是否没有启动相关的网络服务。
查找步骤为进入管理——-服务——启动“支持此计算机通过网络的文件、打印和命名管道共享……”。
具体步骤如下:
1、右键我的电脑——属性;
2、计算机管理——打开【服务和应用程序】;
3、点击【服务】;
4、找到【services】服务,右键单机,启动它!
现在再去共享一下试试!是不是可以了?
什么是【services】服务?
显示名称:Server
进程名称:svchost.exe -k netsvcs
微软描述:支持此计算机通过网络的文件、打印、和命名管道共享。如果服务停止,这些功能不可用。如果服务被禁用,任何直接依赖于此服务的服务将无法启动。
补充描述:简单的说就是档案和打印的共享,除非你有和其它计算机共享,不然就关了。局域网文件/打印共享需要的。
默认:自动
建议:手动

更多文章:
java取模运算(java中的取模%怎么样运算的具体的运算公式是什么)
2026年3月5日 13:30
怎么破解易语言做的软件卡密(我有一个用易语言写的软件,他是用我的机器码才能注册这个软件.我想知道如何破解这个注册码 QQ153309012)
2025年11月29日 03:15
baseline脊柱侧弯测量尺(作为一项体检项目,脊柱侧弯的测量标准是什么样的)
2025年9月15日 17:30
reception中文(reception在法语里什么意思)
2025年6月14日 23:30
javamysql教程(java如何建立数据库(java创建mysql数据库))
2026年8月29日 05:15
亚马逊雨林纪录片国语(亚马逊雨林在哪种地方睡觉休息一直很好奇哎!)
2025年7月25日 00:00
css图片缩小比例不变(CSS强制图片大小 CSS图片大小如何自动缩放)
2026年6月5日 14:45
索尼music transfer(索尼T100的闪光灯参数)
2025年12月27日 09:30
capital和capitol(capitol唱片的capitol唱片简介)
2025年12月12日 11:30
python全为字母的正则(python正则表达式:匹配所有的字母数字但是不匹配1080p这个单词如何操作)
2025年11月27日 11:00
什么是类类与对象的区别是什么?MYSQLSET字段类型怎么查询
2026年3月9日 02:30
javascript基础教程是必修课吗(初中生学计算机最开始要学什么)
2025年7月1日 11:00
vb资料管理系统(vb+access做的一个管理系统怎么能实现在局域网内的数据库共享)
2026年5月23日 03:30









