Getting invalid pointer address when binding socket in C#? - 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.

Related

C# Socket listener does not bind to port

I try to listen a specific port on server. I write a simple console app but I get error:
Only one usage of each socket address (protocol/network address/port)
is normally permitted.
However, I don't find any process to listening that port. Resource manager and CurrPorts don't show any information.
My code (all code):
var ipEndPoint = new IPEndPoint(IPAddress.Any, 7001);
var tcpListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpListener.Bind(ipEndPoint);
tcpListener.Listen(100);
My questions are:
How can I find which process listen the port? Currports doesn't show, also resmon too.
Why node.js is listen the port and getting messages? What is different?
I think I have a hidden thread but I doesn't find it. I use ProcessExplorer.
Update:
When I run my console app after server reset, it is working correctly.
However, when close and re-open the app, it is not working and given that exception above.
I believe you have connections to that port which are not completely closed. This prevents the port from being opened again. To solve the problem you have to set the socket option ReuseAddress before binding the port.
var ipEndPoint = new IPEndPoint(IPAddress.Any, 7001);
var tcpListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
tcpListener.SetSocketOption(SocketOptionLevel.Socket,SocketOptionName.ReuseAddress,true);
tcpListener.Bind(ipEndPoint);
tcpListener.Listen(100);
I run into the same problem with the mysql port 3306. Neither tcpview , netstat -ano nor currports shows anything.
But
netsh interface ipv4 show excludedportrange protocol=tcp
shows that the port is reserved.
It seems hyper-v / docker was the culprit and the fix is to disable hyper-v , reserve the port with
netsh int ipv4 add excludedportrange protocol=tcp startport=<your port> numberofports=1
and reenable hyper-v.
Origin : https://stackoverflow.com/a/54727281/32083 .
More background is here https://github.com/docker/for-win/issues/3171

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.

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.

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.

A socket operation was attempted to an unreachable network

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");

Categories