首页 > 代码库 > 用CGI作为unity3d服务器存储数据

用CGI作为unity3d服务器存储数据

#include   <stdio.h>  
#include   <stdlib.h>  
#include   <string.h>  
/*
<html>
 <head>
 <title>CGI Testing</title>
 </head>
 <body>
 <table width="200" height="180" border="0" style="font-size:12px">
 <div style="font-weight:bold; font-size:15px">Method: POST</div>
 <div>please input two number:<div>
 <form method="post" action="http://127.0.0.1:10086/cgi-bin/gt.cgi">
 <input type="txt" size="3" name="m">*
 <input type="txt" size="3" name="n">=
 <input type="submit" value="http://www.mamicode.com/resu">
 </form>
 </td></tr>
 <tr><td><inputtype="button" value="http://www.mamicode.com/Back Home"onclick=‘javascript:window.location="./index.html"‘></td></tr>
 </table>
 </body>
 </html>
*/

char InputBuffer[4096];  
  
int DecodeAndProcessData(char *input);    /*具体译码和处理数据,该函数代码略*/  
  
int main(int argc, char*argv[])  
{  
    int   ContentLength;   /*数据长度*/  
    int   x;  
    int   i;  
    char   *p;  
    char   *pRequestMethod;     /*   METHOD属性值   */  
    setvbuf(stdin,NULL,_IONBF,0);     /*关闭stdin的缓冲*/  
    printf("Content-type:text/html\n\n");     /*从stdout中输出,告诉Web服务器返回的信息类型*/  
    printf("\n");                                           /*插入一个空行,结束头部信息*/  
    //printf("<p>hello test</p>");  
    /*   从环境变量REQUEST_METHOD中得到METHOD属性值   */  

    pRequestMethod = getenv("REQUEST_METHOD");  
    if(pRequestMethod==NULL)  
    {  
        printf("<p>request = null</p>");  
        return   0;  
    }  

    if (strcmp(pRequestMethod,"POST")==0)  
    {  
        //printf("<p>OK the method is POST!\n</p>");  
        p = getenv("CONTENT_LENGTH");     //从环境变量CONTENT_LENGTH中得到数据长度      
        if (p!=NULL)  
        {  
            ContentLength = atoi(p);  
        }  
        else  
        {  
            ContentLength = 0;  
        }  
        if (ContentLength > sizeof(InputBuffer)-1)   {  
            ContentLength = sizeof (InputBuffer) - 1;  
        }  

        i   =   0;  
        while (i < ContentLength)  
        {                         //从stdin中得到Form数据      
            x  = fgetc(stdin);  
            if (x==EOF)  
                break;  
            InputBuffer[i++] = x;  
        }  
        InputBuffer[i] = \0;  
        ContentLength   =   i;  
        DecodeAndProcessData(InputBuffer);                 //具体译码和处理数据,该函数代码略      
    }  
    else if (strcmp(pRequestMethod,"GET")==0)  
    {  
        //printf("<p>OK the method is GET!\n</p>");  
        p = getenv("QUERY_STRING");     //从环境变量QUERY_STRING中得到Form数据      
        if   (p!=NULL)  
        {  
            strncpy(InputBuffer,p,sizeof(InputBuffer));  
            DecodeAndProcessData(InputBuffer);    //具体译码和处理数据,该函数代码略      
        }  
    }  
    //printf("<HEAD><TITLE>Submitted OK</TITLE></HEAD>\n");//从stdout中输出返回信息      
    //printf("<BODY>The information you supplied has been accepted.</BODY>\n");  

    return   0;  
}  
  
  
int DecodeAndProcessData(char *input)    //具体译码和处理数据     
{  
    printf("%s",input);//输出到客户端
    // 补充具体操作  
    /*int i;
    int len;
    len = (int)strlen(input);
    printf("data length:"+len);
    
    for(i = 0;i < len;i++)
    {
        printf(">>:%d\n",input[i]);
    }*/
    return 0;  
}

 

unity3D 获取数据

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

    // Use this for initialization
    void Start () {
    
    }
    
    // Update is called once per frame
    void Update () {
    
    }

    void Awake()
    {
        TestHttpSend();
    }
    IEnumerator SendPost(string _url, WWWForm _wForm)
    {
        WWW postData = new WWW(_url, _wForm);
        yield return postData;
        if (postData.error != null)
        {
            Debug.Log(postData.error);
        }
        else
        {
            Debug.Log("|"+postData.text+"|");
        }
    }
    public void TestHttpSend()
    {
        //测试POST方法
        WWWForm form = new WWWForm();
        form.AddField("int", "6");
        form.AddField("value", 2);

        /*
        byte[] b = new byte[2];
        b[0] = 1;
        b[1] = 2;
        form.AddBinaryData("myBytes", b,"file.dat","");
        */

        StartCoroutine(SendPost("http://127.0.0.1:10086/cgi-bin/gt.cgi", form));
    }
}

 

unity3D获取的数据

|
int=6&value=http://www.mamicode.com/2|

 

用CGI作为unity3d服务器存储数据