1

Closed

How to force IPv4?

description

Hi there,

I've just created a very simple test project, basically just instantiating a TCPServer and calling .Start() on Port 2001. I noticed it's using IPv6 by default. Any chance I can use IPv4 instead?

Thanks!
Closed Apr 2, 2013 at 10:47 PM by EdwardNutting

comments

EdwardNutting wrote Dec 18, 2012 at 4:56 PM

Hmmm....Been a while since I looked at this project really but if I remember correctly, it simply uses the first available local host IP address which results in IPv6. If you take a look at the source code, the method that handles this issue is "Initialise". Specifically, this bit of code:

Dns.GetHostEntry(Dns.GetHostName()).AddressList.First()

This gets the address list for the local host and then just takes the first one. You could instead use something like (N.B. following is untested code so may need adaptation)

Dns.GetHostEntry(Dns.GetHostName()).AddressList.ToList().Where(x => x.AddressFamily == AddressFamily.InterNetwork/IPv4/).First()

or possibly better:

Dns.GetHostEntry(Dns.GetHostName()).AddressList.First().MapToIPv4()

The last of those solutions may not work (the MSDN documentation is a little confusing on what that method does - it looks like someone copied/pasted the wrong text onto the page!)

Anyway, hope one of these alternatives solves your problem!
Ed

EdwardNutting wrote Dec 18, 2012 at 4:59 PM

P.s. Source code file: ?\Servers\TCP\TCPServer.cs:Initialise(int Port):Line 5

Changing that file will change it for all TCP servers created using my library i.e. the combined UDP/TCP Server files do not replicate code, they just make use of the TCPServer class.

Ed

wrote Feb 13, 2013 at 8:35 PM

wrote Apr 2, 2013 at 10:47 PM

wrote May 16, 2013 at 4:16 AM