The problem is, the following code works well if IPAddress.Any was given as a parameter, but throws an error if `IPAddress.IPv6Any is used.
I receive error #10057
Socket is not connected.
A request to send or receive data was disallowed because the socket is
not connected and (when sending on a datagram socket using
sendto) no address was supplied. Any other type of operation might
also return this error—for example, setsockopt setting SO_KEEPALIVE if
the connection has been reset.
Why does it fails to work as IPv6Any? I'm pretty sure it's not the firewall, since the port remains the same and it works with IPv4 (and my firewall should pass any requests made by my application).
To short up the code, it's something like this:
The Listener:
listener = new TcpListener(IPAddress.IPv6Any, portNr);
listener.AllowNatTraversal(true);
listener.Start();
listener.BeginAcceptTcpClient(this.AcceptClient, null);
The Client:
client = new TcpClient();
client.NoDelay = true;
try
{
this.client.Connect(ip, port); //ip = "localhost" when connecting as server
}
catch (Exception ex)
{
FileLogger.LogMessage(ex);
Disconnect();
}
I'm trying to set up the "server-side" of the TCP-connection.
What I do is that I start a listener at localhost, and then connect to it as a client (and allow others to join as clients as well).
What I'm trying to achieve with this is direct addressability of this TCP server, following this article: http://blogs.msdn.com/b/ncl/archive/2009/07/27/end-to-end-connectivity-with-nat-traversal-.aspx
The reason I'm doing this is that I want person A to be able to connect to a person B when they both are behind NAT routers.
I know this answer comes a bit late, but I also had this issue and it was client related. The problem is, that your provided code...
client = new TcpClient();
... creates an IPv4-Instance of the TcpClient that is not capable of interpreting IPv6-Addresses. So if you already have the IP address at the moment of initialization, try to initialize your TcpClient like this:
TcpClient client = new TcpClient(ip.AddressFamily);
If the variable ip is a string, you need to convert it to type IPAddress first:
IPAddress iAddr = IPAddress.Parse(ip);
Actually a IPv6-TcpClient seems to be compatible to IPv4-Addresses as well, so you can also initialize your client as follows:
TcpClient client = new TcpClient(AddressFamily.InterNetworkV6)
Whilst the upper suggestions seem to be the cleanest ones, the bottom suggestion seems to be the more universal one. At the end it's up to your preferences.
I solved a similar issue where the following line would only block connections coming from IPv4 addresses:
listener = new TcpListener(IPAddress.IPv6Any, portNr);
It seems the socket when configured to accept IPv6 connections, by default, accepts ONLY IPv6 connections. To fix this problem i had to update my code to this:
listener.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
listener.Start();
When you use TcpClient.Connect(string, int) you make it possible to break because of DNS resolution.
Though Microsoft documents that IPv6 address will be tried first, the resolution may only return IPv4 addresses.
http://msdn.microsoft.com/en-us/library/8s2yca91.aspx
Therefore, can you try to call TcpClient.Connect(IPAddress.IPv6Loopback, port) on client side to test if it works?
IPAddress.Loopback == FAIL
IPAddress.IPv6Loopback == SUCCESS
Perhaps localhost is mapping to the IPv4 Loopback Address in your case?
Related
I am trying to use the TCP protocol with the System.Net.Sockets library to have one computer send a string to the other. Here is part of my host code. Is the listener object supposed to be declared with the client IP address or the host IP address (like it is in the code below)?
IPAddress ipAddr = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0]; //Automatically retrieves IPAddress.
int port = 135; //specify port number.
TcpListener listener = new TcpListener(ipAddr, port);
Also, here is a piece of my client code. Which constructor do I use?
TcpClient client = new TcpClient();
The listener object listens on the host's IP. The constructor takes it as an argument because you could have several IP's on the computer and it doesn't know which one you want to listen on.
As far as the client goes, you can pick whichever constructor you want. By using the parameterized constructors, you can connect right away, whereas the default requires a call to one of the overloads of Connect. It mostly depends on if you actually want to connect at instantiation time.
See MSDN for the overloads you can use.
Here is a TCP server sample code:
int port = 135; //specify port number.
TcpListener listener = new TcpListener(IPAddress.Any, port);
// Start listening for client requests
listen.Start();
And then you can select to use polling method to create a simple sync TCP server.
The client (PowerShell):
$Address = [System.Net.Dns]::GetHostAddresses("<hostName>")
$Port = 135
$client = New-Object System.Net.Sorckets.TcpClient
$Client.Connect($Address, $Port)
I'm currently developing a network application on my machine with 2 network interfaces with the following setup -
I want to send UDP message by using the first configuration with IP 192.168.1.2.
using (var udpClient = new UdpClient(5556))
{
udpClient.Connect(IPAddress.Parse("192.168.1.2"), 5556);
// DO STUFF
}
When I try this I get the following error -
No connection could be made because the target machine actively
refused it
Strange this is that when I disable my other network that this works perfectly but with 2 connections (LAN & WiFi) it doesn't work anymore...
I guess that it is sending on wrong adapter?
Is this because my Default gateway is the same or what am I doing wrong?
I'm new to developing network-based applications...
You have to specify an IPEndPoint with networking card IP like this:
var endpoint = new IPEndPoint(IPAddress.Parse("192.168.1.2"), portNum);
UdpClient client = new UdpClient(endpoint);
You are not telling UdpClient which IP to use.
UdpClient has a constructor that can take an IPEndPoint.
const string ip = "192.168.1.2";
const int port = 5556;
var listenEndpoint = new IPEndPoint(IPAddress.Parse(ip), port);
var udpClient = new UdpClient(listenEndpoint);
I'm not that good in C# but I think I understand the problem. You are binding your udpClient to port 5556 but you're not specifying the IP address. The OS will select the IP address for you. I suppose it could be either 192.168.1.2 or 192.168.1.5, you cannot tell which one.
Then with the same udpClient you are trying to connect to a remote host listening on 192.168.1.2, port 5556. And probably nobody is listening there. This is why you receive the error.
I would do the following (sorry for my bad C# skills):
Have two udpClient's, in one thread or program/process:
var udpClient1 = new UdpClient("192.168.1.2", 5556);
This one will listen on IP addres 192.168.1.2, port 5556.
in second thread or program/process:
var udpClient2 = new UdpClient(); //doesn't matter which (IP, port) the OS will bind it to.
udpClient2.Connect(IPAddress.Parse("192.168.1.2"), 5556); //connect to peer/server listening in (IP=192.168.1.2, port=5556)
You could handle both udpClient1 and udpClient2 in the same thread also.
The important is that if you connect to and (IP, port) there has to be some application/process listening there. Otherwise, the remote host will send an ICMP message indicating the datagram cannot be delivered. The connection is refused.
I successfully made a client server program in C# working perfectly under a LAN, we used the TcpListener and TcpSocket class.
We could not get it to work over the internet though, I understand that it has to do with firewalls, router port blocking etc.
We forwarded the port we used and turned off our firewalls and still no luck.
What do I have to do differently in order to make this work? Like a certain port to use that will work without issues? How does say "Msn Messenger" do it?
Server Code:
private static TcpListener serverTcpListener;
public static bool Run()
{
// Initialize new thread for client communications
Thread listenThread = new Thread(new ThreadStart(ListenForClients));
// Initialize TCP listener and attempt to start
ServerTcpListener = new TcpListener(IPAddress.Any, 3000);
try
{
ServerTcpListener.Start();
}
catch (SocketException)
{
return false;
}
// Start client communications thread
listenThread.Start();
return true;
}
public static void ListenForClients()
{
while (true)
{
TcpClient client = ServerTcpListener.AcceptTcpClient();
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
Client Code:
private TcpClient myClient;
private NetworkStream clientStream;
public InitializeResult Initialize()
{
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
try
{
MyClient.Connect(serverEndPoint);
}
catch (SocketException)
{
return InitializeResult.AccessError;
}
catch (ArgumentNullException)
{
return InitializeResult.NullRemote;
}
try
{
ClientStream = MyClient.GetStream();
}
catch (Exception)
{
return InitializeResult.StreamFail;
}
if (!Authenticate())
{
return InitializeResult.AuthenticateFail;
}
return InitializeResult.Success;
}
Here are some common problems you might run in to:
Server
Windows Firewall.
Here is a tutorial on how to allow your program through the firewall: http://windows.microsoft.com/en-US/windows7/Allow-a-program-to-communicate-through-Windows-Firewall
Port forwarding on the router.
http://portforward.com/ is a good resource, I believe you can select your router and it will give you steps for that. You will need to select the LAN IP address of the machine hosting the server, and the port you are attempting to connect on (in your case, 3000) .
Client
For the client, it should be able to connect fine, but you will have to specify the external IP address, and not the LAN IP address of the server. You can find it by going to http://whatismyip.com on the computer hosting the server.
--
Msn and similar chat programs use a straightforward client/server approach like this - very easy to accomplish. A very interesting chat program to look into though, is Skype. It uses a P2P system, not a Client-Server. It accomplishes it using Nat Hole Punching, if you're interested (as a student) I'd suggest looking into it to expand your knowledge of networking.
If this:
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
is exactly what's in your client code, that's probably where the issue lies, even if firewalls are set up right. You'll have to modify the client program to take an IP as a configuration setting, command line parameter, text box, etc. and then use that instead.
I would first test this with two computers on the same LAN, if possible, using the address of the box running the server. If you can't connect, ensure the IP is right (by running ipconfig on the machine running the server), and seeing if Windows Firewall is on on the server. If it is, you'll either have to allow your program, or just open port 3000.
Once that's confirmed working, from another network entirely, with the external IP of the network the server is on. You can get this by going to http://icanhazip.com/ or similar (I prefer this over whatismyip, no ads, it only serves back the IP address), and ensuring port 3000 on the firewall/router is set to be forwarded to the internal IP of the server box.
You need to bind to the external NIC. Chances are you bind to the Loopback adaptor
If you provide some code, we could, maybe, diagnose (or even fix) things
You need to replace 127.0.0.1 with your actual IP address in:
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000);
To get your IP you can Google "what is my ip" or go to www.whatismyip.com/ from the server.
I also recommend installing Wireshark on both your client and server. I use this tool often when debugging sockets and other network issues.
). I got into a bit of issues today with the TcpListener. Things are strange. Initially, I used the new TcpListener(port) constructor, but that has been marked as obsolete. So I dropped it and used this instead:
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, ServerPort);
TcpListener tcpServer = new TcpListener(ipLocalEndPoint);
_TCPClient = tcpServer.AcceptTcpClient();
GotClient();
I do that in a thread, of course, so that it doesn't lock the application. Now, what happens there, is that even though the ipAddress is correct, the server NEVER accepts ANY incoming connection.
HOWEVER, changing to new IPEndPoint(IPAddress.Any, ServerPort) seems to do the trick! Which is silly in 2 ways:
2 hours ago, IPAddress.Any returned 192.168.1.102 which is my correct local IP. This is the same IP which was in ipAddress! But with ipAddress it didn't work, while with IPAddress.Any it worked (that is, it successfully accepted connections from my client).
Right now: IPAddress.Any returns 0.0.0.0 (!?) while the ipAddress variable continues to be assigned my correct IP (192.168.1.102). The result? My client still cannot connect if using ipAddres, but connects when using IPAddress.Any, even though it is 0.0.0.0.
I'm totally puzzled by this... Any thoughts?
I currently have this in Form_HandleCreated but it was acting weird when I had it in the Form's constructor as well.
LATER EDIT: I think I'm wrong about IPAddress.Any returning 192.168.1.102. I probably printed out something else, as many of you have indicated 0.0.0.0 is what .Any should return. Sorry ::- D.
IPAddress.Any should return 0.0.0.0, that is normal. Are you actually sure it returned something else initially?
As for why you couldn't connect before, if you were listening on 192.168.1.102, and tried to connect to 127.0.0.1 (localhost), then it wouldn't work. You need to listen on 127.0.0.1 if you want to connect to that IP address.
Essentially, you must listen on the IP address you are attempting to connect to. Listening on 0.0.0.0 means, "listen on all available IP addresses". Remember, 127.0.0.1 (localhost) is not a synonym for "my local network IP".
0.0.0.0 is blank IP address meaning "I don't care which IP you would use" - i.e. it means you want to listen on all interfaces. It is documented it should return it.
To later edit: First of all dont call [0]. Your hostname may not have any record associated. Even if it do you a) don't know the order b) you don't know where they are (i.e. it may be only 127.0.0.1 i.e. loopback).
On mono such code work (checked with netstat etc.):
using System;
using System.Net;
using System.Net.Sockets;
public class test {
public static void Main(String[] args) {
IPAddress ipAddress = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, 8888);
TcpListener tcpServer = new TcpListener(ipLocalEndPoint);
tcpServer.Start(); // Without this line there is exception
var _TCPClient = tcpServer.AcceptTcpClient();
}
}
I'm guessing it always returned 0.0.0.0...
In c# I am using the UdpClient.Receive function:
public void StartUdpListener(Object state)
{
try
{
udpServer = new UdpClient(new IPEndPoint(IPAddress.Broadcast, 1234));
}
catch (SocketException ex)
{
MessageBox.Show(ex.ErrorCode.ToString());
}
IPEndPoint remoteEndPoint = null;
receivedNotification=udpServer.Receive(ref remoteEndPoint);
...
However I am getting a socket exception saying that the address is not available with error code 10049
What do I do to negate this exception?
Here's the jist of some code I am currently using in a production app that works (we've got a bit extra in there to handle the case where the client are server apps are running on a standalone installation). It's job is to receive udp notifications that messages are ready for processing. As mentioned by Adam Alexander your only problem is that you need to use IPAddress.Any, instead of IPAddress.Broadcast. You would only use IPAddress.Broadcast when you wanted to Send a broadcast UDP packet.
Set up the udp client
this.broadcastAddress = new IPEndPoint(IPAddress.Any, 1234);
this.udpClient = new UdpClient();
this.udpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
this.udpClient.ExclusiveAddressUse = false; // only if you want to send/receive on same machine.
And to trigger the start of an async receive using a callback.
this.udpClient.Client.Bind(this.broadcastAddress);
this.udpClient.BeginReceive(new AsyncCallback(this.ReceiveCallback), null);
Hopefully this helps, you should be able to adapt it to working synchronously without too much issue. Very similar to what you are doing. If you're still getting the error after this then something else must be using the port that you are trying to listen on.
So, to clarify.
IPAddress.Any = Used to receive. I want to listen for a packet arriving on any IP Address.
IPAddress.Broadcast = Used to send. I want to send a packet to anyone who is listening.
for your purposes I believe you will want to use IPAddress.Any instead of IPAddress.Broadcast. Hope this helps!
That error means the protocol cant bind to the selected IP/port combination.
I havent used UDP broadcast in ages, but I do recall you need to use different IP ranges.
There's nothing wrong with the way you have configured your UdpClient. Have you tried a different port number? Perhaps 1234 is already in use on your system by a different app.