结合PHP Webservice和C#Client

I have a C#/Winforms application using PHP webservices. A first PHP webservice uploadFile.php is used to upload a blob file (by a unix application for example) on a mysql database and another webservice, getFile.php (code below), is used to get the file if a file is available for the client.

For the moment the client calls every 5 seconds the webservice. Obviously Apache cannot deal with all the connections and gets slow and is often down or timeout, Today I have counted 2000 connections in 2 minutes, and 100 000 in 25 minutes. 99% of them return no file.

So instead of calling every 5 (or 10 or 20 or else) seconds the webservice, I would like the webservice uploadFile to notify the client when a result is available. I'm not familiar with SOAP/REST or any other kind of webservices, and for practical reasons (old versions of the application still running) I could not replace the current webservice with REST. Here is what currently does the webservice getOK.php :

 if(getJobsForUser($login, $serveur)){

    //data is BLOB content
    $query = "SELECT idjob,fileName,data FROM JobSpool WHERE idcompte=$idcompte LIMIT 1" ;  
    $stmt = $pdo->query($query);
    $res = $stmt->fetch(PDO::FETCH_BOTH);  
    $idjob = $res[0] ;
    $nomfic = $res[1] ;
    $data = $res[2] ;
    //send datas to client
    echo $nomfic."
" ;
    echo $data ; 
 }

 function checkJobsforUser($login, $serveur){
    global $pdo, $idcompte;

    $query = "SELECT count(*),c.idcompte FROM Compte as c,Job as j WHERE c.login='$login' AND c.serveur = '$serveur' AND j.idcompte = c.idcompte" ;

    $stmt = $pdo->query($query);
    $results = $stmt->fetch(PDO::FETCH_BOTH);
    //print_r($results);
    $count = $results[0];
    $idcompte = $results[1];
    //echo "count : $count, idcompte : $idcompte <br/>";
    if($count > 0)
        return true;
    return false;

}

C# code :

public String getMyFile(){
    String nomFicFinal = String.Empty;
    try
    {
        HttpWebRequest hwr;
        String request = "http://myWebServiceDomain.com/getFile.php?login=myLogin&otherId=myOtherId";
        hwr = (HttpWebRequest)HttpWebRequest.Create(request);

        using (HttpWebResponse hwrep = (HttpWebResponse)hwr.GetResponse())
        {
            //read datas 
            br = new BinaryReader(hwrep.GetResponseStream());
            Byte[] tmp = new Byte[256];
            int b = br.Read();
            while (b != -1)
            {
                if (b == 10) break;
                tmp[indNomFic++] = (Byte)b;
                b = br.Read();
            }

            //get filename
            nomFicFinal = System.Text.Encoding.ASCII.GetString(tmp, 0, indNomFic);

            //if parsed data == "0" nothing returned
            if (nomFicFinal.Length == 1 && (nomFicFinal.CompareTo("0") == 0 || nomFicFinal.CompareTo("1") == 0))
            {
                br.Close();
                nomFicFinal = "";
            }
            else //we parse the content of the request and we create the file with the name nomFicFinal in ApplicationData
            {
                nomFicFinal = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\MyApp\" + nomFicFinal;
                bw = new BinaryWriter(File.Open(nomFicFinal, FileMode.Create));

                tmp = new Byte[1024];
                while ((tmp = br.ReadBytes(1024)) != null)
                {
                    if (tmp.Length == 0) break;
                    bw.Write(tmp);
                    bw.Flush();
                }
                br.Close();
                bw.Close();
            }
        }
    }catch(Exception ex){
        //do something
    }

    return nomFicFinal;

}

I have been searching with "callback PHP C#", etc, but I still do not have a clear idea of what to do for the moment.

du22399
du22399 让我们在聊天中继续讨论。
4 年多之前 回复
doudeng8691
doudeng8691 对不起,我没有把所有代码都放好。在调用之前我已经检查过是否有结果。我想编辑
4 年多之前 回复
douzhong8856
douzhong8856 永久使用300个apache线程。我们尝试为mysql_connect和mysql_close更改mysql_pconnect,但仍然相同。我目前只计算了125个不同的用户
4 年多之前 回复
doujiang9887
doujiang9887 至于一个通知选项,你可以简单地ping一个网址告诉c#它有新的结果。例如,php.example.com获取上传。处理文件,并调用csharp.anotherexample.com/api/newUpload(可能只是将文件推送到它,或者只是说,'新上传')。此时,您的c#app,检查新文件或接收文件。
4 年多之前 回复
douti19680318
douti19680318 我的意思是,如果c#是发出请求的那个,并且他每隔5秒轮询一次服务器,那么它不应该造成那么大的负担。如果请求在<100ms内完成,则连接不应保持打开状态,并且apache应该没有任何问题。
4 年多之前 回复
drudfe0446838
drudfe0446838 因为我们有许多客户端和托管unix应用程序的不同unix服务器,有时不在同一区域。而C#是一个客户端应用程序(一个用户)。但你的意思是什么,但直接上传到C#?
4 年多之前 回复
dqqs64238
dqqs64238 所以unix应用程序上传文件,c#应用程序获取文件?什么不只是直接将文件上传到c#?
4 年多之前 回复
douye8500
douye8500 Unix应用程序命中uploadFile.php。在此期间,C#应用程序会定期命中getFile.php。希望它更清楚
4 年多之前 回复
dongzhansong5785
dongzhansong5785 什么实际上击中了什么?c#应用程序正在点击php应用程序?你问题中的“客户”是什么?
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问