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 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