Announcement

Collapse
No announcement yet.

Server Exchange

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Server Exchange

    From my PC PBWin app, I want to send a string to a server app, have the server app look up the string in a text database, then return the string and any associated information to my PBWin app. The returned string might be as much as 4K bytes.

    That sounds like a straight forward thing to do but it's outside my usual areas of expertise.

    Looking up the string with the server app should be easy enough to do. But how to send the lookup string to the server app from my PC PBWin app and then returning the lookup results from the server app to my PC PBWin are nothing I'm familiar with.

    It seems like the task ought to be very straightforward but as I say, it's nothing I've done before.

    Can anyone point to a simple example showing the basics of the task?

  • #2
    One "easy" solution might be to send the string to the server app, have the server app create a temporary file with the lookup results, then use URLDownloadToFile to retrieve the temporary file which I can open from within my PBWin app (perhaps a delay added to make sure the temporary file is created, or a loop waiting for a URLDownloadToFile success).

    ... easy that is, if I knew how to send the string to activate the server app.

    With the string received by the server, doing the lookup and creating the temporary file is something I can do.


    Comment


    • #3
      Is it as simple as something like this ...

      Code:
      URL$ = "http://www.garybeene.com/files/lookup.exe?lookupstring"
      iReturn = ShellExecute(hDlg, "Open", URL, $Nul, $Nul, %SW_ShowNormal)​
      ]

      ... where the 2nd line will run the "lookup.exe", which in turn creates the temporary file containing the lookup information?

      Or, would it be easier if the lookup.EXE was lookup.PHP? Or is the choice of EXE/PHP not really a factor?

      Comment


      • #4
        This will return the contents of a file as a string
        It can also start a program on your server to create the data to be returned.
        The results can be returned within an html page or written to a file.

        Other options are SQLitening, SQLTools, SocketTools and many more.
        Code:
        #DIM ALL
        #INCLUDE "httprequest.inc"
        FUNCTION PBMAIN() 'pbtest.bas
         LOCAL wsurl,wsusername,wspassword,wsdata,wserror AS WSTRING
         wsUrl   = "https://dotysoftware.com/test.txt"
         wsUserName = ""
         wsPassword = ""
         wsError = DownloadTest(wsUrl,wsUsername,wsPassword,wsData)
         ? wsData,,sError 'results: Frank Burns eats worms.   OK
        END FUNCTION
        
        FUNCTION DownloadTest(wsURL   AS WSTRING,_
                      wsUserName      AS WSTRING,_
                      wsPassword      AS WSTRING,_
                      wsDataReturned  AS WSTRING) AS WSTRING 'returns OK or error message
         RESET wsDataReturned
         DIM pHttpReq AS IWinHttpRequest
         pHttpReq = NEWCOM "WinHttp.WinHttpRequest.5.1"
         IF ISNOTHING(pHttpReq) THEN
          ? "WinHttpRequest.5.1 failure",%MB_ICONERROR OR %MB_SYSTEMMODAL,FUNCNAME$
          EXIT FUNCTION
         END IF
         pHttpReq.Open "GET", wsURL, 0
         pHttpReq.SetCredentials wsUserName, wsPassword, %HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
         pHttpReq.Send
         IF pHttpReq.StatusText <> "OK" THEN
          IF pHttpReq.StatusText = "" THEN
            FUNCTION = "No send response"
          ELSE
            FUNCTION = pHttpReq.StatusText
          END IF
         ELSE  'success
          wsDataReturned = pHttpReq.ResponseText
          FUNCTION = pHttpReq.StatusText
         END IF
         pHttpReq = NOTHING
        END FUNCTION'

        Comment


        • #5
          Howdy, Mike!

          Thanks for the reply.

          It can also start a program on your server to create the data to be returned.
          I don't recognize how your example does that. I can see that it downloads the "test.txt" file content as a string but how is the server executable initiated and how is the string passed to the server executable?

          Comment


          • #6
            wsURL = https yoursite.com /cgi-bin/this.exe?request=

            Board modifies my url so chopped it up a bit.
            Code:
            #INCLUDE "pbcgi.inc"  'this.exe
            FUNCTION PBMAIN () AS LONG
             LOCAL sBufferIn,sDataOut, request AS STRING
             sbufferIn = ReadCgi      
             request = cgiParam(sBufferIn,"request") 'get ?request
             'process request into sDataOut
             PageHeader ""       ' load html page header
             WriteCgi sDataOut   ' fill with sDataOut and return page
            END FUNCTION
            
            SUB PageHeader(sDataOut AS STRING)
             WriteCgi "</title><body>"
             WriteCgi "<pre>"
             WriteCgi "<form action='/cgi-bin/this.exe' method='POST'>"
             WriteCgi "  <INPUT TYPE='text'  NAME='request'  Value = '"+sDataOut+"' SIZE='120'><BR>"
             WriteCgi "<INPUT TYPE='submit'  value='Gary Request'>"
             writeCgi "</form>
             writeCgi "</pre>"
             writeCgi "</body>
             writeCgi "</html>"
            END SUB

            Comment


            • #7
              Howdy, Mike!

              Thanks! I'll do some tests with it tomorrow before asking any further questions! I really do appreciate the reply!

              Comment


              • #8
                No problem. If you need a test page or want to put a cgi on my server just ask.

                Comment


                • #9
                  Second time I've said something similar recently:
                  Look at the EchoServer in PB Samples\Internet\TCP. modifying it to do exactly what you want would be trivial.

                  The Client should work "as is".

                  In the server application just replace
                  IF LEN(sBuffer) THEN TCP SEND hEcho, sPacket + " -> Received Ok!"
                  with something like

                  IF LEN(sBuffer) THEN
                  s = GetData(sPacket)
                  TCP SEND hEcho, s
                  end if


                  and have a function
                  FUNCTION GetData(sIn as STRING) as STRING
                  LOCAL sData as STRING
                  ...
                  'Parse sIn and get appropriate data into sData
                  .....
                  Function = sData
                  END FUNCTION

                  Comment


                  • #10
                    I highly suggest using https to get encryption and hide requests and data returned.

                    Comment


                    • #11
                      Originally posted by Mike Doty View Post
                      I highly suggest using https to get encryption and hide requests and data returned.
                      There's no indication that Gary is talking about going ouside of a local network.

                      Even if he is, he could still use the Echo Client/Server, encrypting/decrypting the packets inside the applications.
                      That would also be a trivial exercise and won't require anything fancy like TLS connections or HTTP in any form.
                      For additional security, he can use any port for the communication rather than a likely watched port such as 80.
                      He could even use port switching in the applications to make it even harder for a man in the middle to track.

                      Comment


                      • #12
                        There's no indication that Gary is talking about going ouside of a local network.
                        In Gary's post # 3 the internet is used.
                        URL$ = "http://www.garybeene.com/files/lookup.exe?lookupstring"
                        iReturn = ShellExecute(hDlg, "Open", URL, $Nul, $Nul, %SW_ShowNormal)​​

                        Comment


                        • #13
                          Originally posted by Mike Doty View Post
                          In Gary's post # 3 the internet is used.
                          Assuming that his client app is not on the same LAN as www.garybeene.com

                          OK, let's just say then:
                          In his original post, there's no indication that Gary is talking about going ouside of a local network.

                          Comment


                          • #14
                            Originally posted by Gary Beene View Post
                            [B]
                            I don't recognize how your example does that. I can see that it downloads the "test.txt" file content as a string but how is the server executable initiated and how is the string passed to the server executable?
                            In his example, it doesn't. He's just retrieving a static file from the URL.
                            You would need some sort of server side processing such as a PHP script or a CGI application on a web server to react to parameters includied in a Post or Get request.
                            For a very brief intro to Get and Post, see here;
                            https://www.w3schools.com/tags/ref_httpmethods.asp

                            Once you have acted on the input parameter by querying a database, getting text or whatever,
                            you can then return it either by ECHOing it back to the sender or by building a downloadable attachment using "Header(...)" lines

                            Comment


                            • #15
                              Does the hosting organisation for garybeane.com allow Windows .exe CGIs ?

                              Added: Apparently the server is running on Apache/2.4.6 (CentOS)​ so a WIn32 CGI executable (server app) is probably out of the question.

                              Looks like some PHP study is called for

                              Click image for larger version

