没有这样的主机是已知的套接字连接

Kyl*_*yle 8 c# sockets exception telnet

我正在尝试使用此库进行 telnet 连接。我已经正确调用了该函数,它执行了下面的代码,但未能给出以下错误:

System.Net.Sockets.SocketException was unhandled
  HResult=-2147467259
  Message=No such host is known
  Source=System
  ErrorCode=11001
  NativeErrorCode=11001
  StackTrace:
       at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)
       at MinimalisticTelnet.TelnetConnection..ctor(String Hostname, Int32 Port) in c:\users\kylec\documents\visual studio 2010\Projects\Mail Server Capture\Mail Server Capture\TelnetInterface.cs:line 36
       at Mail_Server_Capture.Form1.btn_MailGet_Click(Object sender, EventArgs e) in c:\users\kylec\documents\visual studio 2010\Projects\Mail Server Capture\Mail Server Capture\Form1.cs:line 55
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at Mail_Server_Capture.Program.Main() in c:\users\kylec\documents\visual studio 2010\Projects\Mail Server Capture\Mail Server Capture\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Run Code Online (Sandbox Code Playgroud)

代码:

public TelnetConnection(string Hostname, int Port)
        {
            tcpSocket = new TcpClient(Hostname, Port);

        }
Run Code Online (Sandbox Code Playgroud)

我在这里搜索过这个问题,它似乎很常见。有些人说主机确实无法访问(事实并非如此),这是 Microsoft .NET 问题或只是可以忽略的异常。如果它是可以忽略的东西,我似乎无法让程序通过它。我似乎也找不到任何解决方案来修复它。我对这个很迷茫,任何帮助将不胜感激。

Chr*_*son 11

相关但罕见/边缘情况:我在我的 C# .NET 4.7.2 Web 应用程序(调用 http 端点)中遇到了同样的错误,因为我所在的机器(Parallels Win10 VM)默默地失去了网络连接(没有互联网) 由于一些 Parallels 错误。一旦我禁用并重新启用网络适配器,错误就消失了。/眼珠

  • 有些人很美丽,你就是其中之一;-) (2认同)

Kyl*_*yle 8

解决方案非常简单,但却被忽视了。首先我注意到 tcpclient 更喜欢 IP 地址而不是名称。然后我还意识到有时域名两边会有多余的空格。所以我使用下面的代码来剥离字符并将其更改为ip。

string.Trim();
//Telnet Start
IPHostEntry hostInfo = Dns.Resolve(hostnamehere);
Run Code Online (Sandbox Code Playgroud)

  • `Dns.Resolve()` 现已过时!更好的选择是使用“Dns.GetHostEntry()” (4认同)