如何将我的屏幕共享给服务器和客户端,我为服务器和客户编写代码但不能正常工作 [英] How To Share My Screen To Server And Client, I Written Code For Server As Well As Client But Not Working Properly

查看:76
本文介绍了如何将我的屏幕共享给服务器和客户端,我为服务器和客户编写代码但不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器的表单1

form 1 of server








using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace share_screen
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new Form2(int.Parse(textBox1.Text)).Show();

        }
    }
}





表格2服务器



form 2 of server





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;

namespace share_screen
{
    public partial class Form2 : Form
    {
        private readonly int port;
        private TcpClient client;
        private TcpListener server;
        private NetworkStream mainStream;

        private readonly Thread Listening;
        private readonly Thread GetImage;
        public Form2( int Port)
        {
            port = Port;
            client = new TcpClient();
            Listening = new Thread(StartListening);
            GetImage = new Thread(ReceiveImage);
                InitializeComponent();
        }
        private void StartListening()
        {
            while (client.Connected)
            {
                server.Start();
                client = server.AcceptTcpClient();
            }
            GetImage.Start();
        }
        private void StopLestining()
        {
            server.Stop();
            client = null;
            if (Listening.IsAlive) Listening.Abort();
            if (GetImage.IsAlive) GetImage.Abort();
    
        }
        private void ReceiveImage()
        {
            BinaryFormatter binFormatter = new BinaryFormatter();
            while (client.Connected)
            {
                mainStream = client.GetStream();
                pictureBox1.Image = (Image) binFormatter.Deserialize(mainStream);
            }

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            server = new TcpListener(IPAddress.Any,port);
            Listening.Start();
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            StopLestining();
        }
    }
}



客户代码




client code





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;

namespace share_screen_client
{
    public partial class Form1 : Form
    {
        private readonly TcpClient client = new TcpClient();
        private NetworkStream Mainstream;
        private int portnumber;
        private static Image GrabDesktop()
        {
            Rectangle bounds = Screen.PrimaryScreen.Bounds;
            Bitmap screenshot = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
            Graphics graphic = Graphics.FromImage(screenshot);
            graphic.CopyFromScreen(bounds.X, bounds.Y, 0, 0, bounds.Size, CopyPixelOperation.SourceCopy);
            return screenshot;
        }
        private void sendDesktopImage()
        {
            BinaryFormatter binFormatter = new BinaryFormatter();
            Mainstream = client.GetStream();
            binFormatter.Serialize(Mainstream, GrabDesktop());
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            portnumber = int.Parse(textBox2.Text);
            try
            {
                client.Connect(textBox1.Text, portnumber);
                MessageBox.Show("connected");
            }
            catch(Exception)
            {
                MessageBox.Show("failed");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text.StartsWith("share"))
            {
                timer1.Start();
                button2.Text = "stop sharing";
            }
            else
            {
                timer1.Stop();
                button2.Text = "share my screen";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            sendDesktopImage();
        }
    }
}

推荐答案

请参阅我对该问题的评论。我不确定阅读您的代码以尝试修复会更有意义。并且你的代码不太适合它,它不好看:你使用硬编码的立即常量和自动生成的名称,如 timer1 button2 ,这违反了(良好的)Microsoft命名约定并且与常识相矛盾。这让代码很痛苦。您应该始终将所有自动生成的名称重命名为语义上合理的名称,这很容易使用VS重构引擎。



也许您应该使用标准RDP协议并且可能存在服务;请参阅:远程桌面协议 - 维基百科,免费的百科全书 [ ^ ]。



然后,如果你愿意,你可以实现你自己的客户部分。我在CodeProject上找到了很好的例子:

使用C#.NET的远程桌面 [ ^ ],

Palantir - 远程桌面管理器 [ ^ ]。



前一段时间,第二个,Palantir,帮助了我很多;请看我对这篇文章的评论。



-SA
Please see my comment to the question. I'm not sure that reading your code to try to fix would make much sense. And your code is not quite suitable for that, it's not nice to read: you use hard-coded immediate constants and auto-generated names such as timer1 and button2, which violates (good) Microsoft naming convention and contradict to common sense. It make the code pain to see. You should always rename all auto-generated names to something semantically sensible, which is easy to do with VS refactoring engine.

Perhaps you should use standard RDP protocol and maybe existing services; please see: Remote Desktop Protocol — Wikipedia, the free encyclopedia[^].

Then, if you want, you can implement your own client part. I found good examples of such works on CodeProject:
Remote Desktop using C#.NET[^],
Palantir - Remote Desktop Manager[^].

A while ago, the second one, Palantir, helped me a lot; please see my comment to the article.

—SA


这篇关于如何将我的屏幕共享给服务器和客户端,我为服务器和客户编写代码但不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