Re: [twsocket] Pinging multipe hosts
Yes, I know that I have to use ICS in sync mode and depend on events. Even I, like many others am not used to think & code like that though, am trying to manage. Actually, I believe I got it. Thing is I don't know how to implement it in my current function. Below is my code where I need to complete my pre-tests. I will appreciate, If you can advise as to how to manage what I am trying to do. Or, you may tell me it is not possible as an answer and I stop losing rest of my remaining hair. Use another way to do it. function TDeneme.GetLicense(ClientTime:TDateTime; ClientIP,ClientUUID,LicenseType:string):string; stdcall; var Err:string; begin if MinutesBetween(now, ClientTime) > 10 then begin Result := '***ERROR: ClientTime'; WriteLicenseLog('GetLicense', ClientIP, ClientUUID, LicenseType, Result); Result := StringToTableToXML(Result); Exit; end; if ClientIP = EmptyStr then begin Result := '***ERROR: ClientIP empty'; WriteLicenseLog('GetLicense', ClientIP, ClientUUID, LicenseType, Result); Result := StringToTableToXML(Result); Exit; end; if ClientUUID = EmptyStr then begin Result := '***ERROR: ClientUUID empty'; WriteLicenseLog('GetLicense', ClientIP, ClientUUID, LicenseType, Result); Result := StringToTableToXML(Result); Exit; end; if LicenseType = EmptyStr then begin Result := '***ERROR: LicenseType empty'; WriteLicenseLog('GetLicense', ClientIP, ClientUUID, LicenseType, Result); Result := StringToTableToXML(Result); Exit; end; // Is Client alive? First PING it. if not PingClient(ClientIP, 500, Err) then begin Result := '***ERROR: Ping fail. ClientIP:' + ClientIP + '.'; if Err <> EmptyStr then Result := Result + ' ' + Err; WriteLicenseLog('GetLicense', ClientIP, ClientUUID, LicenseType, Result); Result := StringToTableToXML(Result); Exit; end; // PING OK. DO DIRECT TCP CONNECTION if not TestClientConnection(ClientIP, Err) then begin Result := '***ERROR: Client TCP connection fail.'; if Err <> EmptyStr then Result := Result + ' ' + Err; WriteLicenseLog('GetLicense', ClientIP, ClientUUID, LicenseType, Result); Result := StringToTableToXML(Result); end; // Code will continue from here once above is solved. Result := 'LICENSE WILL BE GRANTED; WriteLicenseLog('GetLicense', ClientIP, ClientUUID, LicenseType, Result); Result := StringToTableToXML(Result); end; -Original Message- From: TWSocket [mailto:twsocket-boun...@lists.elists.org] On Behalf Of Angus Robertson - Magenta Systems Ltd Sent: Sunday, March 13, 2016 7:35 PM To: twsocket@lists.elists.org Subject: Re: [twsocket] Pinging multipe hosts > Pretty confused and stuck at the moment. Give this to my literally > *no knowledge* of using sockets. Below is as far as it gets. > WSocket.Connect; > Sleep(3500); // Assuming that amount of time is enough for a local tcp > connection. which is I'm afraid a perfect example of why you would be better starting off with my TMagIpLog component, even if you only look at the source code and modify it for your purposes, although certainly the client side will do everthing you need. The component is not designed as a high traffic server, I mostly use for one-one connections between applications. The most important thing is ICS is generally used in async mode, you NEVER have wait loops or sleep, there is an event that fires when a connection successeds or fails, and that is what riggers you next step. The exception is multi-threaded applications that do nothing else until the next ICS call. 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
Re: [twsocket] Pinging multipe hosts
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
Re: [twsocket] Pinging multipe hosts
> I will be writing both server and client softwares. No need for a web server. Then it is easy for you to add an application level ping. That is each server will response to a simple command ("Hello") with a simple answer ("OK"). One or more clients can periodically send that "Hello " command and verify it receives the correct "OK" answer with a reasonable time. This is called an "end-to-end" application check. -- francois.pie...@overbyte.be Embarcadero MVP http://www.overbyte.be http://francois-piette.blogspot.com -- 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
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