Re: [twsocket] Pinging multipe hosts
> For that reason I wrote if it is possible to make it async > earlier. Yes, put your code in the event. > I know that I can put something like "Sleep(100)" No, that will stop the thread for 100ms so nothing more happens. If you really can handle events, you can use something like this: procedure sysDelay (aMs: longword); var Trg: longword; begin Trg := GetTrgMsecs (aMs) ; while True do begin Application.ProcessMessages; if Application.Terminated then break ; if TestTrgTick (Trg) then break ; Sleep(0); // thread now stops for rest of time slice end ; end; Something better is used in some ICS high level components (WaitUntilReady) which exit when the event status changes. 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
Re: [twsocket] Pinging multipe hosts
On 14/03/2016 13:11, Ertan Küçükoğlu wrote: MagIpLog1.StartLogging; if Label2.Caption = 'OK' then begin Label3.Caption := 'Connection OK!'; MagIpLog1.StopLogging; What async means is that the MagIpLog1.StartLogging returns immediately, not after the connection has been established. The component will be trying to establish the connection in another thread but you are already checking if the connection is on with the "if Label2.Caption = 'OK' then begin" line. You have to use the events the component will be firing to check for success/unsuccess of the connection, and proceed sending data from there. In you case, do this from the MagIpLog1LogChangeEvent, using the LogState to know how the connection is going, and when it is OK to send data. -- 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 have prepared two test projects. There is one TMagIpLog > component dropped on form for each project. One test project is > listening its local TCP port Impossible to say what is wrong from your partial code and lack of any attempt to log activities in the applications. You should be reporting everything in LogChangeEvent, instead you are ignoring all errors. I suggest you run two instance of the sample application that comes with TMagIpLog, one as server, one as client, so you see them connecting together, then replace one with your and fix it until it works, then the second one similarly. One common problem with this sort of thing is the Windows Firewall that blocks applications listening by default. I always used to turn off the firewall, since I have a proper hardware firewall, but Windows 10 Update no longer seems to work with the firewall disabled so I'm learning to live with it, and trying to write a component to update it's rules... 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
Re: [twsocket] Pinging multipe hosts
I have prepared two test projects. There is one TMagIpLog component dropped on form for each project. One test project is listening its local TCP port 5000. LogProtocol is set to logprotTcpServer. Second test project is trying to establish a connection to the first test Project using same port number. Below is my code from second test Project. procedure TForm9.Button1Click(Sender: TObject); begin MagIpLog1.LogProtocol := logprotTcpClient; MagIpLog1.RemoteHost := '192.168.1.24'; MagIpLog1.RemoteIpPort := '5000'; MagIpLog1.StartLogging; if Label2.Caption = 'OK' then begin Label3.Caption := 'Connection OK!'; MagIpLog1.StopLogging; end; end; procedure TForm9.MagIpLog1LogChangeEvent(Sender: TObject; Socnr: Integer; LogState: TLogState); begin if LogState = logstateStart then Label1.Caption := 'Start'; if LogState = logstateOK then Label2.Caption:= 'OK'; end; I am seeing that Label3.Caption is not changing at all. It displays 'Label3' always. On the other hand, I read 'Start' and 'OK' texts on relevant components. So, MagIpLog is connecting to other test project. However, connection is, I think, sync. Is there a way to make It async? As you indicated earlier, async connection will solve my problem, in my real code. Thanks. -Original Message- From: TWSocket [mailto:twsocket-boun...@lists.elists.org] On Behalf Of Angus Robertson - Magenta Systems Ltd Sent: Monday, March 14, 2016 12:32 PM To: twsocket@lists.elists.org Subject: Re: [twsocket] Pinging multipe hosts > // PING OK. DO DIRECT TCP CONNECTION > if not TestClientConnection(ClientIP, Err) then begin > Result := '***ERROR: Client TCP connection fail.'; If you really want to do these tests from the server, this needs to become async. You create an array of TWSocket or TMagIpLog components, either one for each client you are going to test or a pool (which is more complicated) setting the Tag to ClientUUID, then you put all your success or fail code in the OnSessionConnected and OnSessionClosed events. Using an array of TMagIpLogs in client mode is easiest, since it already contains threaded ping to test the connection before an open attempt is made, you need a single event onLogChangeEvent and to test LogState. 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
> // PING OK. DO DIRECT TCP CONNECTION > if not TestClientConnection(ClientIP, Err) then begin > Result := '***ERROR: Client TCP connection fail.'; If you really want to do these tests from the server, this needs to become async. You create an array of TWSocket or TMagIpLog components, either one for each client you are going to test or a pool (which is more complicated) setting the Tag to ClientUUID, then you put all your success or fail code in the OnSessionConnected and OnSessionClosed events. Using an array of TMagIpLogs in client mode is easiest, since it already contains threaded ping to test the connection before an open attempt is made, you need a single event onLogChangeEvent and to test LogState. 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
Re: [twsocket] Pinging multipe hosts
> 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. Which does not use any ICS functions or events apart from ping, and is really beyond the scope of this mailing list which is for TWSocket support. But it seems you are still trying to contact clients from the server, which is the wrong way around. 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
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
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
Re: [twsocket] Pinging multipe hosts
> 2- clients that complete ping without error, do additional test > if client software is actually running. What client software, something you wrote or different? Does it contact the web server or vice versa. 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
Re: [twsocket] Pinging multipe hosts
> *Subject:* Re: [twsocket] Pinging multipe hosts > *From:* Ertan Küçüko_lu <ertan.kucuko...@1nar.com.tr> > *To:* "'ICS support mailing'" <twsocket@lists.elists.org> > *Date:* Sat, 12 Mar 2016 19:34:21 +0200 > > I thought, at first, that ping might be a solution. After reading > your post, I better find another way to check if client is > running. More details about subject: > - I will be writing both applications that running on server and > client. > - Client will inform server first time it is running thru a > webservice. > - Client will inform server every minute it is running thru a > webservice. - If it is more than a minute and client still didn't > inform that it is > running, I would like to know that client is really alive, or > busy and couldn't say that it is running. > - If client is really closed, I will do some database operations > on server side. Look at the sample OverbyteIcsPingTst which illustrates ping using a thread, so your application is not blocked while testing a host. As François says, it's better to test the protocol, in your case by sending your own defined packets back and forth every minute. This is very low overhead. But there is no notification if an open TCP connection is lost, until you try to send more data when it will fail after a timeout. But beware opening a TCP connection to a host that is offline is a effectively a blocking operation and will timeout after about 40 seconds preventing any other new connections during that time. That is when it's better to ping first since it has a controlled timeout of a few seconds so you get a failed response more quickly. I did all this 10 years ago tracking 500 London buses with wifi on parts of their routes, so connections dropping all the time, but had to FTP data to and from them when they were online. All this was controlled from a SQL database with a web application showing which buses were online, last contact, and environmental stuff like failed fans that meant the PC would overheat and die, as dozens did. 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
Re: [twsocket] Pinging multipe hosts
I thought, at first, that ping might be a solution. After reading your post, I better find another way to check if client is running. More details about subject: - I will be writing both applications that running on server and client. - Client will inform server first time it is running thru a webservice. - Client will inform server every minute it is running thru a webservice. - If it is more than a minute and client still didn't inform that it is running, I would like to know that client is really alive, or busy and couldn't say that it is running. - If client is really closed, I will do some database operations on server side. I avoid to use a webservice on client, too. Need something faster and simple, if possible. That won't be much of a CPU and network resource user. Maybe a port listenning on client side for a connection. If a connection occurs from server side, it means it is alive, and connection will be closed. Or, client will say it is alive and connection will be closed? Thanks. --Ertan -Original Message- From: TWSocket [mailto:twsocket-boun...@lists.elists.org] On Behalf Of François Piette Sent: Saturday, March 12, 2016 6:37 PM To: 'ICS support mailing' <twsocket@lists.elists.org> Subject: Re: [twsocket] Pinging multipe hosts > I have a Windows service application. That needs to check if several > hosts (IP numbers, > no need to DNS resolve) are still alive. Assuming easiest way to ping them. Since that will > require no installation on my clients at all. Please suggest other > ways that you think apply. Ping messages (ICMP) are frequently blocked for servers. If you have control over the hosts, you can be sure to enable it. But ping doesn't mean the services are running, it only means that the server OS is running. The best way to check for an alive host is to try a connection to the host, connection to the application that you must verify. -- francois.pie...@overbyte.be The author of the freeware multi-tier middleware MidWare The author of the freeware Internet Component Suite (ICS) 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 -- 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 have a Windows service application. That needs to check if several hosts (IP numbers, > no need to DNS resolve) are still alive. Assuming easiest way to ping them. Since that will > require no installation on my clients at all. Please suggest other ways that you think apply. Ping messages (ICMP) are frequently blocked for servers. If you have control over the hosts, you can be sure to enable it. But ping doesn't mean the services are running, it only means that the server OS is running. The best way to check for an alive host is to try a connection to the host, connection to the application that you must verify. -- francois.pie...@overbyte.be The author of the freeware multi-tier middleware MidWare The author of the freeware Internet Component Suite (ICS) 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