Socket debugging

Once you have a problem with some socket communication, you must debug it. This article can help you with this task.

Packet capture

Major debug tool for each network developer is packet capturing tool. It can capture all networks packets what goint to and from your network card. Captured snapshot can be analyzed later and see what happens on the wire.

Even you not understand captured packet flow, don't be a worry! Saved snapshot in the disk file can be analyzed on any computer by any people. Just capture snapshot, save it to file and send it to person, who can help you.

:!: Snapshot can contain any privacy data, passwords, etc. Send it to persons with yout trust only!

Recommended very good, freeware and opensource multiplatform packet capturing tool is WireShark.

Socket events

In some cases you not need to know what happens on the wires. You need to know what happen on the socket interface inside computer instead.

Each sockat based class in Synapse have special event for this purpose. It is called OnStatus. Each Synapse protocol imeplemtation have property Sock1). It is socket class used for handling socket communication, and this class have OnStatus event. Hook this event and log events to the file. Sample:

class procedure TSynaDebug.HookStatus(Sender: TObject; Reason: THookSocketReason; const Value: string);
var
  s: string;
begin
  case Reason of
    HR_ResolvingBegin:
      s := 'HR_ResolvingBegin';
    HR_ResolvingEnd:
      s := 'HR_ResolvingEnd';
    HR_SocketCreate:
      s := 'HR_SocketCreate';
    HR_SocketClose:
      s := 'HR_SocketClose';
    HR_Bind:
      s := 'HR_Bind';
    HR_Connect:
      s := 'HR_Connect';
    HR_CanRead:
      s := 'HR_CanRead';
    HR_CanWrite:
      s := 'HR_CanWrite';
    HR_Listen:
      s := 'HR_Listen';
    HR_Accept:
      s := 'HR_Accept';
    HR_ReadCount:
      s := 'HR_ReadCount';
    HR_WriteCount:
      s := 'HR_WriteCount';
    HR_Wait:
      s := 'HR_Wait';
    HR_Error:
      s := 'HR_Error';
  else
    s := '-unknown-';
  end;
  s := inttohex(integer(Sender), 8) + s + ': ' + value + CRLF;
  AppendToLog(s);
end;

Data monitoring

In some cases you wish to monitor what datas are written and readed to socket by application. Socket class have special event OnMonitor for this. Hook this event (similar to OnStatus) and log informations. Sample:

class procedure TSynaDebug.HookMonitor(Sender: TObject; Writing: Boolean; const Buffer: TMemory; Len: Integer);
var
  s, d: string;
begin
  setlength(s, len);
  move(Buffer^, pointer(s)^, len);
  if writing then
    d := '-> '
  else
    d := '<- ';
  s :=inttohex(integer(Sender), 8) + d + s + CRLF;
  AppendToLog(s);
end;

synadbg unit

For helping with synapse debug tasks I prepare simple unit for debugging on application level. Just include it to your project uses, and then you can made debug file (on same place as your EXE file, but with extension '.slog') by settings hooks to socket what you wish to debug.

Example showing setting of debug hooks on THTTPSend class:

procedure TForm1.Button4Click(Sender: TObject);
var
  d: THTTPSend;
begin
  d := THTTPSend.Create;
  try
    d.Sock.OnStatus := TSynaDebug.HookStatus;
    d.Sock.OnMonitor := TSynaDebug.HookMonitor;
//...continue with work...

You want this unit? Download it here!

1) Some protocols using more sockets, like FTP have Sock and DSock
public/howto/debugsocket.txt · Last modified: 2008/02/04 23:16 by geby
Driven by DokuWiki Recent changes RSS feed