views:

71

answers:

3
+1  Q: 

IPAddress.Any Fail

Hello everybody ::- ). 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:

  1. 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).

  2. 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.

+5  A: 

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".

Mark
Axonn
If you're trying to connect from the emulator to the host machine, I don't know what the IP address would look like... there might be some NAT in there, I couldn't say.
Mark
Also, it strikes me to ask if you've set up networking between the emulator and the host machine. I seem to remember there are various options in the emulator for how networking works. Also, verify your firewall settings, it may be blocked, depending on how you've got things set up.
Mark
As I said, the emulator successfully connects if I use 0.0.0.0. So no firewall issues. I'm gonna use a port sniffer to see exactly where / how things go.
Axonn
I just moved my application to my HTC Tytn2 and now it works. That one connects to 192.168.1.102 so probably this was the answer: the emulator goes through some other address.
Axonn
+1  A: 

I'm guessing it always returned 0.0.0.0...

Kendrick
I might have made a mistake on that. Perhaps printing to the console some other variable (like ipAddress). Sorry if I did so.
Axonn
@Axonn: I don't think that's something you have to appologise for here. I believe we've ALL done it at least once :-)
Kendrick
Axonn
+4  A: 

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();
    }
}
Maciej Piechotka
Hi and thanks for answering ::- ). So that's why that works... I get it. Hm, the issue remains... how to make it work on .1.102. I don't want to listen to all interfaces.
Axonn
I responded to "later edit". Possibly as works-for-me but that at least some answer.
Maciej Piechotka
Thank you Maciej ::- ). It may be related to the emulator - I'm connecting through it, not via a physical device. However, in the emulator, I try to connect to 192.168.1.102, so I don't understand why it doesn't actually do that. Might be some NAT stuff, as Mark already suggested. I have tried looking at what ports are being used but Wireshark doesn't appear to be working for some reason.I just moved my application to my HTC Tytn2 and now it works. That one connects to 192.168.1.102 so probably this was the answer: the emulator goes through some other address.
Axonn