When I set up Socket option SO_REUSEADDR, I returned the error -1.
RET = setsockopt (g_sock_fd_client_model, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on));
Printf ("########%s () -%d, setsockopt reuseaddr ret:%d\n", __FUNCTION__, __LINE__, RET);
My business requires us to close (socket) and reconnect and bind the socket in some cases, but this is bind err
Return error: bind: Address already in use
This problem will be solved after setting the socket attribute SO_REUSEADDR. But now setting SO_REUSEADDR on esp32 fails. And our code can run normally on Ubuntu. But transplant to esp32 settings failed SO_REUSEADDR.
Do you have this problem when you are using it? Please help me to analyze it. Thank you.
Failure to set address to reuse attribute SO_REUSEADDR
-
- Posts: 30
- Joined: Mon Jun 11, 2018 5:52 am
-
- Posts: 30
- Joined: Mon Jun 11, 2018 5:52 am
Re: Failure to set address to reuse attribute SO_REUSEADDR
my complete code:
int g_sock_fd_client_model;
void tcp_reconnect()
{
struct sockaddr_in mine,dest;
int n = 0;
int ret = 0;
struct timeval receiving_timeout;
receiving_timeout.tv_sec = 20;
receiving_timeout.tv_usec = 0;
struct timeval conn_timeout;
conn_timeout.tv_sec = 5;
conn_timeout.tv_usec = 0;
int bDontLinger = 0;
int on = 1;
_reconnect:
if( (g_sock_fd_client_model = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("creat socket error,g_sock_fd_client_model:%d\n",g_sock_fd_client_model);
vTaskDelay(3000 / portTICK_PERIOD_MS);
}
ret = setsockopt(g_sock_fd_client_model, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
printf("########%s()-%d,setsockopt reuseaddr ret:%d\n",__FUNCTION__,__LINE__,ret);
ret = setsockopt(g_sock_fd_client_model, SOL_SOCKET, SO_RCVTIMEO, &receiving_timeout ,sizeof(receiving_timeout));
printf("########%s()-%d,setsockopt recv_timeout ret:%d\n",__FUNCTION__,__LINE__,ret);
//set local_port
bzero(&mine, sizeof(mine));
mine.sin_family = AF_INET;
mine.sin_port = htons(9999);
mine.sin_addr.s_addr = htonl(INADDR_ANY);
//set dest_port
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(1234);
dest.sin_addr.s_addr = inet_addr("192.168.55.2");
//bind local_port
ret = bind(g_sock_fd_client_model,(struct sockaddr*)&mine,sizeof(mine));
if(ret < 0)
{
perror("bind");
}
printf("########%s()-%d,bind ret:%d\n",__FUNCTION__,__LINE__,ret);
if( connect(g_sock_fd_client_model, (struct sockaddr *)&dest, sizeof(dest)) < 0)
{
printf("########%s()-%d,connect refused\n",__FUNCTION__,__LINE__);
close(g_sock_fd_client_model);
vTaskDelay(8000 / portTICK_PERIOD_MS);
goto _reconnect;
}
printf("########%s()-%d,connect end\n",__FUNCTION__,__LINE__);
return;
}
void tcp_client_to_uart()
{
char buf[1024] = {0};
int n = 0;
int ret = 0;
tcp_reconnect();
printf("start connect\n");
while(1)
{
bzero(buf, sizeof(buf));
if ((n = recv(g_sock_fd_client_model, buf, MAXDATASIZE, 0)) <= 0)
{
ret = close(g_sock_fd_client_model);
printf("########%s()-%d,n:%d,ret:%d\n",__FUNCTION__,__LINE__,n,ret);
//need reconnect
vTaskDelay(8000 / portTICK_PERIOD_MS);
tcp_reconnect();
}
else
{
uart_write_bytes(UART_NUM_2, buf, n);
}
}
return;
}
int g_sock_fd_client_model;
void tcp_reconnect()
{
struct sockaddr_in mine,dest;
int n = 0;
int ret = 0;
struct timeval receiving_timeout;
receiving_timeout.tv_sec = 20;
receiving_timeout.tv_usec = 0;
struct timeval conn_timeout;
conn_timeout.tv_sec = 5;
conn_timeout.tv_usec = 0;
int bDontLinger = 0;
int on = 1;
_reconnect:
if( (g_sock_fd_client_model = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("creat socket error,g_sock_fd_client_model:%d\n",g_sock_fd_client_model);
vTaskDelay(3000 / portTICK_PERIOD_MS);
}
ret = setsockopt(g_sock_fd_client_model, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
printf("########%s()-%d,setsockopt reuseaddr ret:%d\n",__FUNCTION__,__LINE__,ret);
ret = setsockopt(g_sock_fd_client_model, SOL_SOCKET, SO_RCVTIMEO, &receiving_timeout ,sizeof(receiving_timeout));
printf("########%s()-%d,setsockopt recv_timeout ret:%d\n",__FUNCTION__,__LINE__,ret);
//set local_port
bzero(&mine, sizeof(mine));
mine.sin_family = AF_INET;
mine.sin_port = htons(9999);
mine.sin_addr.s_addr = htonl(INADDR_ANY);
//set dest_port
bzero(&dest, sizeof(dest));
dest.sin_family = AF_INET;
dest.sin_port = htons(1234);
dest.sin_addr.s_addr = inet_addr("192.168.55.2");
//bind local_port
ret = bind(g_sock_fd_client_model,(struct sockaddr*)&mine,sizeof(mine));
if(ret < 0)
{
perror("bind");
}
printf("########%s()-%d,bind ret:%d\n",__FUNCTION__,__LINE__,ret);
if( connect(g_sock_fd_client_model, (struct sockaddr *)&dest, sizeof(dest)) < 0)
{
printf("########%s()-%d,connect refused\n",__FUNCTION__,__LINE__);
close(g_sock_fd_client_model);
vTaskDelay(8000 / portTICK_PERIOD_MS);
goto _reconnect;
}
printf("########%s()-%d,connect end\n",__FUNCTION__,__LINE__);
return;
}
void tcp_client_to_uart()
{
char buf[1024] = {0};
int n = 0;
int ret = 0;
tcp_reconnect();
printf("start connect\n");
while(1)
{
bzero(buf, sizeof(buf));
if ((n = recv(g_sock_fd_client_model, buf, MAXDATASIZE, 0)) <= 0)
{
ret = close(g_sock_fd_client_model);
printf("########%s()-%d,n:%d,ret:%d\n",__FUNCTION__,__LINE__,n,ret);
//need reconnect
vTaskDelay(8000 / portTICK_PERIOD_MS);
tcp_reconnect();
}
else
{
uart_write_bytes(UART_NUM_2, buf, n);
}
}
return;
}
Re: Failure to set address to reuse attribute SO_REUSEADDR
Please check whether you have SO_REUSE support enabled in menuconfig: https://docs.espressif.com/projects/esp ... P_SO_REUSE
Who is online
Users browsing this forum: No registered users and 103 guests