TThread in TList. Acces to TTCPBlockSocket methods lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- and_rivne · 2018-08-09 12:46 Hi All! Initial data: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  blcksock; //from synapse type   TClientThread = class(TThread)    public     Socket: TTCPBlockSocket;   end; var   Socket: TTCPBlockSocket;  T: TClientThread;  ThreadList: TList;   I launch a thread in which I wait for connection from web browsers. Add pointer thread in TList Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Socket.CreateSocket;Socket.Bind(Socket.LocalName,'57777'); repeat  if Socket.CanRead(100) then       begin          T:=TClientThread.Create(Socket.Accept);          ThreadList.Add(pointer(T));                            T.Start;       end;until Terminated; After several requests from the browser, I rewind the elements and I want to call SendString for the socket. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  cln : TClientThread; for i:=0 to ThreadList.Count-1 do  begin    cln := TCli entThread(ListenerClient.ThreadList.Items[i]).Socket.SendString('ok'); end; But the data is sent only for the element with index 0. For others I get the error SIGSEGV Have I correctly stored the sockets from the client in TList? ---------------------------------------------------------------------- Thaddy · 2018-08-09 13:13 Tlist is not threadsafe, although with care you can use it as you did. Use TThreadList from classes or TThreadlist from rtl-generics (generics.collections.pas) The latter can greatly simplify your code as well. TThreadlist; !! No  more casts... Also note specifically the last part of your code looks fishy to me: you are doing the sendstring from the context of the main thread, not from the threads. I suspect you need a redesign. ---------------------------------------------------------------------- and_rivne · 2018-08-09 17:59 Thanks for the answer. Yes, I'm trying to send data from another class, to a higher level. I do not understand why: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i cln := TCli entThread(ListenerClient.ThreadList.Items[0]).Socket.SendString('ok'); //Items[0]  working but Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i cln := TCli entThread(ListenerClient.ThreadList.Items[1]).Socket.SendString('ok'); // Items[1] does not work. And the data reaches the client (a few times), but there is an exception. ---------------------------------------------------------------------- sash · 2018-08-09 22:10 Quote from: and_rivne on August 09, 2018, 07:59:41 pmYes, I'm trying to send data from another class, to a higher level. Although threads are classes (objects) and can have their own data and methods, they are not intended to be containers. A typical usage of threads is like a rocket: you setup it, you launch it and wait (or not) until they finish. They should do a job autonomously. Of course, a two-way communication is possible (with known limitations), but definitely you should not touch (directly) thread's internal data in the middle of the work. ---------------------------------------------------------------------- and_rivne · 2018-08-10 07:11 What are the options to save threads, except for the TList ? I did not understand how to get an element from a TThreadlist, and do not delete it. ---------------------------------------------------------------------- taazz · 2018-08-10 08:18 just a quick note. 1) the code you posted makes no sense what so ever. 2) the code you posted is too short to analyze the execution flow. 3) if the tlist that holds a list of threads is to be accessed from multiple threads then it is better to use a TThreadedList instead. At list that will give you some protection against list corruption. other than that I need to see the execution method of TClientThread as well as the complete procedure of the loop you just posted before I can decide if the code you posted has any relevance to the problem you are having let alone pin point the problem it self. ---------------------------------------------------------------------- and_rivne · 2018-08-10 10:18 Quoteother than that I need to see the execution method of TClientThreadCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i while not Terminated  do   begin    if Socket.WaitingData > 0 then     begin      s:=Socket.RecvPacket(2000);      if Socket.LastError = 0 then        begin          ProcessingData(S);// we process the data before sending it to the server        end;      end;    sleep(10);   end; The stream expects data from the user and passes on to server. The problem is that I can not send the result from server to user Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i cln := TCli entThread(ListenerClient.ThreadList.Items[1]).Socket.SendString('ok'); ---------------------------------------------------------------------- sash · 2018-08-10 11:31 I'm very confused by your usage of terms client, server, thread, stream. It seems like your TClientThread is actually a Peer Thread on a server. In thast case you should call (a response from server to client (browser) )   Socket.SendString('ok'); after or inside   ProcessingData(S); as a part of your logical protocol. Anyway, it seems like you're trying to implement a usual webserver (with Synapse). There are Synapse server demo, FPC's TFPHttpServer. Why don't you use it? ---------------------------------------------------------------------- and_rivne · 2018-08-10 12:13 Yes you are right. This part of the code is similar to the operation of a web server. After the first request, the browser returns to the page. And this code is executed: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i cln := TCli entThread(ListenerClient.ThreadList.Items[0]).Socket.SendString('.... http body page....'); index=0 Then the browser sends requests for css and images in multiple threads simultaneously, which I save for return the result but here we get the error: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i cln := TCli entThread(ListenerClient.ThreadList.Items[1]).Socket.SendString('...cs s file code ...'); index=1 sash, thank you for understanding the logic. You most accurately understand the situation. The task: to intercept requests from the browser, process them and return result.