C# client connection, Socket Exception:The system detected an invalid pointer address - c#

I've written a server and client in C# .net 4.6 that should accept both ipv6 and ipv4 connections. The relevant code for the server listener is as follows:
listener = new Socket(AddressFamily.InterNetworkV6,
SocketType.Stream, ProtocolType.Tcp);
listener.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
listener.DualMode = true;
// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(new IPEndPoint(IPAddress.IPv6Any, 3425));
listener.Listen(100);
And the client connects like this:
socket = new Socket(AddressFamily.Unspecified, SocketType.Stream, ProtocolType.Tcp);
try
{
IPAddress[] ip = Dns.GetHostAddresses(ipAddress);
StatusBoxHandler.statusText = "Connecting...";
IPEndPoint remoteEP = new IPEndPoint(ip[0], port);
Debug.Log(ip[0]);
socket.Connect(remoteEP);
I've had no problems so far with 3 different people trying to connect to this server with the given client. The fourth person, who is running Windows 8.1 however is having a bit of difficulty.
He is unable to ping the server with the hostname as it says destination unreachable, and he is unable to ping the servers ipv6 address, giving the error:
PING: transmit failed, general failure.
He is however able to ping the ipv4 address successfully. The weird bit with this is that he has ipV6 enabled and I have verified this.
When attempting to connect with the client and forcing it to use the ipv4 address he obtains the error:
System.Net.Sockets.SocketException: The system detected an invalid pointer address in attempting to use a pointer argument in a call.
I've also verified that in the log it's connecting to the exact same ipv4 address that mine would use to connect, so it's not a matter of it obtaining the wrong ip address. Additionally, I've connected from the client to the server successfully with ipv6, ipv4 and the dns hostname addresses and they all work fine for my machine and two others.
Is there some sort of problem in my code that I'm missing or is there just some sort of strange unrelated issue that is only affecting his machine?

The way IP addess is being retrieved is not correct. Instead use below way
var ipHostInfo = Dns.GetHostEntry("localhost");
var ipAddress = ipHostInfo.AddressList.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork);

Related

Connect TcpClient to a remote Tcp server, binding to a specific local port

I built tcp client/server application for my organisation. The server opens and listens to a specific port, and each client establishes Tcp Connection to the server port. Nothing special. The application works beautifully. But today, one client wanted the Tcp client app to work over WiFi network with firewall. The WiFi firewall is configured to block all ports by default. If I want my application to work, I have to give their network administrator a list of ports to open for my application. The server listening port is configurable so it is easy. Once I configure the port, I can give them is this specific port for the server. However the client app is unable to connect to the server because each time a TcpClient establishes a connection, it creates a random local Tcp port that will be blocked by their firewall.
Their network admin will not open all ports for the machine because they said it created security risks for their organisation. Therefore, I am looking for a way to force the client to open a specific local port when it establishes a Tcp connection. I've both looked into MSDN docs and been Googling but I haven't found an adequate answer. Would you be able to suggest a workaround or a third party library that can do that? Thank heaps.
I'm not aware of any way to have this level of control with TcpClient. However, if you manually create the Socket object, you can bind to a local port of your choosing:
var sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Specify the local port to use
var endpoint = new IPEndPoint(IPAddress.Any, 9999);
sock.Bind(endpoint);
// And connect to the remote end point
sock.Connect("example.com", 80);
Of course, by doing this you limit yourself to one connection on the machine.
how about use bind() like this(if you are using c++)
struct sockaddr_in cli;
memset(&cli, 0x00, sizeof(cli));
cli.sin_family = AF_INET;
cli.sin_port = htons(YOUR PORT);
cli.sin_addr.s_addr = inet_addr(YOUR IP ADDRESS);
int on = 1;
if (setsockopt(hSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0){
perror("Set Error");
}
bind(hSocket, (struct sockaddr *)&cli, sizeof(struct sockaddr));
int nRet = connect(hSocket,(sockaddr*)&svr, sizeof(svr));

What IP should I use to host my chat server?

I made chat server in C# using
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.TCP);
s.Bind(IPAddress.Parse(theIP),thePort);
But then I am getting a
"SocketException was unhandled, The requested address is not valid in
its context"
How do I look for the correct IP to use? cmd ipconfig IPv4Address? Because that IP (I believe) is for the internal IP. I want the server IP to be an external IP address that is accessible from outside my network
You need to create a static path (sometimes called port forwarding) in your router binding one of your external IP/ports to an internal address and the port needed for your chat server. You will then reference your internal IP address, on your network, in your code.

TCP server/client connection issue C#

