if ListenerSocket.canread(100) then // <--- use 100 here instead of 1000
begin
//...
Correction... you do need to loop for that. In the original code this was a repeat until loop. Otherwise the if is false and the whole procedure just ends without checking for connections. So correct that back.
Just don't do the CloseSocket after enabling the timer.
(and your Timer1.enabled:=true; was again too soon, it needed to be AFTER the setting of ConnectionSocket.Socket inside the loop)
I know I said a long running event is bad but that's why you need to disable the button at the top of that event.
which button is it? the connect button?
Yes, the connect button on the server isn't really a connect button. It's a "Allow incoming connections" button.
Because it's not the server that's initiating the connection but the client is.
Do they all need to receive the same string?
at this point yes. i think if i want to send different string to different clients it will be too much for me right now
i just want to make clients to connect my server first. but how do you know which one is client1, client2 and client 3? say if i open 3 client programs, how can i know which one is which?
You could identify them from their IP address but maybe it's best to listen for the first string and set a client-name to that value. That means that the clients need to connect and send their client-name as first string.
And now that you've put a repeat loop in the button of the server and are sending a client-name from the clients you can also build in the "Connections: array of TTCPBlockSocket" I mentioned earlier.
Remove the variable ConnectionSocket and replace it with the Connection[xxx] variable.
Before the Connection[xxx].Socket := ListeningSocket.Accept you need to enlarge the array and create the TTCPBlockSocket.
And actually, because you want to use client-names you can use this:
type
ConnectionType = record
Sock: TCPBlockSocket;
ClientName: String;
end;
var
Connections: array of ConnectionType;
And on an incoming connection:
SetLength(Connections, Length(Connections) + 1);
Connections[Length(Connections)].Sock := TTCPBlockSocket.Create;
Connections[Length(Connections)].Sock.Socket := ListenerSocket.Accept;
Timer1.enabled:=true; // <-- here we can start listening to strings
Then in the timer you need to check if ever Connections record is you CanRead(100) from it.
If you can you read the string from it and you check if clientname is set.
If clientname isn't already set, then set the name in the Connections record
If the clientname was set you can print the message to the memo.
(I hope you understand this a bit
)