Tutorial :TServerSocket: Confusion with Socket Objects



Question:

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?


Solution:1

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.


Solution:2

Typically, your server socket is not going to be broadcasting its outgoing messages to all connected clients. Instead, it will be choosing one specific connected client to send the response. Think of all those connections from different clients as unique. Sure, they may have some common settings, but they are unique connections. (In database-speak, the primary key for a connection is a combination of all of the server IP, server port, client IP, client port)

I've not used TServerSocket, but the IPWorks library makes this explicit by using a connection ID that is specified both on the receiving side and the sending side. This way you know that the data you are reading/writing will be using a specific connection and the data is from/to the expected client.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »