Actually, I started to implement TWSocket in my code before I got your
e-mail, Angus. Pretty confused and stuck at the moment. Give this to my
literally *no knowledge* of using sockets. Below is as far as it gets. I
copy-paste from sample applications. Changed some code as to my
understanding.

In my scenario, "server connects to client".
I don't get to see 'HEY!' message in my client application (check below
code), but my server application claims connection is just fine.

Server side code:
WSocket is a private variable. 
Server is a webservice VCL application when I am debugging, but will be a
windows service application when finished. 

function TDeneme.TestClientConnection(IP:string):Boolean;
begin
  WSocket := TWSocket.Create(nil);
  try
    WSocket.OnSessionConnected := SocketSessionConnected;
    WSocket.Proto              := 'tcp';
    WSocket.SocketFamily       := sfIPv4;
    WSocket.Addr               := IP;
    WSocket.LocalAddr          := '0.0.0.0';
    WSocket.Port               := DM.WSOCKET_CLI_PORT;
    WSocket.LocalPort          := DM.WSOCKET_SRV_PORT;
    WSocket.LineMode           := True;
    WSocket.LineEnd            := #13#10;
    { Connect is asynchronous (non-blocking). When the session is  }
    { connected (or fails to), we have an OnSessionConnected event }
    { This is where actual sending of data is done.                }
    WSocket.Tag := -1;
    WSocket.Connect;
    Sleep(3500); // Assuming that amount of time is enough for a local tcp
connection.
    Result := WSocket.Tag = 0;
    WSocket.Close;
  finally
    WSocket.Free;
  end;
end;

procedure TDeneme.SocketSessionConnected(Sender: TObject; ErrCode: Word);
var Buf:string;
begin
  WSocket.Tag := ErrCode;
  if ErrCode = 0 then begin
    Buf := 'HEY!' + #13#10;
    try
      WSocket.SendStr(Buf);
    except
    end;
  end;
end;


----------
Client side code:
Client application is a vcl windows application.

procedure TForm4.FormCreate(Sender: TObject);
begin
  // Wait for a TCP connection
  with WSocket1 do begin
    Proto              := 'tcp';
    SocketFamily       := sfIPv4;
    Addr               := '0.0.0.0';
    Port               := '0';
    LocalAddr          := '0.0.0.0';
    LocalPort          := DM.WSOCKET_CLI_PORT;
    LineMode           := True;
    LineEnd            := #13#10;
    Listen;
  end;
end;

procedure TForm4.WSocket1DataAvailable(Sender: TObject; ErrCode: Word);
var
  Buffer : array [0..1023] of AnsiChar;
  Len    : Integer;
  Src    : TSockAddrIn6;
  SrcLen : Integer;
begin
  if FSenderAddr.sin6_family = AF_INET then begin
    SrcLen := SizeOf(TSockAddrIn);
    Len    := WSocket1.ReceiveFrom(@Buffer, SizeOf(Buffer),
PSockAddr(@Src)^, SrcLen);
    if Len >= 0 then begin
      if (PSockAddr(@FSenderAddr).sin_addr.S_addr = INADDR_ANY) or
      (PSockAddr(@FSenderAddr).sin_addr.S_addr =
PSockAddr(@Src).Sin_addr.S_addr) then begin
        Buffer[Len] := #0;
        Label1.Caption := String(Buffer);
      end;
    end;
  end;
end;

procedure TForm4.WSocket1SessionClosed(Sender: TObject; ErrCode: Word);
begin
  Label1.Caption := 'Connection closed.';
end;

procedure TForm4.WSocket1SessionConnected(Sender: TObject; ErrCode: Word);
begin
  Label1.Caption := 'Connected. Listening.';
end;



-----Original Message-----
From: TWSocket [mailto:twsocket-boun...@lists.elists.org] On Behalf Of Angus
Robertson - Magenta Systems Ltd
Sent: Sunday, March 13, 2016 11:04 AM
To: twsocket@lists.elists.org
Subject: Re: [twsocket] Pinging multipe hosts

> I will be writing both server and client softwares. No need for a web 
> server.

In which case the server should only ever listen for TCP connections from
clients, which open the connection and send a hello packet once a minute to
prove they are alive, expecting a response so they know the server is alive.


Your can start with my TMagIpLog component, which can be configured as the
client or server, and handles all the opening, retrying and closing of
connections, allowing lines of text (ie your protocol) to be send
back and forward.   

http://www.magsys.co.uk/delphi/magics.asp

You only need to add a couple of events in your code and it should all be
working in an hour.  

For my old bus project, we had three 'activity servers' that the client PCs
contacted in rotation in case of hardware or other problems, and we had a
fall back of a UDP listener on the client that listened to UDP broadcasts
from the server to force all the clients to immediately call home, and a few
others things.  Our protocol document was about 50 pages with 40 different
commands or something, quite complicated, things like retrieving CCTV from
the buses. 

Angus
 

--
To unsubscribe or change your settings for TWSocket mailing list please goto
http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

-- 
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to