We help IT Professionals succeed at work.

sending record through sockets

manganzon
manganzon asked
on
571 Views
Last Modified: 2010-08-05
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?
Comment
Watch Question

TheRealLokiSenior Developer
CERTIFIED EXPERT

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

MerijnBSr. Software Engineer
CERTIFIED EXPERT

Commented:
you cannot sent this record since there is an object in there (TPicture).
You cannot just sent an object.

Author

Commented:
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;
Senior Developer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
TheRealLokiSenior Developer
CERTIFIED EXPERT

Commented:
btw, there are many ways to do this, this is just one method.
excuse typos, etc
Loki
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.