使用UDP和RDP共享电脑屏幕和声音
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); } }
以上代码仅供参考,是测试代码