A socket operation was attempted to an unreachable network - c#

I am trying to validate email domain validation using the following code (Found on Code Proejct)
string hostName="<hostName>"; //Ex: yahoo.com
IPHostEntry Iphost=Dns.GetHostEntry(hostName);
IPEndPoint endPt=new IPEndPoint(Iphost.AddressList[0],25);
Socket s=new Socket(endPt.AdressFamily, SocketType.Stream, ProtocolType.Tcp);
s.Connect(endPt);
At s.Connect I am getting the error: A socket operation was attempted to an unreachable network.
What might be the possible reasons and how can I resolve them? I have Firewall (Comodo) on my machine.

The computer is not able to connect to the address that was resolved.
Look at the address you were given by Dns.Resolve.
Note: The Resolve method is obsolete, and replaced with GetHostEntry. e.g.:
IPHostEntry host = Dns.GetHostEntry("yahoo.com");

Related

Index was outside the bounds of the array at socket client c# code

I am using server and client sockets communication provided from (server, client). When I running those projects from the same machine everything is working fine. When I tried to use other pc as a client I am receiving the following exception message:
Index was outside the vounds of the array at asynchronousClient.StartClient() in line 47 which in fact is the second line:
IPHostEntry ipHostInfo = Dns.GetHostEntry("serverIp");
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port);
EDIT IPHostEntry contains the name of the PC where server is stored. However ipAdress is null.
The error is raised because ipAdress is empty. The most likely cause for this is that the hostname exists (DNS knows about the domain), however, no A records exists. For clarification, the A in A record stands for Address and this record is used to find the address of a computer connected to the internet from a name.
From the documentation of Dns.GetHostEntry:
IPv6 addresses are filtered from the results of the GetHostEntry method if the local computer does not have IPv6 installed. As a result, it is possible to get back an empty IPHostEntry instance if only IPv6 results where available for the hostNameOrAddress.parameter.
Meaning, you only got back IPv6 records, and the method filtered them for you.

Getting invalid pointer address when binding socket in C#?

On one of our teams desktop, we are getting a strange error when running this code:
IPAddress ipaddress = IPAddress.Parse(sIPDaddress);
var endpoint = new IPEndPoint(ipaddress, m_iPort);
listener.Bind(endpoint);
listener.Blocking = true;
listener.Listen(-1);
We are getting the following 10014 error on the bind command:
WSAEFAULT 10014 Bad address. The system detected an invalid pointer
address in attempting to use a pointer argument of a call. This error
occurs if an application passes an invalid pointer value, or if the
length of the buffer is too small. For instance, if the length of an
argument, which is a sockaddr structure, is smaller than the
sizeof(sockaddr).
We initially thought it was a network issue but my laptop running the same code works when connected to his network port.
The desktop is ip4 enabled and this is the only machine we are having this issue. We even changed the network card but the error still exists.
Any ideas where to look?
I found a line above the code above:
Socket listener = new Socket(0, SocketType.Stream, ProtocolType.Tcp);
I switched this to:
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
and it worked.
Still not sure why this happened on one the developer machines but not on the others.
What I did notice was when I did ipconfig on a working development machine there is a Link-local IPV6 address shown. However on the non-working machine there is no entry for a Link-local IPV6 address. Not sure if this is related.

Comparison of C# Socket Implementation

I have been looking over socket implementation and ran across a few ways of implementing them.
However I am confused as to why some examples create extra variables to accomplish the same task.
IPHostEntry ipHost = Dns.GetHostEntry("");
IPAddress ipAddr = ipHost.AddressList[0];
ServerSocket = new Socket(ipAddr.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
ServerSocket.Connect(hostName, 56);
I managed to get the above code collapsed down into two lines. Other than the ability to enumerate IP Addresses, is there another benefit to the code above?
ServerSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
ServerSocket.Connect(hostName, 56);
Thank you in advance for your help.
The intention of the first snippet is to automatically choose between IPv4 and IPv6. The first snippet probably has a bug. If there are multiple adapters (which is normal) an arbitrary address family will be chosen. Maybe IPv6 will be chosen and the connection will fail because the target of the connect call does not support IPv4.
Use the second version.
Also, this difference is not about "variables". It is about different semantics. You can arrange the variables as you see fit.
The:
IPHostEntry ipHost = Dns.GetHostEntry("");
IPAddress ipAddr = ipHost.AddressList[0];
is giving you your local Ip address. Well to clarify.. it is giving you the first one.
see Dns.GetHostEntry()
The GetHostEntry method queries a DNS server for the IP address that is associated with a host name or IP address.
When an empty string is passed as the host name, this method returns the IPv4 addresses of the local host.
The reason for the AddressList[0] is because a machine may have multiple local Ip addresses.

SocketException Connection failed

I use the sample code on MSDN but it cannot work.
Below is code:
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddr = ipHost.AddressList[2];
IPEndPoint ipEndPoint = new IPEndPoint(ipAddr, 11000);
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
client.Connect(ipEndPoint);
You will need to also have an Active TcpListener on your local machine (i'm guessing by using Dns.GetHostname()).
Instead of relying on DNS when trying to connect back to yourself, you can use IPAddress.Loopback
The code that MSDN provided is for example. This means it may not work in all circumstances.
The problem you facing is, that there is no software listening on the port 11000. (for a client to connect on a port, there should be a server listening.) irl same like: If you (client) phone your friend, but your friend(server) isn't home(listening) to pick up the homephone, there will be no conversation. ;-)

Get local IP address for socket

I'm looking to get the local IP address of the socket I just created. I need to be able to support a server with more than one NIC and communicate back to the requesting client what the direct IP address is to connect later on. I'm using for following code:
Socket rsock = null;
rsock= new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
rsock.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, 0);
rsock.Bind(new IPEndPoint(IPAddress.IPv6Any, port));
rsock.Listen((int)SocketOptionName.MaxConnections);
After this point, .LocalEndPoint kicks out: [::]:PORT.
Background:
The reason I need the IP address is that a secondary connection by another client will need to return to this specific server. These servers will likely be behind a load balancer for the initial server selection so the client cannot resolve the IP address based on the host name.
Since you're binding to IPAddress.IPv6Any, the endpoint information will not be available before the first I/O operation occurs. The documentation says:
If you allow the system to assign your socket's local IP address and
port number, the LocalEndPoint property will be set after the first
I/O operation. For connection-oriented protocols, the first I/O
operation would be a call to the Connect or Accept method.
So, in your case, you will have to call Accept() before accessing LocalEndPoint in order to obtain meaningful information.

Categories