TCPClient how to have compatibility with old style entering ipaddress

Hello, first versions of TCPclient have just char address input. Now there is 4 byte possible IP address input.
All my program designed to use char input as address(domain or IP). Is there a way to pass IP as char into TCP client? Now it won’t connects with “192.168.1.100” in url.

Hi @ekbduffy

You should use IPAddress like this:

IPAddress myHost(192,168,1,100);
//pass myHost to TCPclient
1 Like

I know, but it’s complicated to pass ip address in one way and doman name in another.
Especially if you have class, wich pass this into another class, which pass in TCP client :smile:
Is there any way to avoid this?

Hi @ekbduffy

If you have your own class, you can follow the pattern used in TCPClient where you have two methods that take different type arguments. Here’s the example for TCPClient for connect in the header file:

	virtual int connect(IPAddress ip, uint16_t port);
	virtual int connect(const char *host, uint16_t port);

So there are two possible connect methods, one that takes and IPAddress and port and another one that takes a char* array (hostname) and a port.

Can your code follow this type of pattern?

1 Like

That’s the problem, i’ve started to write this methods to handle two different agruments, but there are issue - i need to hold address for reconnect in case of disconnection.
So i need every time to hold info about what method was used on connectiong, to know wich type of address is entered, and then use one of variables defined(one char and second is byte[])… Ok, i will take a look at counting dots in address and in point of quyuing connect on tcpclient split string if there are 3 dots… :slight_smile:
4th level domains go to hell :)))

Sure–how ever you want to manage it! Here’s a link to an example using Arduino String objects instead of char arrays but the idea is the same:

http://docs.spark.io/examples/#local-communication

1 Like

Thanks, I’ve already found nice way to do this:

char arr[] = "192.168.1.102"; 
byte a, b, c, d;
sscanf(arr, "%hu.%hu.%hu.%hu", &a, &b, &c, &d);

Now i’m looking to one line way to count occurancies of dot :smile:

1 Like

Finally:

void WebSocketClient::reconnect() {
  bool result = false;
  bool isconnected = false;
	int i, count;
	for (i=0, count=0; _hostname[i]; i++)
	  count += (_hostname[i] == '.');
	if (count == 3)
	{
		byte ip[4];
		sscanf(_hostname, "%hu.%hu.%hu.%hu", &ip[0], &ip[1], &ip[2], &ip[3]);
		isconnected = _client.connect(ip, _port);
	}
	else
	{
		isconnected = _client.connect(_hostname, _port);
	}
	if(isconnected)
	{
		sendHandshake(_hostname, _path, _protocol);
		result = readHandshake();
	}
 }
1 Like