Sockets / fpAccept break signal lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- marcodor · 2015-03-26 16:08 Hello, Trying to write a socket server using Sockets unit. Everything it's OK till now, but some questions appears. Server initialize socket and are waiting for client connections. Later it create a new thread for each new socket connection to handle client. This is done in an infinite cycle repeat/until: Code: [Select]function TUSocketServer.Start: Boolean; const Sal: TSockLen = SizeOf(TInetSockAddr); var A, B: TInetSockAddr; begin   FLink := fpSocket(AF_INET, SOCK_STREAM, 0);   if FLink = S_FAIL then Exit(False);   A.sin_family := AF_INET;   A.sin_port := NToHs(FPort);   A.sin_addr.s_addr:= 0;   if fpBind(FLink, @A, Sal) = S_FAIL then Exit(False);   if fpListen(FLink, 1) = S_FAIL then Exit(False);   repeat     if fpAccept(FLink, @B, @Sal) = S_FAIL then begin Write('ACP'); Exit(False); end;     // <<< Handle new connection   until false; end; The question is: How to stop server waiting for new connections, terminate main thread, disconnect connected clients and free resources in a graceful way? For example on Windows / COM Port communication there is a possibility to signal "waiting" state using Events and Signals. I'm looking for something similar in socket handling that also works on Linux platform. Thanks in advice! -   Dorin ---------------------------------------------------------------------- marcodor · 2015-03-26 16:36 I found a way using KillThread, is this only option available? ---------------------------------------------------------------------- engkin · 2015-03-26 16:43 The thread loop should be: Code: [Select]  while not Terminated do ... TThread has both Terminated and Terminate. ---------------------------------------------------------------------- marcodor · 2015-03-26 16:48 engkin, fpAccept function wait unlimited time until some socket connection is established. Calling Thread.Terminate and check for Terminated won't help here, because "hang" is inside fpAccept function. ---------------------------------------------------------------------- engkin · 2015-03-26 17:40 This is just my assumption. Turning the socket into non-blocking *might* take it out of FpAccept and allow you to tear it down with fpshutdown and CloseSocket. ---------------------------------------------------------------------- marcodor · 2015-03-26 18:09 Engkin, Lol, i need blocking model. Controlled Query->Answer. Like in http/sql protocol. Non-blocking when you receive something while sending something and you don't know exactly what you receive is not a solution for me. Non- blocking is used in other cases. ---------------------------------------------------------------------- marcodor · 2015-03-26 18:13 I decided to put just "fpAccept" cycle in a separate thread and use later KillThread to stop it. Will report about success. By the way, calling KillThread while Thread executing a small critical section make it wait a bit of nanoseconds till thread exit that section? or kill it immediately in an barbarian cruel way? ---------------------------------------------------------------------- engkin · 2015-03-26 18:19 Barbarian way. ---------------------------------------------------------------------- marcodor · 2015-03-26 18:36 Ah, just found a solution from Remy Lebeau, Indy author, here Thanks, Engkin for quick answers! I'm new in FP world ---------------------------------------------------------------------- engkin · 2015-03-26 20:34 Thanks for sharing that solution, and welcome to FPC/Lazarus forum.