No such host is known socket connection
Asked Answered
A

6

19

I'm trying to work with this library for telnet connections. I have called the function correctly and it executes the code below but fails giving the following error:

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: 

Code:

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

        }

I have searched here looking for this problem and it seems pretty common. Some people are saying the host truly is unreachable (this is not the case), its a Microsoft .NET problem or its just an exception that can be ignored. I can't seem to get the program to pass it though if it is something that can be ignored. I also can't seem to find any solutions as to fixing it. I'm pretty lost on this one, any help would be appreciated.

Andy answered 30/5, 2014 at 18:58 Comment(11)
What is the prefix on the hostname that you are giving?Mutable
Does it work with an IP address insread of hostname?Amosamount
prefix? I had passed something like e32.co.us.ibm.comAndy
@keiv.fly, I can't test that as my code resolves these mx addresses from a domain name.Andy
use console command: "ping e32.co.us.ibm.com". It will show you the IPAmosamount
I have the Ip as this, 32.97.110.150. But my code that passes to that function doesn't resolve an ip from the domain, it resolves the mx domain addresses of a domain. I can do the entire process start to finish from the cmd and it works fine. But I can't get my code to do it.Andy
Sorry I stand corrected, I was able to edit the data going into the function using a breakpoint and I got the same result with an ip address instead of a name space.Andy
Did you test the smtp connection? Though you wrote that you tested everything from cmd I do not see the exact commands. So use Windows telnet command to verify the connection to port 25 (smtp). It is explained here: technet.microsoft.com/en-us/library/aa995718(v=exchg.65).aspxAmosamount
And try mxtoolbox.com to verify that mx record is resolved correctly.Amosamount
Yes I have verified using telnet in the cmd. And yes according to mxtoolbox, the sample domain I posted is valid for ibm.comAndy
Can be a simple space at end of host address like '192.168.1.119 'Landmeier
A
8

The solution was something quite easy and overlooked. First I noticed that the tcpclient prefers an ip address and not a name. Then I also realized that sometimes there were extra spaces on either side of the domain name. So I used the below code to strip the characters and change it to an ip.

string.Trim();
//Telnet Start
IPHostEntry hostInfo = Dns.Resolve(hostnamehere);
Andy answered 2/6, 2014 at 15:22 Comment(2)
Just a note that you need to dig into the IPHostEntry to get the actual IP: hostInfo.AddressList[0].ToString()Denunciatory
Dns.Resolve() is now obsolete! Better option is to use Dns.GetHostEntry()Sammons
M
18

Related but rare/fringe case: I was getting this same error in my C# .NET 4.7.2 web app (calling a http endpoint) because the machine I was on (a Parallels Win10 VM) had silently lost its network connection (no internet) due to some Parallels bug. Once I disabled and re-enabled the network adapter the error went away. /eyeroll

Magician answered 4/2, 2020 at 11:56 Comment(0)
A
8

The solution was something quite easy and overlooked. First I noticed that the tcpclient prefers an ip address and not a name. Then I also realized that sometimes there were extra spaces on either side of the domain name. So I used the below code to strip the characters and change it to an ip.

string.Trim();
//Telnet Start
IPHostEntry hostInfo = Dns.Resolve(hostnamehere);
Andy answered 2/6, 2014 at 15:22 Comment(2)
Just a note that you need to dig into the IPHostEntry to get the actual IP: hostInfo.AddressList[0].ToString()Denunciatory
Dns.Resolve() is now obsolete! Better option is to use Dns.GetHostEntry()Sammons
P
4

I'm going to put this answer in here for people who are getting this error like me, who never seemed to find an answer:

If you're trying to grab a client hostname from the DNS server within your company's network using: Dns.GetHostEntry(ClientIP) and each time you keep getting the "No such host is known socket connection error," your DNS server may not have Reverse Lookup Zones set for the IP range you're passing in code.

For example, I have a program that works with our web application that captures the computer name and IP address of the employee submitting a help request. It worked if the computer using the application was on a vlan that was in the reverse lookup zone.

If you have access to your domain's DNS server, add reverse lookup zones for all of the IP addresses that you use on your domain, or have your network administrator do it.

Always have a try catch around it, just in case you have a situation where someone accesses your webapp from outside of your network.

Knowing this would have saved me hours of frustration.

Parole answered 8/4, 2020 at 14:47 Comment(0)
D
2

#No such host is known in asp.net web api

In your appsetting.json file check for the connection string if host name not included add the host name eg:

"ConnectionStrings": {
    "AppCon" : "Server=yourDbServer;Database=YourDbName;Host:localhost;Port=5050;User Id=postgres;Password=1234"
}

This Solved My problem.Check Other Forums also

Dyke answered 4/3, 2022 at 10:49 Comment(2)
Yeah this was my issue as wellAntarctica
This was your issue because Server option is non needed and the Host is the right property for NPGSQL as the commendation describes. (I assume you use NPGSQL because of postgres in you connection string). npgsql.org/doc/connection-string-parameters.htmlFerdelance
W
0
"ConnectionStrings": {
    "AppCon" : "Server=yourDbServer;Database=YourDbName;Host:localhost;Port=5050;
User Id=postgres;Password=1234"
}
Weintraub answered 5/6, 2023 at 12:13 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Effulgence
Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?Langton
P
0

If you are using hostname to connect, try switching to ip and see if that works. That fixed this error for me.

Here is my connection string before:

      "ConnectionStrings": {
"DefaultConnection": "Host=rpi5-8gb-green.local;Port=9999;Username=myuser;Password=mypass;Database=postgres;"}

Here is the updated and working one:

  "ConnectionStrings": {
"DefaultConnection": "Host=192.168.1.120;Port=9999;Username=myuser;Password=mypass;Database=postgres;"}

Since I used avahi-daemon for ease, why not use hostname directly at the code too? For me, using the ip is the solution.

Persona answered 10/6 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.