Name:	gb.jpg
Views:	146
Size:	63.7 KB
ID:	818959

                              Comment


                              • #16
                                At it's absolute simplest, save this as the file GetData.php at www.garybeene.com

                                '
                                Code:
                                <?php
                                if (!(isset($_GET['Data'])) ) {
                                    die("No requested data");
                                }
                                $In = $_GET['Data'];
                                echo 'Just looked up the data for '.$In ;
                                ?>


                                Then try
                                wsUrl = "https://www.garybeene.com/GetData.php?Data=somekey"
                                in Mike's post 4

                                Comment


                                • #17
                                  Slightly more complex: return a text file based on the input parameter.
                                  '
                                  Code:
                                  <?php
                                  if (!(isset($_GET['Data'])) ) {
                                      die("No requested data");
                                  }
                                  $filename = $_GET['Data'].".txt";
                                  $fh = fopen($filename,'r') or die("Can't open ".$filename) ;
                                  $readtext = fread($fh, filesize($filename));
                                  fclose($fh);
                                  echo $readtext;
                                  ?>
                                  '

                                  It would not be too much additional effort to query a database to get the return string.
                                  Last edited by Stuart McLachlan; 22 Sep 2022, 10:02 AM.

                                  Comment


                                  • #18
                                    Stuart,
                                    Now you did it. I'm going to get php.

                                    Gary,​
                                    The wrapper function I wrote in post 4 "DownloadTest" was from code by Jose Roca.
                                    It also allows access to https protected pages without a library.
                                    Very useful to restrict access. I would only use with https.

                                    I don't know if deleting cache could be added or is needed? I will do some testing.
                                    It makes requests and returns results as a string not a file (which is a big plus.)
                                    The term "DownLoadTest" is not a good name for it.

                                    Might not want to use names ?names= discussed on this forum.
                                    Encrypted with https, but might not want to give them away.
                                    Might also include some hidden things and a timestamp to prevent playback.
                                    Have no idea what you are doing or if security is needed.
                                    I added a protected subdirectory under cgi-bin if only want username/password people.

                                    Comment


                                    • #19
                                      Originally posted by Mike Doty View Post
                                      Might not want to use names ?names= discussed on this forum.
                                      Encrypted with https, but might not want to give them away.
                                      Might also include some hidden things and a timestamp to prevent playback.
                                      The only reason for the ?names= is because your code used GET.
                                      If you use POST , you don't expose the parameters. You'd do something like:

                                      pHttpReq.Open "POST", wsURL, 0
                                      pHttpReq.Send("Data=abc")​


                                      and in your PHP, use $_POST['Data']

                                      That removes the issue of playback.

                                      Comment


                                      • #20
                                        Wow, I'll definitely be changing code.
                                        Thank you!

                                        Comment

                                        Working...
                                        X