每次使用C#:点击catch(exeption)创建应用程序
我试图创建一个远程桌面查看器。在应用程序的这一部分中,用户将在这里设置他们的ip和端口,以供其他用户连接。问题是:每次我按下connect按钮,它都会点击catch(exception),连接失败 分享每次使用C#:点击catch(exeption)创建应用程序,c#,visual-studio,visual-studio-2019,C#,Visual Studio,Visual Studio 2019,我试图创建一个远程桌面查看器。在应用程序的这一部分中,用户将在这里设置他们的ip和端口,以供其他用户连接。问题是:每次我按下connect按钮,它都会点击catch(exception),连接失败 分享 using System; using System.Drawing; using System.Drawing.Imaging; using System.Net.Sockets; using System.Runtime.Serialization.Formatters.Binary; us
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
namespace Remote_Desktop
{
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 graphics = Graphics.FromImage(screenshot);
graphics.CopyFromScreen(bounds.X, bounds.Y, 0, 0,bounds.Size, CopyPixelOperation.SourceCopy);
return screenshot;
}
private void SendDesktopImage()
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
mainStream = client.GetStream();
binaryFormatter.Serialize(mainStream, GrabDesktop());
}
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
portNumber = int.Parse(txtPort.Text);
try
{
client.Connect(txtIp.Text, portNumber);
MessageBox.Show("Connected!");
}
catch(Exception)
{
MessageBox.Show("Failed To Connect!");
}
}
private void btnSend_Click(object sender, EventArgs e)
{
if (btnSend.Text.StartsWith("Share"))
{
timer1.Start();
btnSend.Text = "Stop Sharing";
}
else
{
timer1.Stop();
btnSend.Text = "Share Screen";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
SendDesktopImage();
}
}
}
任何帮助都意味着非常感谢!如果有任何问题,告诉我我会设法解决 忘了多给点背景什么的。很快更新帖子!您在按钮connect click中编写了连接到外部服务的代码<代码>client.Connect(txtIp.Text,端口号)您是否在该IP端口上运行服务?如果我理解正确,我认为答案是否定的。因此
客户端。Connect
必须连接到某个外部服务。请启动您要连接的服务,然后重试。您可以使用TcpServer
并将其托管在应用程序中(侦听传入的连接),并将GrabDesktop
的结果发送给正在连接的客户端。但是,您必须实现一些授权协议,不允许任何客户端连接。@oleksa好的,将尝试。希望尽快回复您!