int EthernetClient::connect(IPAddress ip, uint16_t port) { if (sockindex < MAX_SOCK_NUM) { if (Ethernet.socketStatus(sockindex) != SnSR::CLOSED) { Ethernet.socketDisconnect(sockindex); // TODO: should we call stop()? } sockindex = MAX_SOCK_NUM; } #if defined(ESP8266) || defined(ESP32) if (ip == IPAddress((uint32_t)0) || ip == IPAddress(0xFFFFFFFFul)) return 0; #else if (ip == IPAddress(0ul) || ip == IPAddress(0xFFFFFFFFul)) return 0; #endif sockindex = Ethernet.socketBegin(SnMR::TCP, 0); if (sockindex >= MAX_SOCK_NUM) return 0; Ethernet.socketConnect(sockindex, rawIPAddress(ip), port); uint32_t start = millis(); while (1) { uint8_t stat = Ethernet.socketStatus(sockindex); if (stat == SnSR::ESTABLISHED) return 1; if (stat == SnSR::CLOSE_WAIT) return 1; if (stat == SnSR::CLOSED) return 0; if (millis() - start > _timeout) break; delay(1); } Ethernet.socketClose(sockindex); sockindex = MAX_SOCK_NUM; return 0; }
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; }
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 = socketStatus(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 = 49152; //Use IANA recommended ephemeral port range 49152-65535 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; }
int EthernetClient::connect(IPAddress ip, uint16_t port) { struct SWS_sockaddr_in pin; int yes = 1, no = 0; uint8_t *ipchar; SWS_u_long iplong, lArg = 1; if (sws == NULL) return 0; if (sws->_sock != SWS_INVALID_SOCKET) stop(); ipchar = rawIPAddress(ip); memcpy(&iplong, ipchar, 4); bzero(&pin, sizeof(pin)); pin.sin_family = SWS_AF_INET; pin.sin_addr.SWS_s_addr = iplong; pin.sin_port = SWS_htons(port); sws->_sock = SWS_socket(SWS_AF_INET, SWS_SOCK_STREAM, 0); if (sws->_sock == SWS_INVALID_SOCKET) return 0; if (SWS_setsockopt(sws->_sock, SWS_SOL_SOCKET, SWS_SO_REUSEADDR, (const char*)&yes, sizeof(int)) < 0) { SWS_shutdown(sws->_sock, SWS_SD_BOTH); SWS_close(sws->_sock); sws->_sock = SWS_INVALID_SOCKET; return 0; } if (SWS_setsockopt(sws->_sock, SWS_SOL_SOCKET, SWS_SO_DONTLINGER, (const char*)&no, sizeof(int)) < 0) { SWS_shutdown(sws->_sock, SWS_SD_BOTH); SWS_close(sws->_sock); sws->_sock = SWS_INVALID_SOCKET; return 0; } if (SWS_ioctl(sws->_sock, SWS_FIONBIO, &lArg) < 0) { SWS_shutdown(sws->_sock, SWS_SD_BOTH); SWS_close(sws->_sock); sws->_sock = SWS_INVALID_SOCKET; return 0; } if (swssock_connect(sws->_sock, (const SWS_sockaddr*)&pin, sizeof(pin)) == -1) { SWS_shutdown(sws->_sock, SWS_SD_BOTH); SWS_close(sws->_sock); sws->_sock = SWS_INVALID_SOCKET; return 0; } _id = -1; pServer = NULL; return 1; }
int WiFlyClient::connect(IPAddress ip, uint16_t port){ /* */ _ip = rawIPAddress(ip); _port = port; _domain = NULL; return _connect(); }
int EthernetUDP::beginPacket(IPAddress ip, uint16_t port) { uint8_t *ipchar; SWS_u_long iplong; if (sws == NULL) return 0; if (sws->_sock == SWS_INVALID_SOCKET) return 0; ipchar = rawIPAddress(ip); memcpy(&iplong, ipchar, 4); bzero(&sws->txaddr, sizeof(sws->txaddr)); sws->txaddr.sin_family = SWS_AF_INET; sws->txaddr.sin_addr.SWS_s_addr = iplong; sws->txaddr.sin_port = SWS_htons(port); TxSize = 0; return 1; }
int EthernetUDP::beginPacket(IPAddress ip, uint16_t port) { _offset = 0; return startUDP(_sock, rawIPAddress(ip), port); }