How to close TInetServer without except?

classic Classic list List threaded Threaded
47 messages Options
123
Reply | Threaded
Open this post in threaded view
|

Re: How to close TInetServer without except?

zaher dirkey

On Sat, May 4, 2013 at 9:42 PM, Ludo Brands <[hidden email]> wrote:

However, in your code you do
if (c = 0) or (c = SOCKET_ERROR) then
  begin
    Error;

which is not correct. c=0 is the result of select returning after a
timeout. It is not an error.
The fact that
   if Socket.Select(10000, slRead) = erNone then
is blocked forever​ is not normal. It should at least return after 10
seconds.

​I am not testing in linux it is broked with me and i have not good internet to fix it.

They not use Select before acce​pt

and in http://msdn.microsoft.com/en-us/library/windows/desktop/ms740141%28v=vs.85%29.aspx

>The parameter readfds identifies the sockets that are to be checked for readability. If the socket is currently in the listen state, it will be marked as readable if >an incoming connection request has been received such that an accept is guaranteed to complete without blocking.

I still not sure in windows need Select, but maybe in Linux only, but i can't test it there.



--
I am using last revision of Lazarus, FPC 2.6 on Windows XP SP3

Best Regards
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to close TInetServer without except?

Ludo Brands
On 05/04/2013 09:59 PM, Zaher Dirkey wrote:

> now in this example
> http://msdn.microsoft.com/en-us/library/windows/desktop/ms737526%28v=vs.85%29.aspx
> They not use Select before acce​pt
>

If you are happy with accept blocking or if you use non-blocking sockets
you don't need select before accept.


> and in
> http://msdn.microsoft.com/en-us/library/windows/desktop/ms740141%28v=vs.85%29.aspx
>
>>The parameter /readfds/ identifies the sockets that are to be checked
> for readability. If the socket is currently in the *listen*
> <http://msdn.microsoft.com/en-us/library/windows/desktop/ms739168%28v=vs.85%29.aspx>
> state, it will be marked as readable if >an incoming connection request
> has been received such that an *accept*
> <http://msdn.microsoft.com/en-us/library/windows/desktop/ms737526%28v=vs.85%29.aspx>
> is guaranteed to complete *without blocking*.
>

That is exactly the purpose of select. The timeout guarantees that your
program stays alive and can do something else.

> I still not sure in windows need Select, but maybe in Linux only, but i
> can't test it there.
>

I repeat, you don't have to use select in Windows or Linux. If you
prefer blocking sockets you don't need select but accept/recv/send will
block. If you use non-blocking sockets you need to deal with EAGAIN or
EWOULDBLOCK (WSAEWOULDBLOCK on windows). There are also alternatives to
select: poll, epoll, libevent, etc., etc. on linux, WSAPoll on windows
Vista and later. For completeness, on windows you have also the
asynchronous overlapped IO mode which is used a lot in high performance
servers but rather complex to program.

Ludo
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to close TInetServer without except?

silvioprog
2013/5/5 Ludo Brands <[hidden email]>
On 05/04/2013 09:59 PM, Zaher Dirkey wrote:

> now in this example
> http://msdn.microsoft.com/en-us/library/windows/desktop/ms737526%28v=vs.85%29.aspx
> They not use Select before accept
>

If you are happy with accept blocking or if you use non-blocking sockets
you don't need select before accept.

> and in
> http://msdn.microsoft.com/en-us/library/windows/desktop/ms740141%28v=vs.85%29.aspx
>
>>The parameter /readfds/ identifies the sockets that are to be checked
> for readability. If the socket is currently in the *listen*
> <http://msdn.microsoft.com/en-us/library/windows/desktop/ms739168%28v=vs.85%29.aspx>
> state, it will be marked as readable if >an incoming connection request
> has been received such that an *accept*
> <http://msdn.microsoft.com/en-us/library/windows/desktop/ms737526%28v=vs.85%29.aspx>
> is guaranteed to complete *without blocking*.
>

That is exactly the purpose of select. The timeout guarantees that your
program stays alive and can do something else.

> I still not sure in windows need Select, but maybe in Linux only, but i
> can't test it there.
>

I repeat, you don't have to use select in Windows or Linux. If you
prefer blocking sockets you don't need select but accept/recv/send will
block. If you use non-blocking sockets you need to deal with EAGAIN or
EWOULDBLOCK (WSAEWOULDBLOCK on windows). There are also alternatives to
select: poll, epoll, libevent, etc., etc. on linux, WSAPoll on windows
Vista and later. For completeness, on windows you have also the
asynchronous overlapped IO mode which is used a lot in high performance
servers but rather complex to program.

