Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > pl.comp.programming > #31016 > unrolled thread
| Started by | fir <profesor.fir@gmail.com> |
|---|---|
| First post | 2017-12-05 08:32 -0800 |
| Last post | 2017-12-08 09:13 -0800 |
| Articles | 5 — 2 participants |
Back to article view | Back to pl.comp.programming
from blocking to nonblocking server fir <profesor.fir@gmail.com> - 2017-12-05 08:32 -0800
Re: from blocking to nonblocking server fir <profesor.fir@gmail.com> - 2017-12-05 14:46 -0800
Re: from blocking to nonblocking server Roman Tyczka <noemail@because.no> - 2017-12-08 09:30 +0100
Re: from blocking to nonblocking server fir <profesor.fir@gmail.com> - 2017-12-08 08:58 -0800
Re: from blocking to nonblocking server fir <profesor.fir@gmail.com> - 2017-12-08 09:13 -0800
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-12-05 08:32 -0800 |
| Subject | from blocking to nonblocking server |
| Message-ID | <69d6a065-253e-4edd-899f-ff8855f36157@googlegroups.com> |
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)
[toc] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-12-05 14:46 -0800 |
| Message-ID | <495ac9fc-2527-498f-9c86-44abe5943dbe@googlegroups.com> |
| In reply to | #31016 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Roman Tyczka <noemail@because.no> |
|---|---|
| Date | 2017-12-08 09:30 +0100 |
| Message-ID | <1soo5c8nnuthq.dlg@tyczka.com> |
| In reply to | #31017 |
On Tue, 5 Dec 2017 14:46:44 -0800 (PST), fir wrote: > W dniu wtorek, 5 grudnia 2017 17:32:33 UTC+1 użytkownik fir napisał: > 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) Czemu po prostu nie użyjesz jakieś open sourcowej biblioteki, która to załatwi od strzału? Zwłaszcza jeśli czujesz się cienki w socketach i tcp. -- pozdrawiam Roman Tyczka
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-12-08 08:58 -0800 |
| Message-ID | <43402e12-2b6e-4c62-afe0-52f4741c2c8a@googlegroups.com> |
| In reply to | #31038 |
W dniu piątek, 8 grudnia 2017 09:30:30 UTC+1 użytkownik Roman Tyczka napisał: > On Tue, 5 Dec 2017 14:46:44 -0800 (PST), fir wrote: > > > W dniu wtorek, 5 grudnia 2017 17:32:33 UTC+1 użytkownik fir napisał: > > 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) > > Czemu po prostu nie użyjesz jakieś open sourcowej biblioteki, która to > załatwi od strzału? Zwłaszcza jeśli czujesz się cienki w socketach i tcp. > nie lubie dependencji, nie czuje sie cienki w socketach tylko nie mam znimi doswiadczenia (tak jak wczesniej np z formatem PE), posiedzialem 3 dni i teraz czuje w zasadzie sockety mam mw tak samo opanowane jak uzywanie fopen/fclose/(fwrtie/fgetc etc) osiagnalem socketowe zen i jestem zadowolony (troche oczywisccie jeszze mozna by doczytac ale ogolnei dziala.. problem byl raczej z tymz e tutoriale sa zle napisne a same to api jest tez co nieco brzydkie)
[toc] | [prev] | [next] | [standalone]
| From | fir <profesor.fir@gmail.com> |
|---|---|
| Date | 2017-12-08 09:13 -0800 |
| Message-ID | <a50f6947-0054-427a-a133-530ea36a45be@googlegroups.com> |
| In reply to | #31041 |
W dniu piątek, 8 grudnia 2017 17:58:37 UTC+1 użytkownik fir napisał:
> W dniu piątek, 8 grudnia 2017 09:30:30 UTC+1 użytkownik Roman Tyczka napisał:
> > On Tue, 5 Dec 2017 14:46:44 -0800 (PST), fir wrote:
> >
> > > W dniu wtorek, 5 grudnia 2017 17:32:33 UTC+1 użytkownik fir napisał:
> > > 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)
> >
> > Czemu po prostu nie użyjesz jakieś open sourcowej biblioteki, która to
> > załatwi od strzału? Zwłaszcza jeśli czujesz się cienki w socketach i tcp.
> >
> nie lubie dependencji, nie czuje sie cienki w socketach tylko nie mam znimi doswiadczenia (tak jak wczesniej np z formatem PE),
> posiedzialem 3 dni i teraz czuje w zasadzie sockety mam
> mw tak samo opanowane jak uzywanie fopen/fclose/(fwrtie/fgetc etc)
>
> osiagnalem socketowe zen i jestem zadowolony
>
> (troche oczywisccie jeszze mozna by doczytac ale ogolnei dziala.. problem byl raczej z tymz e tutoriale sa zle napisne a same to api jest tez co nieco brzydkie)
nawiasdem mowiac ten kawalek
do 'nieblokujacego' (troche wyglada raczej jak blokujacy tylko z ustawialnym timeoutem)
znalezniony w necie
int WaitForServerConnectionUnblocked()
{
u_long no_block=1;
ioctlsocket(clientSock,FIONBIO,&no_block);
//connect to the server
// iResult = connect(client, (sockaddr*)&server, sizeof(server));
int retVal = connect(clientSock,(LPSOCKADDR)&serverInfo, sizeof(serverInfo));
if( retVal == SOCKET_ERROR)
{
int iError = WSAGetLastError();
//check if error was WSAEWOULDBLOCK, where we'll wait
if(iError == WSAEWOULDBLOCK)
{
/// cout << "Attempting to connect.\n";
fd_set Write, Err;
TIMEVAL Timeout;
FD_ZERO(&Write);
FD_ZERO(&Err);
FD_SET(clientSock, &Write);
FD_SET(clientSock, &Err);
Timeout.tv_sec = 0;
Timeout.tv_usec = 30*1000;
int iResult = select(0, NULL, &Write, &Err, &Timeout);
if(iResult == 0)
{
// cout << "Connect Timeout (" << TimeoutSec << " Sec).\n";
return 0;
}
else
{
if(FD_ISSET(clientSock, &Write))
{
// cout << "Connected!\n";
return 1;
}
if(FD_ISSET(clientSock, &Err))
{
// cout << "Select error.\n";
return 0;
}
}
}
else
{
// cout << "Failed to connect to server.\n";
// cout << "Error: " << WSAGetLastError() << endl;
// WSACleanup();
return 0;
}
}
else
{
//connected without waiting (will never execute)
//cout << "Connected!\n";
return 1;
}
}
co dziwne lokalnie to dziala acz od czasu do czasu nie zawiazuje polaczenia - zdalnie nie chcialo dzilac
jeszcze tego nie testowalem ale byc moze ten timeout tutaj ustawiony (30 ms) jest za maly
to wogole chyba nie tak powinno wygladac, wydaje mi sie ze nalezy chyba zarzucic connect
(ktory ma byc nieblokujacy a pozniej sprawdzac czy przyszlo info ze 'zaszedl' (jest connected) - jesli tak to dzial to chyab powinienem to podzielic na dwie funkcje ale musze jeszcze troche doczytac by uzupelnic zen ;c (nie wszystko na raz, ogolneie jednak czuje sie z scketami jestem juz ok ;c)
[toc] | [prev] | [standalone]
Back to top | Article view | pl.comp.programming
csiph-web