-
Data: 2017-12-05 23:46:44
Temat: Re: from blocking to nonblocking server
Od: fir <p...@g...com> szukaj wiadomości tego autora
[ pokaż wszystkie nagłówki ]W dniu wtorek, 5 grudnia 2017 17:32:33 UTC+1 użytkownik fir napisał:
> his is offtopic but maybe someone here will/would know
>
> i dont like to read a weeks on tcp and sockets as this is a lot and confusing
> (i prefer this time to spend on c/graphics etc)
>
> i got some old tcp-server (on winapi winsock2) code, that worked but it was
> not tested/inspected for being ok and was yet only working in blocking mode
>
> the code goes
>
> int SetupBlockingWinsockServer()
> {
>
> WSADATA wsaData;
> WSAStartup(MAKEWORD(2,2), &wsaData);
>
> /// Creating socket
>
> servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
>
> SOCKADDR_IN sin;
> sin.sin_family = PF_INET;
> sin.sin_port = htons(1111);
> sin.sin_addr.s_addr = INADDR_ANY;
>
> //bind
>
> int retVal = bind(servSock, (LPSOCKADDR)&sin, sizeof(sin));
>
> /// Trying to listen socket
>
> retVal = listen(servSock, 10);
>
> // Waiting for a client
>
> ServClientSock = accept(servSock, NULL, NULL); return
1;
>
>
> }
>
> (i skipped the few lines of retvalue checking for errors for clarity)
>
> my goal is just to turn it to non blocking without a lot of work but i would like
to know this is ok
>
> i checked experimentally via message boxes that it "blocks" on accept call
> (block may be euphemism a bit here as it hang real hard, when no client is run then
not only hourglass-pointer is shown, not only app gets fully unresponsible and non
redraws itself but even closing it form task manager is hard, its possible but takes
few seconds to kill)
>
> what i did :
>
> i added some line thet is supposed to set socked as nonblocked and move accept to
separate function which i call then periodically (from run frame event)
>
> int SetupBlockingWinsockServer()
> {
>
> WSADATA wsaData;
> WSAStartup(MAKEWORD(2,2), &wsaData);
>
> /// Creating socket
>
> servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
>
> SOCKADDR_IN sin;
> sin.sin_family = PF_INET;
> sin.sin_port = htons(1111);
> sin.sin_addr.s_addr = INADDR_ANY;
>
> //bind
>
> int retVal = bind(servSock, (LPSOCKADDR)&sin, sizeof(sin));
>
> /// Trying to listen socket
>
> retVal = listen(servSock, 10);
>
> // set no blocking
>
> u_long no_block=1;
> ioctlsocket(servSock,FIONBIO,&no_block);
>
> }
>
> int WaitForClientAccept()
> {
> /// Waiting for a client
>
> ServClientSock = accept(servSock, NULL, NULL);
>
> if(ServClientSock == INVALID_SOCKET) return 0;
>
>
> u_long no_block=1;
> ioctlsocket(ServClientSock,FIONBIO,&no_block);
> return 1;
>
> }
>
> this last one is as i said run periodically on server app frame and when it return
1 (it means it gett accepted connection?) i flag out both server code to not call it
more and
> client not to send more connections
>
> it seem to work but as this is my
> own slight experimentation only i would like to get pproval/confirmation
> that this is in fact ok
>
> is this ok or is there something wrong with it?
>
> (btw as a side questions, do i need those flaging out here, or if i connected i
could not bother for flaging out more sendings for connections and checks for
incomming connections? (i know it would be wastefull maybe to send it still 50 times
a second after connection was accepted but besides this waste would it work? (if that
would be work maybe sending it once a second would work as
> automatic connection reboval when something would get disconnected?)
>
> tnx for answer if someone know whats goin on in that sockets/winsock, i got not
much time for that
>
> (fir)
moge dodac ze to chyba dziala ok
- za to przelaczenie connecta w
kliencie na nie-blokujacy chyba nie da sie tak prosto zrobic a nie
moge nigdzie w google znalezc gotowego kodu jak to zrobic
(a czytac o tym kilka/nascie/dziesiat godzin nie mam jakos checi bo to straszne
smieci)
Następne wpisy z tego wątku
- 08.12.17 09:30 Roman Tyczka
- 08.12.17 17:58 fir
- 08.12.17 18:13 fir
Najnowsze wątki z tej grupy
- Nowa ustawa o ochronie praw autorskich - opis problemu i szkic ustawy
- Alg. kompresji LZW
- Popr. 14. Nauka i Praca Programisty C++ w III Rzeczy (pospolitej)
- Arch. Prog. Nieuprzywilejowanych w pełnej wer. na nowej s. WWW energokod.pl
- 7. Raport Totaliztyczny: Sprawa Qt Group wer. 424
- TCL - problem z escape ostatniego \ w nawiasach {}
- Nauka i Praca Programisty C++ w III Rzeczy (pospolitej)
- testy-wyd-sort - Podsumowanie
- Tworzenie Programów Nieuprzywilejowanych Opartych Na Wtyczkach
- Do czego nadaje się QDockWidget z bibl. Qt?
- Bibl. Qt jest sztucznie ograniczona - jest nieprzydatna do celów komercyjnych
- Co sciaga kretynow
- AEiC 2024 - Ada-Europe conference - Deadlines Approaching
- Jakie są dobre zasady programowania programów opartych na wtyczkach?
- sprawdzanie słów kluczowych dot. zła
Najnowsze wątki
- 2025-03-20 Grubość socketa AM4+procesor
- 2025-03-20 Środa Wielkopolska => Konsultant wewnętrzny SAP FI/CO <=
- 2025-03-20 Warszawa => Senior Programmer C <=
- 2025-03-20 Re: Dlaczego tak odstają od Tesli?
- 2025-03-20 Greenpeace została zobowiązana do zapłaty niemal 667 mln dolarów [USA,wyrok sądu]
- 2025-03-20 Re: Dlaczego tak odstają od Tesli?
- 2025-03-19 Brak ograniczeń dla chińskiego kapitału - wam nie do rządu, tylko na zmywak do chińskiej knajpy!!!
- 2025-03-19 Wietnam wykłada 500M$ i chce zbudować fabrykę za 50G$
- 2025-03-19 szal-Unia == federacja policyjna
- 2025-03-19 Polsza == państwo policyjne
- 2025-03-19 Grzegorz Płaczek o programie szczepień dzieci. ,,Stworzono eldorado dla firm farmaceutycznych"
- 2025-03-19 Wietnam wykłada 500M$ i chce zbudować fabrykę za 50G$
- 2025-03-19 Gemini
- 2025-03-19 Mokry sen Zenka :)
- 2025-03-19 Re: Dlaczego tak odstają od Tesli?