zoukankan      html  css  js  c++  java
  • 命名管道不能实现局域网内通信,该怎么处理

    原文链接:http://www.myexception.cn/vc-mfc/304571.html

    命名管道不能实现局域网内通信,该怎么处理

     
    www.MyException.Cn  网友分享于:2015-08-26  浏览:157次
     
     
     
     
    命名管道不能实现局域网内通信
    服务器端创建命名管道:

    hPipe=CreateNamedPipe("\\.\pipe\MyPipe",
    PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,
    0,1,1024,1024,0,NULL);
    if(hPipe==INVALID_HANDLE_VALUE)
    {
    MessageBox("Failed to create named pipe!");
    hPipe=NULL;
    return;
    }
    HANDLE hEvent;
    hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
    if(!hEvent)
    {
    MessageBox("Failed to create event!");
    CloseHandle(hPipe);
    hPipe=NULL;
    return;
    }

    OVERLAPPED ovlap;
    ZeroMemory(&ovlap,sizeof(OVERLAPPED));
    ovlap.hEvent=hEvent;

    if(!ConnectNamedPipe(hPipe,&ovlap))
    {
    if(GetLastError()!=ERROR_IO_PENDING)
    {
    MessageBox("Failed to wait for client!");
    CloseHandle(hPipe);
    CloseHandle(hEvent);
    hPipe=NULL;
    return;
    }
    }

    if(WaitForSingleObject(hEvent,INFINITE)==WAIT_FAILED)
    {
    MessageBox("Failed to wait for event!");
    CloseHandle(hPipe);
    CloseHandle(hEvent);
    hPipe=NULL;
    return;
    }
    CloseHandle(hEvent);

    客户端创建命名管道:


    if(!WaitNamedPipe("\\UT\pipe\MyPipe",NMPWAIT_WAIT_FOREVER))
    {
    MessageBox("No available pipe!");
    return;
    }
    hPipe=CreateFile("\\UT\pipe\MyPipe",GENERIC_READ|GENERIC_WRITE,
    0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if(hPipe==INVALID_HANDLE_VALUE)
    {
    MessageBox("Failed to open named pipe!");
    hPipe=NULL;
    return;
    }

    其中UT为服务器主机名。以上两个程序是照孙鑫的《VC++深入详解》抄的。

    把这两个程序都放在同一个机子上可以通信。但放在局域网的两个主机上就不能通信了,请问为什么?错在哪?

    ------解决方案--------------------
    看似乎否有防火墙等拦截.. 
    ------解决方案--------------------
    代码似乎没看出什么问题 
    ------解决方案--------------------

    我试验的结果是,如果直接连,很可能连不上,
    但如果你用 win键 + R ,然后输入目标机器地址,手动连接到目标机器上
    然后再运行程序就可以连上了 
    ------解决方案--------------------
    先与目标机器建立IPC连接试试看
    一种办法是在程序中执行命令
    net use \ip\ipc$ password /user:user  
    user和password是你用于访问的用户名和密码
    另一种办法是用下面的程序建立IPC连接
    C/C++ code
    BOOL ConnetIPC(char * RemoteName,char * User,char * PassWord) 
    { 
      char tmp[128]="\\"; 
     strcat(tmp,RemoteName); 
     strcat(tmp,"\ipc$"); 
     NETRESOUCE NetResouce; 
     NetResouce.lpRemoteName=tmp; 
     NetResouce.dwType=RESOURCETYPE_ANY; 
     NetResouce.lpProvider=NULL; 
     if (WnetAddConnection2(&NetResouce,PassWord,User,FLASE)==NO_ERROR) 
      //建立连接! 
      return FALSE; 
     else 
      return TRUE; 
    }
    
    ------解决方案--------------------
    C/C++ code
        SECURITY_ATTRIBUTES     sa;
        SECURITY_DESCRIPTOR     sd;
    
        if( InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION) )
        {
    // add a NULL disc. ACL to the
    // security descriptor.
            if (SetSecurityDescriptorDacl(&sd, TRUE, (PACL) NULL, FALSE))
            {
                sa.nLength = sizeof(sa);
                sa.lpSecurityDescriptor =&sd;
                sa.bInheritHandle = TRUE;
    
                //在这里
                CreateNamedPipe(.......&sa);
            }
                
    }






  • 相关阅读:
    Study From DevOps 学习交流会议
    GS 服务器超时时间设置
    k8s 实验过程中遇到的两个小问题 端口 和 批量删除Error的pods
    这两天学到的简单Linux的命令
    Prometheus 和 Grafana的简单学习
    jenkins 添加 k8s 云
    常用的cpl 命令 运行直接打开控制台的简单方法
    [转贴] VIM 常用快捷键 --一直记不住
    [转载] 什么是istio 官网内容
    微软补丁201807补丁惹祸
  • 原文地址:https://www.cnblogs.com/huhu0013/p/10740600.html
Copyright © 2011-2022 走看看