Fog Creek Software
Discussion Board

Winsock server with a dynamic IP

In a client/server application based on winsock, if the clients are to access the server over the Internet and the server has a dynamic IP address and not a static one, how must the VB 6 application code detect the server's IP address at the time of requesting for a connection?

Sathyaish Chakravarthy
Thursday, September 11, 2003


Thursday, September 11, 2003

You'll have to use a directory service of some kind. Dynamic DNS services, such as DynDNS and others, are an option. You could homebrew some kind of directory service, but DNS is nice because there's a lot of infastructure in place already.

Thursday, September 11, 2003

If you are using the VB Winsock control, use the server's computer name in the control's RemoteHost property and it will resolve it to the IP using DNS.  If you're using the API, call the gethostbyname function to get a long integer containing the IP address. 

If you're new to Winsock programming with VB, look into or get a copy of 'Network Programming for Microsoft Windows'

Thursday, September 11, 2003

Not wanting to point out the obvious, but right back to the early days of IP networks it's been traditional to a client to connect to a server via a "well known port". In the beginning this meant that you put an entry into /etc/services, a file that a client application could search to find the host name, port (socket) id and protocol (TCP/UDP) for the named service. Note that it is the host name that was entered and not its IP address. This allowed the IP address to change, for example if this was allocated using DHCP, so long as it could be correctly resolved at run time through some form of DNS query.

If you don't know the host upon which the service is running then you've got a problem. If you don't know either the host or the port then you've an even bigger problem. On a LAN I've successfully addressed this issue using UDP. You need to implement the server so that it listens for both UDP and TCP packets. The client starts by broadcasting a UDP packet to all hosts requesting access to the server, and the server replies with its host name and the port TCP its listening on. The client then connects to this port.

To make the connection mechanism robust (UDP packets can get dropped without compunction) you'll need a bit richer protocol than this, but any good book on programming with sockets will give you a better explanation than I can in a short reply. In any event it won't work across sub-net boundaries as UDP packets are inevitably dropped.

David Roper
Thursday, September 11, 2003

*  Recent Topics

*  Fog Creek Home