更新:
通過協議
here,我無法弄清楚未知的信封記錄.我在網上找不到任何例子.
原版的:
我有以下WCF服務
static void Main(string[] args)
{
var inst = new PlusFiver();
using (ServiceHost host = new ServiceHost(inst,new Uri[] { new Uri("net.pipe://localhost") }))
{
host.AddServiceEndpoint(typeof(IPlusFive),new NetNamedPipeBinding(NetNamedPipeSecurityMode.None),"PipePlusFive");
host.Open();
Console.WriteLine("Service is Available. Press enter to exit.");
Console.ReadLine();
host.Close();
}
}
[ServiceContract]
public interface IPlusFive
{
[OperationContract]
int PlusFive(int value);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class PlusFiver : IPlusFive
{
public int PlusFive(int value)
{
Console.WriteLine("Adding 5 to " + value);
return value + 5;
}
}
我輸出添加5行,所以我知道服務器是否處理了
請求與否.
我有一個.NET客戶端,我曾經測試這一切,一切正常工作
預期.
現在我想為這個做一個非托管的C客戶端.
我想出了如何得到管道的名稱,并寫信給它.
我從here下載了協議
我可以寫信給管道,但我看不懂.每當我嘗試讀取它,我得到一個ERROR_BROKEN_PIPE 109(0x6D)管道已經結束.錯誤.如果我用寫入替換讀取,則寫入是成功的,所以我不認為管道是封閉的,至少直到我嘗試讀取.
這是我如何連接到管道.
HANDLE OpenPipe(OLECHAR* bstrGuid)
{
wstring pipeName = L"\\\\.\\pipe\\";
wstring strGuid = bstrGuid;
pipeName.append(strGuid.substr(1,36));
wcout << "Pipe Name " << endl;
wcout << pipeName.c_str() << endl;
HANDLE hPipe = CreateFile(pipeName.c_str(),GENERIC_WRITE | GENERIC_READ,FILE_SHARE_WRITE | FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL);
if(hPipe == INVALID_HANDLE_VALUE)
{
wcout << "Failed to create pipe" << endl;
system("pause");
return NULL;
}
return hPipe;
}
這是我正在創建我發送的第一條消息
std::list GetFirstMessage()
{
std::list message;
message.push_back(0x00);// version record
message.push_back(0x01);// major version
message.push_back(0x00);// minor version
message.push_back(0x01);// mode record
message.push_back(0x01);// singleton-unsized mode
message.push_back(0x02);// via record
wstring url = L"net.pipe://localhost/PipePlusFive";
message.push_back(url.length());// via length
for(int x= 0;x
{
message.push_back(url[x]); // via
}
message.push_back(0x03);
message.push_back(0x08);
return message;
}
這是我如何寫文件.
int WriteMessage(HANDLE hPipe,LPVOID message,int size)
{
DWORD bytesWritten;
BOOL bWrite = WriteFile(hPipe,&message,size,&bytesWritten,NULL);
wcout << "Bytes Written: " << bytesWritten << endl;
if(bWrite == false)
{
wcout << "fail"<
CloseHandle(hPipe);
system("pause");
return 1;
}
return 0;
}
list full_message = GetFirstMessage();
int result = WriteMessage(hPipe,&full_message,full_message.size());
if (result == 1)
{ return 1;}
這是我如何寫最后的消息
wchar_t message = 12;
result = WriteMessage(hPipe,1);
if (result == 1)
{ return 1;}
這是我如何讀取響應
char buffer[10];
DWORD bytesRead;
BOOL bRead = ReadFile(hPipe,buffer,1,&bytesRead,NULL);
if(bRead == false)
{
wcout << "fail read"<
wcout << "error: " << GetLastError() << endl;
CloseHandle(hPipe);
system("pause");
return 1;
}
我是新來的c,所以我不知道我是不是正確地遵循協議,或者是以我這樣做的方式犯了一個愚蠢的錯誤?
更新:
問題是我正在將命名地址寫入命名管道而不是列表的內容.我已經解決了,我現在可以閱讀前言Ack記錄.現在我必須弄清楚下一部分協議需要發送什么.