@@ -259,9 +259,10 @@ bool Socket::isValid() {
259
259
* @param [in] isDatagram True if we are listening on a datagram. The default is false.
260
260
* @return Returns 0 on success.
261
261
*/
262
- int Socket::listen (uint16_t port, bool isDatagram) {
262
+ int Socket::listen (uint16_t port, bool isDatagram, bool reuseAddress ) {
263
263
ESP_LOGD (LOG_TAG, " >> listen: port: %d, isDatagram: %d" , port, isDatagram);
264
264
createSocket (isDatagram);
265
+ setReuseAddress (reuseAddress);
265
266
int rc = bind (port, 0 );
266
267
if (rc != 0 ) {
267
268
ESP_LOGE (LOG_TAG, " << listen: Error in bind: %s" , strerror (errno));
@@ -285,14 +286,19 @@ bool Socket::operator <(const Socket& other) const {
285
286
return m_sock < other.m_sock ;
286
287
}
287
288
288
- int Socket::setSocketOption (int option, char * value, size_t len)
289
+
290
+ /* *
291
+ * @brief Set the socket option.
292
+ */
293
+ int Socket::setSocketOption (int option, void * value, size_t len)
289
294
{
290
- int res = setsockopt (m_sock, SOL_SOCKET, option, value, len);
295
+ int res = :: setsockopt (m_sock, SOL_SOCKET, option, value, len);
291
296
if (res < 0 ) {
292
297
ESP_LOGE (LOG_TAG, " %X : %d" , option, errno);
293
298
}
294
299
return res;
295
- }
300
+ } // setSocketOption
301
+
296
302
297
303
/* *
298
304
* @brief Socket timeout.
@@ -479,6 +485,18 @@ void Socket::sendTo(const uint8_t* data, size_t length, struct sockaddr* pAddr)
479
485
} // sendTo
480
486
481
487
488
+ /* *
489
+ * @brief Flag the socket address as re-usable.
490
+ * @param [in] value True to mark the address as re-usable, false otherwise.
491
+ */
492
+ void Socket::setReuseAddress (bool value) {
493
+ ESP_LOGD (LOG_TAG, " >> setReuseAddress: %d" , value);
494
+ int val = value?1 :0 ;
495
+ setSocketOption (SO_REUSEADDR, &val, sizeof (val));
496
+ ESP_LOGD (LOG_TAG, " << setReuseAddress" );
497
+ } // setReuseAddress
498
+
499
+
482
500
/* *
483
501
* @brief Flag the socket as using SSL
484
502
* @param [in] sslValue True if we wish to use SSL.
@@ -651,3 +669,5 @@ SocketInputRecordStreambuf::int_type SocketInputRecordStreambuf::underflow() {
651
669
SocketException::SocketException (int myErrno) {
652
670
m_errno = myErrno;
653
671
}
672
+
673
+
0 commit comments