Ludo

Ludo, I'm almost by completing the units in this link:

https://bitbucket.org/silvioprog/tcpipcomp (this repository is temporary)

The current code (I'm updating it daily) in these units is good? I'm open to suggestions, and we can change whatever it takes.

I want to send these units to the Free Pascal team and a package to the Lazarus team.

Sorry for my English please.

--
Silvio Clécio
My public projects - github.com/silvioprog

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to close TInetServer without except?

Ludo Brands
On 05/06/2013 07:17 AM, silvioprog wrote:

>
> Ludo, I'm almost by completing the units in this link:
>
> https://bitbucket.org/silvioprog/tcpipcomp (this repository is temporary)
>
> The current code (I'm updating it daily) in these units is good? I'm
> open to suggestions, and we can change whatever it takes.
>
> I want to send these units to the Free Pascal team and a package to the
> Lazarus team.
>
> Sorry for my English please.
>

In TTcpIpClientSocketThread.Execute you do a read followed by a select.
The logic is: first select then read when something is available.

The sleep(0) is not needed. Select suspends already the thread when waiting.

          VTimeVal.tv_sec := 0;
          VTimeVal.tv_usec := 100;
That is 100 microseconds which is very short. On top of that, when the
timeout expires, you test if FDisconnectClient is set and, if not set,
you do the read immediately again. The read is therefor again hanging
until something comes in.

Watch out for Synchronize(@DoReceive) in a recv loop. That is going to
drag down your performance considerably.

Ludo
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to close TInetServer without except?

zaher dirkey

On Mon, May 6, 2013 at 9:10 PM, Ludo Brands <[hidden email]> wrote:
Watch out for Synchronize(@DoReceive) in a recv loop. That is going to
drag down your performance considerably.

​Maybe you do need to call synh, leave it to the user to add it in his event, or make it as an option.

For that i hate using components as GUI components, no events in my classes.

--
I am using last revision of Lazarus, FPC 2.6 on Windows XP SP3

Best Regards
Zaher Dirkey

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to close TInetServer without except?

silvioprog
In reply to this post by Ludo Brands
2013/5/6 Ludo Brands <[hidden email]>
In TTcpIpClientSocketThread.Execute you do a read followed by a select.
The logic is: first select then read when something is available.

Sorry, but I don't understand well this part. :/ In the old code I not used select, and now I'm using it just to know that recv received data, but without lock the application.
But if there is how to improve, feel free buddy, I noticed that you know well about sockets. :)

I can add your SSH key on my Bitbucket account and you can commit directly in this code. Or, if your prefer, you can send me GIT patches. Feel free again.
 
The sleep(0) is not needed. Select suspends already the thread when waiting.

Done. I removed that.
 
          VTimeVal.tv_sec := 0;
          VTimeVal.tv_usec := 100;
That is 100 microseconds which is very short.

Is 500us good value for delay?
 
On top of that, when the timeout expires, you test if FDisconnectClient is set and, if not set,
you do the read immediately again. The read is therefor again hanging
until something comes in.

Yes. I needed it when I made a small HTTP server and I needed to disconnect the client in each request. There are a best way to do it? :/
 
Watch out for Synchronize(@DoReceive) in a recv loop. That is going to
drag down your performance considerably.

I don't understand well this part hehe... Hm... In some tests I did not see decreased performance, please could you show me an example decreasing the performance? I need to reproduce the problem, and debug the code to improve it.

Thank you very much for the explanations again. On the end of it maybe we have a nice multi-threaded component for work with TCP/IP. :)

-- 
Silvio Clécio
My public projects - github.com/silvioprog

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How to close TInetServer without except?

silvioprog
In reply to this post by zaher dirkey
2013/5/6 Zaher Dirkey <[hidden email]>
On Mon, May 6, 2013 at 9:10 PM, Ludo Brands <[hidden email]> wrote:
Watch out for Synchronize(@DoReceive) in a recv loop. That is going to
drag down your performance considerably.

Maybe you do need to call synh, leave it to the user to add it in his event, or make it as an option.

But it already implemented friend, eg:


  if VSynchronized then
    Synchronize(@DoClientReceive)
  else
    DoClientReceive;

 
For that i hate using components as GUI components, no events in my classes.

There you can see two examples: console (client/server); gui (client/server).

--
Silvio Clécio
My public projects - github.com/silvioprog

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
123