Board index » delphi » Sending and receiving a packed record over a Socket

Sending and receiving a packed record over a Socket

Hello Group
I trying to figure out what I'm doing wrong. Here. I'm using SendPage()
to send
a message over socket. I'm not sure fow to tell the length of var Page
or thePage.
Please send me some feed back.
Here is a snippet of code.
Thank you
Don Biederman
g...@ripco.com

Unit Common;
interface
uses Windows, ScktComp, Classes, SysUtils;

const ServerPort       = 49999;
      MSG_SEND_PAGE    = $00010001;
      MSG_SEND_RESULTS = $00010002;
      MSG_REQ_LOG_FILE = $00020001;
      MSG_LOG_FOLLOWS  = $00020002;
      MSG_PURGE_LOG    = $00020003;
      MSG_ACK_PURGE    = $00020004;
Type
  TMsgHeader = packed record
    OpCode: DWORD;
    PayLoadLen: DWORD;
end;

Type
  TPageMsg = packed record
    PagerId: String; //Pager Number
    PMessage: String;//Page Message
    HostName: String;
    Done: String; //No = multi pages
end;

procedure SendPage(Sock: TCustomWinSocket; Code: DWORD; thePage:
TPageMsg);
var
  S: TMemoryStream;
  Header: TMsgHeader;
begin
  //setup header
  with Header do
  begin
    OpCode := Code;
    PayLoadLen := SizeOf(thePage);
  end;
  S := TMemoryStream.Create;
  S.Write(Header, SizeOf(Header));
  // in case of message without paload...
  if Header.PayLoadLen > 0 Then
     S.Write(thePage,SizeOf(thePage));
  S.Position := 0;
  Sock.SendStream(S);
end;

//from Client application
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  Buf: String;
  MsgLen,
  LenReceived: Integer;
  Header: TMsgHeader;
  Page : TPageMsg;
begin
 //get approximate message length
  MsgLen := Socket.ReceiveLength;
  Log.Lines.Add('Socket.ReceiveLength" ' + IntToStr(MsgLen));

   //prepare a buffer and get message
  SetLength(Buf, MsgLen);
  LenReceived := Socket.ReceiveBuf(Buf[1], MsgLen);
  Log.Lines.Add('LenReceived: ' + IntToStr(LenReceived));

  //**always** adjust for actually received number of bytes
  Buf := Copy(Buf, 1, LenReceived);
  Log.Lines.Add('Buf: ' + Buf);

  if Length(Buf) >= SizeOf(Header) THen
    begin
     //extract header
      move(Buf[1], Header, SizeOf(Header));
     //delete header from message
      Delete(Buf, 1, SizeOf(Header));
     //Test opcode
      case Header.OpCode of
        MSG_SEND_PAGE:
                     begin
                         Log.Lines.Add('MSG_SEND_PAGE');
                         if Length(Buf) >= SizeOf(Page) Then
                          begin
                              Log.Lines.Add('SizeOf(Page): ' +
IntToStr(SizeOf(Page)));
                             //extract Page
                             move(Buf[1], Page, SizeOf(Page));
                             Delete(Buf,1, SizeOf(Page));
                             Log.Lines.Add('Pager Id: ' + Page.PagerId);

                             Log.Lines.Add('Pager Message: ' +
Page.PMessage);
                             Log.Lines.Add('Client Host Name: ' +
Page.HostName);
                          end;
                       end
      else
        Log.Lines.Add('ILLEGAL_CODE');
      end;
    end
    else
      begin
        Log.Lines.Add('Message corrupt');
        Socket.Close;
      end;
end;

 

Re:Sending and receiving a packed record over a Socket


On 13 Jul 2002, Don & Traci <g...@ripco.com> wrote in news:3D3027D9.1F79CB15
@ripco.com:

Quote
> Hello Group
> I trying to figure out what I'm doing wrong. Here. I'm using SendPage()
> to send
> a message over socket. I'm not sure fow to tell the length of var Page
> or thePage.
> Please send me some feed back.
> Here is a snippet of code.
> Thank you
> Don Biederman
> g...@ripco.com

Your problem is that your records contains dynamic strings and these are
actually only pointers. Change that to use fixed-length strings (or char
array) or use writeln/readln to transfert this data type (or better yet, use
streams).

Good luck,
Stephane

Other Threads