what is the return value of sockect close() function

policeman0077
Posts: 11
Joined: Mon Jul 16, 2018 8:52 pm

what is the return value of sockect close() function

Postby policeman0077 » Sat Jul 08, 2023 5:06 pm

I am new to socket programming.

what is the return value of sockect close() function. I could not find in https://docs.espressif.com/projects/esp ... light=lwip.

I always get -1 as return of close(). I have SO_REUSEADDR enabled. Is it safe to reconnect use the same sock after I call close()? Would lwip reuse the sock I just close().

Thank you for any input.

here is the code i wrote:

Code: Select all

int tcp_socket_close(asl_socket_t *s)
{
    int ret=0;
    ret = shutdown(s->socketfd,SHUT_WR);//close write, send fin
    if(ret<0)
    {
        ESP_LOGE(TAG, "tcp_socket_close:fail to SHUT_WR");
    }
    ret = shutdown(s->socketfd,SHUT_RD);//close read
    if(ret<0)
    {
        ESP_LOGE(TAG, "tcp_socket_close:fail to SHUT_RD");
        //return ret;
    }
    
    ret = close(s->socketfd);//destroy socket
    ESP_LOGI(TAG, "tcp_socket_close: close() return:%d",ret);
    return ret;
}
I used to check return of socket close() but is always return -1

Code: Select all

int tcp_socket_reconnect(asl_socket_t *s)
{
    int ret=0;
    ret=tcp_socket_close(s);
    // if(ret<0)
    // {
    //     ESP_LOGI(TAG, "tcp_socket_reconnect:fail to close socket");
    //     //esp_restart();//fail to close the socket restart
    //     return ret;
    // }
    ret=tcp_socket_connect();
    return ret;
}

ESP_Sprite
Posts: 9616
Joined: Thu Nov 26, 2015 4:08 am

Re: what is the return value of sockect close() function

Postby ESP_Sprite » Sun Jul 09, 2023 1:33 am

Close is a posix function; if it returns -1, there has been an error. I think you can do this to get the specifics:

Code: Select all

#include <stdio.h>

[...]

if (close(..)==-1) {
	perror("close");
}

policeman0077
Posts: 11
Joined: Mon Jul 16, 2018 8:52 pm

Re: what is the return value of sockect close() function

Postby policeman0077 » Tue Jan 30, 2024 5:53 pm

ESP_Sprite wrote:
Sun Jul 09, 2023 1:33 am
Close is a posix function; if it returns -1, there has been an error. I think you can do this to get the specifics:

Code: Select all

#include <stdio.h>

[...]

if (close(..)==-1) {
	perror("close");
}
Thank you. I will try your code. :D

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Eshwar487 and 78 guests