[personal profile] 3mer
На заметку.
Если вы когдалибо интенсивно пользовались Internet Sockets под Windows.
То вам конечно хорошо знакома функция int select(nfds,readfds, writefds, exceptfds, timeout).
Где параматр nfds документация Microsoft описывает как nfds - Ignored. The nfds parameter is included only for compatibility with Berkeley sockets.

И когда пришлось написать сокет код под линуксом, я почемуто решил что nfds это просто кол-во сокет дескриптеров в fd_set.

Но вот оказалось что нет, куча времени потрачено на поиск ответа почему не работает, то что по определению не может поломаться.
Пока наконец не увидел в сотнях строк документации важное:

The parameter numfds should be set to the values of the highest file descriptor plus one. Beej

Документация рулит. Немогу понять только почему описание параметров не вынесли в отдельное место, а прятали по всему
тексту.

Ниже просто на память.


How do I change or shorten the timeout on a call to connect()?

Instead of giving you exactly the same answer that W. Richard Stevens would give you, I'll just refer you to lib/connect_nonb.c" in the UNIX Network Programming source code.

The gist of it is that you make a socket descriptor with socket(), set it to non-blocking, call connect(), and if all goes well connect() will return -1 immediately and errno will be set to EINPROGRESS. Then you call select() with whatever timeout you want, passing the socket descriptor in both the read and write sets. If it doesn't timeout, it means the connect() call completed. At this point, you'll have to use getsockopt() with the SO_ERROR option to get the return value from the connect() call, which should be zero if there was no error.

Finally, you'll probably want to set the socket back to be blocking again before you start transferring data over it.

Notice that this has the added benefit of allowing your program to do something else while it's connecting, too. You could, for example, set the timeout to something low, like 500 ms, and update an indicator onscreen each timeout, then call select() again. When you've called select() and timed-out, say, 20 times, you'll know it's time to give up on the connection.

Like I said, check out Stevens' source for a perfectly excellent example.

Profile

3mer

June 2016

S M T W T F S
   1234
5678 91011
12131415161718
19202122232425
2627282930  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 10th, 2026 02:37 pm
Powered by Dreamwidth Studios