douna2014 2015-10-28 15:27
浏览 48

结合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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)