so I am currently programming a simple server/client to play some basic games i did in the past.
The problem is that I can only connect while I'm on the same computer, and not via LAN (as I would want it to work), here's the code I'm working with:
Server:
IPEndPoint Ep = new IPEndPoint(IPAddress.Any, 8000);
listener = new TcpListener(Ep);
listener.Start();
Client:
IPAddress direc = IPAddress.Parse(ipManager);
Ep = new IPEndPoint(direc, 8000);
The problem is that when I try connect from another computer (connected to the same Wi-Fi obviously) I get the following error:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.0.5:8000
I shuold also add that this is the code when I try to connect:
Sever:
TcpClient client = listener.AcceptTcpClient();
Client:
client.Connect(Ep);
Apparently when I am connecting the server never "accepts" the connection, but I haven't been able to figure out why's that.
Since it works locally, you are probably using 127.0.0.1 or localhost as the server's listening interface. Try using 0.0.0.0 so it will listen on all interfaces or specify the IP address of the interface you want the server to listen on.
Try to connect to the port using telnet or netcat. Check if the port is accessible.
$ nc 192.168.0.5 8000
$ telnet 192.168.0.5 8000

Remote UDP send and receive

Im trying to create a server/client application that will work on two or more remote computers with no local network between them.
So i searched the internet and find a TON and TONES of C# UDP client/server examples just like here.
BUT i didn't find anywhere how to send a UDP socket over the internet on remote computer with, lets say IP="130.204.159.205";
please with your answers give me some example code
The example you linked shows how to create the socket, just remove the line IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()) and put use this constructor and pass in a byte array with your ip address 130.204.159.205
(...)
// Connect to a remote device.
try {
// Establish the remote endpoint for the socket.
// This example uses port 11000 on the local computer.
IPAddress ipAddress = new IPAddress(new byte[] { 130, 204, 159, 205});
IPEndPoint remoteEP = new IPEndPoint(ipAddress,11000);
(...)

No connection could be made because the target machine actively refuses it

I'm working on a simple hello world TCP/IP client server app in C# and am unable to get my client to connect. Can anyone offer any additional troubleshooting steps? I'm starting to run out of ideas...
Here are the relevant sections of code:
server:
Console.Out.WriteLine("About to bind address");
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
Console.Out.WriteLine("Choose a port to bind...");
String port = Console.In.ReadLine();
int iPort = Int32.Parse(port);
TcpListener myList = new TcpListener(ipAd, iPort);
myList.Start();
Console.WriteLine("The server is running at: "+myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
client:
Console.Out.WriteLine("enter address: ");
string address = Console.In.ReadLine();
Console.Out.WriteLine("enter port: ");
int port = Convert.ToInt32(Console.In.ReadLine());
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
Console.Out.WriteLine("Address: " + address + ":" + port);
tcpclnt.Connect(address, port);
I am able to ping the server from the client machine, however I am unable to telnet to the server using the bound port. I've tried a variety of ports (a few in the low 8000s and a few up around 40000). I have disable windows firewall on both systems. The systems are connected to a router which is not on the internet. I've tried with and without port forwarding set to forward incoming requests on the given port to the server machine with no effect.
The only exception that I've been able to trap is thrown by the client:
No connection could be made because
the target machine actively refuses
it.
I checked for an InnerException but it seems that there are none - that is the base exception. Could that be right?
Not sure what else I should be looking at - any additional troubleshooting steps would be helpful.
Thanks!
I've run into this before. The trick is to bind to 0.0.0.0 rather than 127.0.0.1. When you bind to 127.0.0.1 the server will only accept connections from localhost. Binding to 0.0.0.0 it will accept all requests.
You also may want to nmap the host machine from the client and see what ports it sees as being open.
EDIT: If you hard code the IP address of the machine in it the listener will only listen on that network interface. If you use 0.0.0.0 the listener will listen on all available network interfaces. This includes interfaces between your computer and a USB attached hand held device, a second network card or a VPN link.
The code above is listening to request coming from the loopback address. This will effectively only listen to connection on that network, and that network only includes your machine.
Have you tried listening to the address bound to the network from which the connection should be coming? On a local network it should be something like 192.168.x.x or 10.x.x.x
try netstat -al on your machine (the exact command line varies between Windows and unix) and see if the server is listening on the port
Why don't you use .NET remoting? It is better than doing a TCP/IP client server. You can pass messages between objects.
Have you tried running the client server on the same machine to make sure the connection is made first? Beyond that try using the router assigned or static IP of the machine running the server vs binding to loopback.

Categories

Resources