无法建立连接,因为目标计算机主动拒绝它 - 远程桌面 [英] No connection could be made because the target machine actively refused it - remote desktop
本文介绍了无法建立连接,因为目标计算机主动拒绝它 - 远程桌面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在玩构建远程桌面。
我正在使用TCP。
我收到错误'无法建立联系,因为目标机器积极拒绝它'。
关于如何使这个工作的任何想法?
客户
I am playing around with building a remote desktop.
I am using TCP.
I am getting the error 'No connection could be made because the target machine actively refused it'.
Any ideas on how to get this working?
The client
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 btnConnect_Click(object sender, EventArgs e)
{
portNumber = int.Parse(txtPort.Text.Trim());
try
{
client.Connect(txtIP.Text, portNumber);
MessageBox.Show("Connected");
}
catch(Exception ex)
{
MessageBox.Show("Failed to Connect: " + ex.Message);
}
}
private void btnShare_Click(object sender, EventArgs e)
{
if (btnShare.Text.StartsWith("Share"))
{
timer1.Start();
btnShare.Text = "Stop Sharing";
}
else
{
timer1.Stop();
btnShare.Text = "Share my screen";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
SendDesktopImage();
}
服务器如下
the server is as follows
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 btnConnect_Click(object sender, EventArgs e)
{
portNumber = int.Parse(txtPort.Text.Trim());
try
{
client.Connect(txtIP.Text, portNumber);
MessageBox.Show("Connected");
}
catch(Exception ex)
{
MessageBox.Show("Failed to Connect: " + ex.Message);
}
}
private void btnShare_Click(object sender, EventArgs e)
{
if (btnShare.Text.StartsWith("Share"))
{
timer1.Start();
btnShare.Text = "Stop Sharing";
}
else
{
timer1.Stop();
btnShare.Text = "Share my screen";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
SendDesktopImage();
}
我尝试过:
我试过运行上面的代码
What I have tried:
I have tried running the above code
推荐答案
所以你创建了两个客户端而没有服务器来监听Connections?我会再次阅读有关基础知识的内容。实现一个异步侦听套接字(如MSDN所示 - 如果你搜索套接字服务器 - 我不喜欢这样说 - 但是RTFM)...
短版本:一方面创建一个监听器:
So you created two Clients and no Server to listen for Connections? I'd say read about the Basics again. Implement an async listening socket (as showed on MSDN - if you search for Socket Server - I don't like to say it - but RTFM)...
Short Version: On one side create a listener:
Socket socketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socketListener.Bind(localEndPoint);
socketListener.Listen(100);
// then in some "wait"-Loop (async to main thread) you wait for Connections
socketListener.BeginAccept(new AsyncCallback(AcceptCallback), socketListener);
快速浏览后,客户端看起来还不错......
随意到aks进一步的问题,祝你的项目好运!
You Client side seems ok after a quick look...
Feel free to aks further questions, good luck with your Project!
这篇关于无法建立连接,因为目标计算机主动拒绝它 - 远程桌面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文