quite simple network question

Bakul Shah bakul at bitblocks.com
Fri Jul 5 06:39:59 UTC 2019


On Fri, 05 Jul 2019 08:28:20 +0200 Wojciech Puchar <wojtek at puchar.net> wrote:
> i wrote a program with client and server part - running under FreeBSD.
>
> Client program use socket, then connect to establish TCP connection to =
>
> server.
>
> then it uses select every now and then to check if anything came from =
>
> server, when select returns nonzero recv is executed.
>
> If recv returns negative or zero - connection error is assumed, close is =
>
> called and disconnect reported to user.
>
>
> This works almost properly:
>
> a) reception works fine
> b) when server disconnects it's detected and reported.
>
>
> But if network fails (for simulating i simply turn ethernet interface off)
> it doesn't see it for a long time.
>
> so between socket and connect i added
>
>    a=3D1;
>    setsockopt(serverconnfd,SOL_SOCKET,SO_KEEPALIVE,&a,sizeof(a));
>    a=3D5; //KEEPALIVE co 5 sekund
>    setsockopt(serverconnfd,SOL_SOCKET,TCP_KEEPIDLE,&a,sizeof(a));
>    setsockopt(serverconnfd,SOL_SOCKET,TCP_KEEPINTVL,&a,sizeof(a));
>    a=3D2; //2 pr=F3by nim uzna =BFe roz=B3=B1czony
>    setsockopt(serverconnfd,SOL_SOCKET,TCP_KEEPCNT,&a,sizeof(a));
>
> and i assumed it will keepalive every 5 seconds and after 2 failures =
>
> behave as disconnect.
>
> But even after minute of waiting i don't get disconnect.
>
> What i am doing wrong?

I believe you have to set the exceptfds argument to select() properly.


More information about the freebsd-hackers mailing list