Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TServerSocket: Confusion with Socket Objects

So I'm having this application that verifies weather a user can log-in or not.

It consists of multiple Clients (up to 200) and a server that processes the login querys (containing Username, PW and IP). The Server checks weather the user exists and sends an answer back.

TLoginQuery is a Record.

procedure TLogin_Form.btnLoginClick(Sender: TObject);
 var LoginQuery1: TLoginQuery;
begin
 if not LoginSocket.Active then
  begin
   LoginSocket.Open;
  end;

//Paketchen schnüren.
 LoginQuery1.Name := ledtName.Text;
 LoginQuery1.Passwort := ledtPasswort.Text;
 LoginQuery1.IP := LoginSocket.Socket.LocalAddress;
//LoginQuery ín den Socket legen.
 LoginSocket.Socket.SendBuf(LoginQuery1, SizeOf(LoginQuery1));

end;

The Server currently reads:

procedure TServer_Form.ServerSocketClientRead(Sender: TObject;
Socket: TCustomWinSocket);
 var LoginQuery: TLoginQuery;
     uservalid: boolean;
begin
 uservalid := false;
 Socket.ReceiveBuf(LoginQuery, SizeOf(LoginQuery));
 if CheckIfUserValid(LoginQuery) then
  begin
   uservalid := true;
   ServerSocket.Socket.SendBuf(uservalid, SizeOf(uservalid));
  end;

end;

The question now is: Does the server (as it should generally be) create a different socket connection per client?

My assumption:

ClientA sends his login data and recieves the uservalid boolean (code above) from the server. As the uservalid boolean is written into the socket connection the following happens: Just before ClientA can get the uservalid boolean (as it should be) ClientB, who is already logged in, reads from the socket and gets (as it should NOT be) the uservalid boolean.

This could be intervented using one socket per client. Right?

like image 486
Acron Avatar asked May 27 '26 12:05

Acron


1 Answers

TServerSocket.OnClientRead's Socket: TCustomWinSocket parameter represents just that one connection between one of your clients and the server. Thus, when client Foo sends the login record, and TServer_Form.ServerSocketClientRead is called, just say

 if CheckIfUserValid(LoginQuery) then
  begin
   uservalid := true;
   Socket.SendBuf(uservalid, SizeOf(uservalid));
  end;

and you'll send the data to the right client.

like image 92
Frank Shearar Avatar answered May 30 '26 05:05

Frank Shearar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!