Synapse - How to send data? lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- Edson · 2014-03-10 02:10 Hi. I finally, get my code working. I have implemented my version of the 'echo server' example  of Synapse for conecting two PC: Code: [Select]type   TProcesoTCP = class(TThread)   private     Sock:TTCPBlockSocket;     procedure ProcConex(Hsock: TSocket);   public     Constructor Create;     Destructor Destroy; override;     procedure Execute; override;   end; implementation Constructor TProcesoTCP.Create; begin   inherited create(false);   sock:=TTCPBlockSocket.create;   FreeOnTerminate:=true; end; Destructor TProcesoTCP.Destroy; begin   Sock.free; end; procedure TProcesoTCP.Execute; var   ClientSock:TSocket; begin   sock.CreateSocket;   sock.setLinger(true,10000);   sock.bind('0.0.0.0','80');   sock.listen;   while not terminated do begin     if sock.canread(1000) then begin         ClientSock:=sock.accept;         if sock.lastError=0 then ProcConex(ClientSock);     end;   end; end; procedure TProcesoTCP.ProcConex(Hsock:TSocket); var s: string;     sock2: TTCPBlockSocket; begin   sock2:=TTCPBlockSocket.create;   sock2.socket:=Hsock;   sock2.GetSins;   while not terminated do begin     s := sock2.RecvPacket(60000);     if sock2.lastError<>0 then break;     sock2.SendString(s);  //response the same     if sock2.lastError<>0 then break;   end;   sock2.Free; end; And it works, but I want to know if it's OK to create a new "Sock2" variable in ProcConex(). Other problem I can see, is that it just response when receiving data. How Do I can send data asynchronously? Is it possible? I have tested this: Code: [Select]procedure TProcesoTCP.SendData(const s: string); begin   sock.SendString(s); end; But it doesn't seem to work. ---------------------------------------------------------------------- Edson · 2014-03-11 15:50 Any suggestions? ---------------------------------------------------------------------- snorkel · 2014-03-11 18:25 First Synapse is synchronous, so you can't do async  like you can with ICS or Lnet. What you want to do is when a connection comes in spawn a thread and pass the socket from the accept in the threads constructor. Then in the thread you can simply use the single passed socket to read and write to and from the connected client. You can also keep a list of all connected sockets and have each thread have a protected write queue, when you need to send a message to all connected clients you can loop through the list of sockets and drop the message in each threads write queue, then when the thread is not reading you can pop a message out of the queue and write to the threads socket.  That way you can have a single socket per thread and it performs like a async system would. you could easily handle a thousand or so users on a run of the mill server, maybe a bit more. The disadvantage is you do get more context swaps. The other thing is to set the stack space of each thread to better manage memory, by default a thread gets a stack space of 1 or 2 mb, not what the default is exactly, but depending on what you are doing you may be able to set it much lower and that means more simultaneous connections. ---------------------------------------------------------------------- Edson · 2014-03-16 18:02 Thank for the response. Quote from: snorkel on March 11, 2014, 07:25:48 pmThen in the thread you can simply use the single passed socket to read and write to and from the connected client. I will try to modify my code. I don't need to manage with multiple connections. I just want to serve one connection (I used threads, just because of the example used it). The important thing for me, is to make the server can send a data to the client in any moment, no needing that the client send anything. If Synapse is synchronous, does it mean that this is not possible? Sorry, I don't understand how using a queue could help.