Link to home
Start Free TrialLog in
Avatar of manganzon
manganzonFlag for Peru

asked on

sending record through sockets

Hi, I'm trying to send a record through sockets.

I have a record TLogin:

type
  TStatus=(online,cacona,offline,busy,passedout);

  PLogin=^TLogin;
  TLogin=record
    Login:string;
    Pass:string;
    Status:TStatus;
    Display:TPicture;
  end;

and I want to Send a var of this record using SendBuf but when the server recive the buffer...

First I tried to do this:

Client:
  var log:TLogin;
  log.Login:='jojo'
  Client.Socket.SendBuff(log, SIzeof(log));

Server

   var Buffer:TLogin;
    while socket.ReceiveLength>0 do begin
     Socket.ReceiveBuf(Buffer, Sizeof(Buffer));
    end;

but it didnt work, then I found a code like this:

   procedure SendDataStruct(Socket: TCustomWinSocket);
    var
        joy: TLogin;
       
    begin
        Client.socket.SendBuf(@joy, sizeof(joy));
    end;

Server:
  var Log:PLogin;
        Socket.ReceiveBuf(Log^, sizeof(TLogin));
    end;

but I got an error at this line: Client.socket.SendBuf(@joy, sizeof(joy)); it says that Im passing a constant object as a parameter.

Anyone know how to solve this problem?
Avatar of TheRealLoki
TheRealLoki
Flag of New Zealand image

try changing it to
procedure SendDataStruct(Socket: TCustomWinSocket);
    var
        joy: TLogin;
        joyp: PLogin;
    begin
        ...
        joyp := @joy;
        Client.socket.SendBuf(joyp^, sizeof(joy));
    end;

you cannot sent this record since there is an object in there (TPicture).
You cannot just sent an object.
Avatar of manganzon

ASKER

I've tried both solution, but still have errors. When the server try to use the buffer I got a Read Memory error.

server:

procedure TfrmServer.ServerMainClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  log:PLogin;
  vildoso:TLogin;
begin
  while socket.ReceiveLength>0 do begin
    Socket.ReceiveBuf(Log^, sizeof(TLogin));
  end;
    Showmessage(Log^.Login);//Error here
    vildoso:=log^;//Error here
    Showmessage(vildoso.Login);//Error Here
end;
ASKER CERTIFIED SOLUTION
Avatar of TheRealLoki
TheRealLoki
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
btw, there are many ways to do this, this is just one method.
excuse typos, etc
Loki