首页 > 其他分享 >使用UDP和RDP共享电脑屏幕和声音

使用UDP和RDP共享电脑屏幕和声音

时间:2023-08-02 14:33:29浏览次数:40  
标签:UDP RDP void object private new public 电脑屏幕 string

public partial class Form1 : Form
    {


        private WasapiLoopbackCapture mic;//音频输入
        protected RDPSession _rdpSession = null;

        public Form1()
        {
            InitializeComponent();
        }
        static Thread receiveThread;
        private void Form1_Load(object sender, EventArgs e)
        {
            SocketReceiver socketReceiver = new SocketReceiver();
            socketReceiver.Send();
        }


        public string GetInvitationString()
        {
            _rdpSession = new RDPSession();  // 新建RDP Session
            _rdpSession.OnAttendeeConnected += new _IRDPSessionEvents_OnAttendeeConnectedEventHandler(OnAttendeeConnected);
            _rdpSession.OnAttendeeDisconnected += new _IRDPSessionEvents_OnAttendeeDisconnectedEventHandler(OnAttendeeDisconnected);
            _rdpSession.OnControlLevelChangeRequest += new _IRDPSessionEvents_OnControlLevelChangeRequestEventHandler(OnControlLevelChangeRequest);//用户的级别
            //_rdpSession.SetDesktopSharedRect(0, 0, 991, 699); // 设置共享区域,如果不设置默认为整个屏幕,当然如果有多个屏幕,还是设置下主屏幕,否则,区域会很大           
            _rdpSession.Open();
            IRDPSRAPIInvitation pInvitation = _rdpSession.Invitations.CreateInvitation("WinPresenter", "PresentationGroup", "", 5);//获取连接的字符串
            string invitationString = pInvitation.ConnectionString;
            return invitationString;
        }

        /// <summary>
        /// 开始
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {

            textBox1.Text = GetInvitationString();
            SoundSender sender1 = new SoundSender("172.21.47.86");
            sender1.StartSending();
        }


        private void OnAttendeeConnected(object pObjAttendee)
        {
            IRDPSRAPIAttendee pAttendee = pObjAttendee as IRDPSRAPIAttendee;
            pAttendee.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_VIEW;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                _rdpSession.Close();
                Marshal.ReleaseComObject(_rdpSession);
                _rdpSession = null;
            }
            catch (Exception ex)
            {

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            new Form3().Show();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text.Trim()))
            {
                MessageBox.Show("IP地址不能为空");
                return;
            }
            SocketSend send = new SocketSend();
            send.Send(textBox2.Text.Trim());
            button5.Enabled = false;
        }

        void OnAttendeeDisconnected(object pDisconnectInfo)
        {
            IRDPSRAPIAttendeeDisconnectInfo pDiscInfo = pDisconnectInfo as IRDPSRAPIAttendeeDisconnectInfo;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            var txtMsg = this.button4.Text.Trim();
            if (txtMsg == "获取声音")
            {
                SoundReceiver receiver = new SoundReceiver();
                receiver.StartReceiving();
                this.button4.Text = "关闭声音";
            }
            else
            {
                SoundReceiver receiver = new SoundReceiver();
                receiver.StopReceiving();
                this.button4.Text = "获取声音";
            }

        }

        /// <summary>
        /// 更改控制级别
        /// </summary>
        /// <param name="pObjAttendee"></param>
        /// <param name="RequestedLevel"></param>
        void OnControlLevelChangeRequest(object pObjAttendee, CTRL_LEVEL RequestedLevel)
        {
            IRDPSRAPIAttendee pAttendee = pObjAttendee as IRDPSRAPIAttendee;
            pAttendee.ControlLevel = RequestedLevel;
        }

    }

 

 

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public string connctionString = "";
        private void axRDPViewer1_Enter(object sender, EventArgs e)
        {

        }
       

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        public void SetConnectionString(string conn)
        {
            connctionString = conn;
        }

        public void button1_Click(object sender, EventArgs e)
        {
            //new Form3().Show();
            Connect(connctionString);
        }
        /// <summary>
        /// 开始共享仪器客户端界面
        /// </summary>
        public void Connect(string connctionString)
        {
            string ConnectionString = connctionString;
            if (ConnectionString != null)
            {
                try
                {
                    axRDPViewer1.Connect(ConnectionString, "Viewer1", "");
                }
                catch (Exception ex)
                {

                }
            }

        }
        /// <summary>
        /// 结束控制
        /// </summary>
        public void stopcontrol()
        {
            axRDPViewer1.RequestControl(RDPCOMAPILib.CTRL_LEVEL.CTRL_LEVEL_MAX);
        }
        /// <summary>
        /// 开始控制
        /// </summary>
        public void control()
        {
            axRDPViewer1.RequestControl(RDPCOMAPILib.CTRL_LEVEL.CTRL_LEVEL_MAX);
        }
        /// <summary>
        /// 停止共享仪器客户端界面
        /// </summary>
        public void DisConnect()
        {
            axRDPViewer1.Disconnect();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            control();
        }
    }

 

public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var form= new Form2();
            form.Show();
            form.SetConnectionString(textBox1.Text);
            form.button1_Click(sender,e);
            this.Hide();
        }
    }
public class SocketReceiver
    {
        // 本地IP地址和端口
        string ipAddress = "0.0.0.0";
        int port = 12344;
        Thread thread;
        Socket receiverSocket;
        public string Send()
        {
            string receivedMessage = "";
            thread = new Thread(ReceiveData);
            thread.Start();
            return receivedMessage;
        }

        public void ReceiveData()
        {
            // 创建接收端Socket
            receiverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
           
            try
            {
                // 创建本地终结点
                IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);

                // 将Socket绑定到本地终结点
                receiverSocket.Bind(localEndPoint);
                while (true)
                {
                    // 接收数据
                    byte[] buffer = new byte[1024];
                    EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    int bytesRead = receiverSocket.ReceiveFrom(buffer, ref remoteEndPoint);

                    // 将接收到的数据转换为字符串
                    string receivedMessage = Encoding.UTF8.GetString(buffer, 0, bytesRead);
                    SoundSender sender1 = new SoundSender(receivedMessage);
                    sender1.StopSending();
                    sender1.StartSending();
                }
                  

            }
            catch (Exception ex)
            {
                // 关闭接收端Socket
                receiverSocket.Close();
            }
            finally
            {
               
            }
        }


    }
 public class SocketSend
    {
        // 目标IP地址和端口
        string ipAddress = "127.0.0.1";
        int port = 12344;
        public void Send(string message)
        {
            //message = "Hello, Receiver!";
            // 创建发送端Socket
            Socket senderSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            try
            {
                byte[] data = Encoding.UTF8.GetBytes(message);

                // 创建目标终结点
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), port);

                // 发送数据到目标终结点
                senderSocket.SendTo(data, endPoint);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                // 关闭发送端Socket
                senderSocket.Close();
            }
        }
    }
 public class SoundReceiver
    {
        public static UdpClient udpClient;
        public static IPEndPoint endPoint;
        public static WaveOut waveOut;
        public static BufferedWaveProvider waveProvider;

        public SoundReceiver()
        {
        }

        public void StartReceiving()
        {
            udpClient = new UdpClient(12345); // 接收端监听的端口号
            endPoint = new IPEndPoint(IPAddress.Any, 0);

            waveOut = new WaveOut();
            waveProvider = new BufferedWaveProvider(new WaveFormat(44100, 1));

            waveOut.Init(waveProvider);
            udpClient.BeginReceive(ReceiveCallback, null);
            waveOut.Play();
        }

        public void StopReceiving()
        {
            waveOut.Stop();
            udpClient.Client.Shutdown(SocketShutdown.Both);
            udpClient.Close();
        }

        private void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                byte[] receivedData = udpClient.EndReceive(ar, ref endPoint);
                waveProvider.AddSamples(receivedData, 0, receivedData.Length);
                udpClient.BeginReceive(ReceiveCallback, null);
            }
            catch (Exception)
            {
            }
          
        }
    }
 public class SoundSender
    {
        private WasapiLoopbackCapture waveIn;
        private UdpClient udpClient;
        private IPEndPoint endPoint;

        public SoundSender(string ipAddress)
        {
            waveIn = new WasapiLoopbackCapture();
            waveIn.WaveFormat = new WaveFormat(44100, 1);

            udpClient = new UdpClient();
            endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), 12345); // 替换为接收端的IP地址

            waveIn.DataAvailable += WaveIn_DataAvailable;
        }

        public void StartSending()
        {
            waveIn.StartRecording();
        }

        public void StopSending()
        {
            waveIn.StopRecording();
        }

        private void WaveIn_DataAvailable(object sender, WaveInEventArgs e)
        {
            udpClient.Send(e.Buffer, e.BytesRecorded, endPoint);
        }
    }

以上代码仅供参考,是测试代码

 

标签:UDP,RDP,void,object,private,new,public,电脑屏幕,string
From: https://www.cnblogs.com/liao-long/p/17600593.html

相关文章

  • WordPress Qui-Pure V2.4发布纯文本/图文博客主题正式发布!
    主题介绍:Qui-Pure是我开发的第一款主题,纯文本展示博客类型,后台控制是否加载图片/轮播图,页面布局改成图文排版!兼容erphpdown,加入个人中心,由于技术学习来源互联网,WordPress是开源平台,因此主题免费回报大家,希望大家喜欢这款简约至上的主题!主题免费、免费、免费...主题功能:1.......
  • udp接收上位机编程(2)彩色图像
    由于QT上位机只能接收BGR565的图像格式,且只能显示灰度或者RGB888,所以PL2PS的数据必须要变换位置,并使用cvtColor函数进行转换1voidMainWindow::recieve_dis(intudp_index)2{3Matrecv_img_2(img_h_size,img_w_size,CV_8UC2);4Matrecv_img_3(img_h_size,img_......
  • 009 python socket UDP服务端程序,在pycharm正常运行,生成的exe运行有时候接收不到数据,
    写了一个简单的socket服务端程序,在pycharm中正常,生成exe后运行,有时候接收不到数据。经过半天折腾,发现,只要按一下回车,程序又能正常运行了。最终发现,是因为控制台的快速编辑功能打开导致程序假死。在控制台程序的菜单栏右键点击--->默认值--->去掉“快速编辑模式”和“插入模式”......
  • Wordpress:在Fastcomet中如何进行网站备份?
    使用Fastcomet平台搭建Wordpress网站后,为了安全,需要进行备份,如何操作呢?步骤如下:1.登陆Fastcomet后台后,点击网站后面的AccesscPanel,进入网站面板管理; 2.选择cPanel下的WPToolkit选项,点击Backup/Restore 3.侧边弹出的面板中,点击BackUp,底部会出现进度条,等待备份OK后,会......
  • linux 4 网络基础 POLL EPOLL epoll堆 线程池 udp 本地套接字
    Linux5day1.poll监听poll相对与sellect的优缺点优点:没有文件描述符1024的限制请求和返回是分离的如:selectread集合返回read集合缺点和select一样:每次都需要将需要监听的文件描述符从应用层拷贝到内核每次都需要将数组中的元素遍历一遍才知道那个变化了大量并发,少......
  • 【wordpress开发必备】新增必填字段相关函数和钩子,适用6.1版本
    当表单包含多个必填字段时,它们的标签可能带有一个带有图例的星号,以说明这些字段是必填的。为了减少代码重复并帮助维护全局一致的标记,WordPress有两个新函数:wp_required_field_indicator()和wp_required_field_message()。如果主题和插件至少需要 WordPress6.1,它们也可以使用这......
  • v831-T113-c-udp发送篇
    又来分析udp,目标是从v831中获取到的图片信息发送给t113然后再屏幕上显示出来挫折一t113作为服务端,再接收到客户端发来的数据后会自动设置clinetaddr,那么我们就不再需要取从新aton这个ip地址了,直接使用clientaddrintudp_send(void){ //if(0==inet_aton("192.168.1.118",......
  • wordpress pwnscriptum
    vulhub/wordpress/pwnscriptum/README.zh-cn.mdatmaster·vulhub/vulhub·GitHub(191条消息)漏洞复现-Wordpress4.6PwnScriptumRCE命令执行_wordpressrce_amingMM的博客-CSDN博客1、介绍名称:Wordpress4.6任意命令执行漏洞编号:原理:漏洞缺陷处在后台找回密码的地......
  • t113-c-udp篇
    之前学习了tcp发送数据,但是效果很不理想,那么试一下用udp会怎么样,学习一下udp是怎么用用的吧看例子例子是在百问网下载的,serverclient这个有点奇怪,有点看不懂#include<sys/types.h>/*SeeNOTES*/#include<sys/socket.h>#include<string.h>#include<sys......
  • wordpress 插件 woocommerce自定义订单信息验证
    使用php钩子函数增加自定义验证add_action('woocommerce_after_checkout_validation',function($fields){if($fields['billing_phone']&&!preg_match('/^((\+1|1)?(|-)?)?(\([2-9][0-9]{2}\)|[2-9][0-9]{2})(|-)?([2-9][0-9]{2}(|-)?[0-9......