• [h5棋牌项目]-15-C#与C++通信


    参考资料

    C#与C++通信 https://blog.csdn.net/chihun_love/article/details/53944425

    C#和C++的Socket通信 https://www.cnblogs.com/rainbow70626/p/8000984.html

    C#与c++的通信 [问题点数:30分] https://bbs.csdn.net/topics/390314206?page=1

    Windows下C++程序与C#程序间通信 https://wenku.baidu.com/view/5f7f7b2fed630b1c59eeb592.html

    C++代码

    #include <windows.h>
    #include <iostream>
    #include <tchar.h>
    using namespace std;
    int main()
    {
    	HANDLE h; 
    	TCHAR * pipename=_T("\\.\pipe\testpipe"); //管道名,要跟服务器的一致
    	//等待这个命名管道可用,确保服务器已经运行
    	if (WaitNamedPipe( pipename, NMPWAIT_WAIT_FOREVER)==FALSE ){
    		cout << "请先运行服务器!" << endl; 
    		return 1;
    	}
    	//打开管道文件以便读写,实际上连接到命名管道服务器程序
    	h=CreateFile(pipename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ| FILE_SHARE_WRITE,NULL, OPEN_EXISTING, 
    		FILE_ATTRIBUTE_ARCHIVE|FILE_FLAG_WRITE_THROUGH, NULL); 
    	if (h!=INVALID_HANDLE_VALUE){
    		char buf[100];
    		DWORD len;
    		if(ReadFile(h,buf,100,&len,NULL)){  //管道连接成功,从管道读取数据
    			buf[len] = '';				//设置字符串结束标志,输出读取到的数据
    			cout << buf << endl;
    		}
    		WriteFile(h,buf,len,&len,NULL);		//回送刚才读取的数据到服务器
    		Sleep(3000);						//等待以便服务器取走回送的数据
    		CloseHandle(h);						//关闭管道连接
    	}
    	return 0;
    }
    

    C#代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.IO;
    using System.IO.Compression;
    using Newtonsoft.Json.Linq;
    using System.IO.Pipes;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void test()
            {
                //创建命名管道
                NamedPipeServerStream pipeServer = new NamedPipeServerStream("testpipe");
                //等待客户端连接
                pipeServer.WaitForConnection();
                Console.WriteLine("Client connected.");
                try
                {
                    StreamWriter sw = new StreamWriter(pipeServer); //准备管道writer
                    StreamReader sr = new StreamReader(pipeServer); //准备管道reader
                    sw.AutoFlush = true;                            //设置写后自动刷新
                    Console.Write("Enter text: ");                  //读取用户从键盘输入的数据发送到客户端
                    sw.WriteLine(Console.ReadLine());
                    Console.WriteLine("客户端回送的数据:" + sr.ReadLine());//读取客户端回传的数据
                }
                catch (IOException e)                               //异常处理
                {
                    Console.WriteLine("ERROR: {0}", e.Message);
                }
            }
    
            static void Main(string[] args)
            {
                test();
            }
        }
    }
    
  • 相关阅读:
    js总结:增加和减少文本框
    java总结:字符串切割
    Spring-----ioc
    Hibernate之二级缓存
    Hibernate之HQL
    Hibernate关联关系(一对多自关联 ,多对多)
    Hibernate关联关系(一对多)
    Hibernate之主键生成策略
    如何使用hibernate完成CRUD操作
    Struts2-----文件上传与拦截器原理
  • 原文地址:https://www.cnblogs.com/byfei/p/14104139.html
Copyright © 2020-2023  润新知