Go Down

Topic: The return value of a EthernetClient.connect() (Read 111 times) previous topic - next topic

malok


The official documentation says that the method the  EthernetClient.connect() returns an int (1,-1,-2,-3,-4). But in the source code EthernetClient.cpp we can see:
Code: [Select]

int EthernetClient::connect(IPAddress ip, uint16_t port) {
  if (_sock != MAX_SOCK_NUM)
    return 0;

  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    uint8_t s = W5100.readSnSR(i);
    if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
      _sock = i;
      break;
    }
  }

  if (_sock == MAX_SOCK_NUM)
    return 0;

  _srcport++;
  if (_srcport == 0) _srcport = 1024;
  socket(_sock, SnMR::TCP, _srcport, 0);

  if (!::connect(_sock, rawIPAddress(ip), port)) {
    _sock = MAX_SOCK_NUM;
    return 0;
  }

  while (status() != SnSR::ESTABLISHED) {
    delay(1);
    if (status() == SnSR::CLOSED) {
      _sock = MAX_SOCK_NUM;
      return 0;
    }
  }

  return 1;
}


Hence, this function can return only 0 or 1. Am I wrong or not?

I translated it to Russian language in this way.

pYro_65

#1
Feb 19, 2015, 12:33 pm Last Edit: Feb 19, 2015, 12:34 pm by pYro_65
Those return codes are for the dns server response on the second overload of connect().

Code: [Select]
int EthernetClient::connect(const char* host, uint16_t port) {
  // Look up the host first
  int ret = 0;
  DNSClient dns;
  IPAddress remote_addr;

  dns.begin(Ethernet.dnsServerIP());
  ret = dns.getHostByName(host, remote_addr);
  if (ret == 1) {
    return connect(remote_addr, port);
  } else {
    return ret;
  }
}


Not there? Update your IDE.

malok

#2
Feb 19, 2015, 06:56 pm Last Edit: Feb 19, 2015, 08:48 pm by malok
This function eventually calls a function, which I mentioned above:
return connect(remote_addr, port);
 Accordingly for versions client.connect(URL, port) - returns int(1,0,-1,-2,-3,-4), and for version  client.connect(ip, port) - returns int (0,1)

pYro_65

This function eventually calls a function, which I mentioned above:
return connect(remote_addr, port);
 Accordingly for versions client.connect(URL, port) - returns int(1,0,-1,-2,-3,-4), and for version  client.connect(ip, port) - returns int (0,1)
Correct. The function you mentioned is only called if the dns succeeds in the client.connect(URL, port) version.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy