顯示具有 [Code]C 標籤的文章。 顯示所有文章
顯示具有 [Code]C 標籤的文章。 顯示所有文章

ACM Q101 陣列函數、ANSI

By // 沒有留言:
http://www3.tcgs.tc.edu.tw/~sagit/luckycat/q101.htm

在luckycat中被列為是兩顆星的難度
我的認知是程式撰寫本身一顆星;題目理解邏輯釐清大概三顆星
所以平均下來兩顆星無誤!
此題被列為simulation向,
解題時除非是天資聰穎,不然還是如我一般,土法煉鋼地拿出一張紙,一步一步邊畫邊思考吧!

記錄一:
  意外地發現原來函數在傳遞陣列的時候,傳遞至函數的並不是一整個陣列,而是傳遞存放陣列的記憶體位址!
  原本要在陣列前加&傳遞,後來發現不用加就自動耶!傳遞陣列好像就是直接call by pointer...

紀錄二:
  自討苦吃的在宣告模擬的二微陣列時,宣告成了char型態
  因此在設定初始值的時候要把數字0~N填入陣列好像有一點麻煩
  異想天開死地發現可以用ANSI的方式從48+0~48+n的方式把值填入

後記:
  工作上在維運修改其他的程式的時候,常常有這種不能修改原始宣告的情況
  因為程式最初不是由自己開發的,尤其是大型系統更是好多年來不斷地修改下來的成果
  有時候只能一直去想一些偏門的方法來透過最小的修改方法來達成目的...
  唉...當然就長遠來看,這也是造成一個系統不斷肥大的問題之一
  不過我目前還只是一個小螺絲,說實在話的也不能做什麼
  如果想出各種奇怪的方法來解決問題實在是很令人感到開心
  這也變成我在做ACM的目的
  阿不就好棒棒~~

#include <stdlib.h>
#include <stdio.h>

#define ILLEGAL -1
#define LEGAL   0

int Q101();
void insertArray(int n, char array[][n]);
void printArray(int n, char array[][n]);
void printRow(int n, char row[n]);

int checklegal(int n, char a[][n], int x, int y);
void pileArray(int n, char a[][n], char act[], int x, char row[]);
void moveArray(int n, char a[][n], char anr[], int x, char row[]);
void overArray(int n, char a[][n], char act[], int y, char row[]);
void ontoArray(int n, char a[][n], char acr[], int y, char row[]);

int Q101() {
        printf("Q101 : **\n");

        int     st=0;
        int     n;
        int     x, y;
        char    c1[14+1], act1[4], act2[4];

        gets(c1);
        sscanf(c1, "%d", &n);
        char    array[n][n];
        insertArray(n, array);
        char    row[n];
        memset(row, 0x00, sizeof(char[n]));

        while(1) {
                gets(c1);
                if ( !strcmp(c1, "quit") )
                        break;
                else {
                        sscanf(c1, "%s %d %s %d", &act1, &x, &act2, &y);
                        if ( checklegal(n, array, x, y) != LEGAL )
                                continue;
                        if ( !strcmp(act1, "pile") )
                                pileArray(n, array, act1, x, row);
                        else if ( !strcmp(act1, "move") )
                                moveArray(n, array, act1, x, row);
                        if ( !strcmp(act2, "over") )
                                overArray(n, array, act2, y, row);
                        else if ( !strcmp(act2, "onto") )
                                ontoArray(n, array, act2, y, row);
                }
        }

        printArray(n, array);
}

void ontoArray(n, array, act, y, row)
int     n, y;
char    array[n][n], act[4], row[n];
{
        int     i, j, cnt;
        int     flag=0;
        char    set[0];
        sprintf(set, "%d", y);

        for ( i=0; i<n; i++ ) {
                for ( j=0; j<n; j++ ) {
                        if ( array[i][j] == set[0] ) {
                                flag=1;
                                break;
                        }
                }
                if ( flag ) break;
        }
        printf("Onto Location: [%d][%d]\n", i, j);

        int     a=i, b=j, offset;

        for ( b++; array[a][b]!='\0'; b++ ) {
                for ( offset=0; offset<n; offset++ ) {
                        if ( array[a][b] == 48+offset ) {
                                array[offset][0] = 48+offset;
                                array[a][b]='\0';
                        }
                }
        }

        for ( j++, cnt=0; j<n; j++, cnt++ ) {
                array[i][j] = row[cnt];
        }

}

void overArray(n, array, act, y, row)
int     n, y;
char    array[n][n], act[4], row[n];
{
        int     i, j, cnt;
        int     flag=0;
        char    set[0];
        sprintf(set, "%d", y);

        for ( i=0; i<n; i++ ) {
                for ( j=0; j<n; j++ ) {
                        if ( array[i][j] == set[0] ) {
                                flag=1;
                                break;
                        }
                }
                if ( flag ) break;
        }
        for ( j; j<n; j++ ) {
                if ( array[i][j] == '\0' )
                break;
        }
        printf("Over Location: [%d][%d]\n", i, j);

        for ( cnt=0; row[cnt]!='\0'; cnt++, j++ ) {
                array[i][j] = row[cnt];
        }
}

void moveArray(n, array, act, x, row)
int     n, x;
char    array[n][n], act[4], row[n];
{
        int     i, j, cnt;
        int     flag=0;
        memset(row, 0x00, sizeof(char[n]));
        char    set[0];
        sprintf(set, "%d", x);

        for ( i=0; i<n; i++ ) {
                for ( j=0; j<n; j++ ) {
                        if ( array[i][j] == set[0] ) {
                                flag=1;
                                break;
                        }
                }
                if ( flag ) break;
        }
        printf("Move Location: [%d][%d]\n", i, j);

        row[0] = array[i][j];
        array[i][j] = '\0';

        int     a=i, b=j, offset;

        for ( b++; array[a][b]!='\0'; b++ ) {
                for ( offset=0; offset<n; offset++ ) {
                        if ( array[a][b] == 48+offset ) {
                                array[offset][0] = 48+offset;
                                array[a][b]='\0';
                        }
                }
        }

}

void pileArray(n, array, act, x, row)
int     n, x;
char    array[n][n], act[4], row[n];
{
        int     i, j, cnt;
        int     flag=0;
        memset(row, 0x00, sizeof(char[n]));
        char    set[0];
        sprintf(set, "%d", x);

        for ( i=0; i<n; i++ ) {
                for ( j=0; j<n; j++ ) {
                        if ( array[i][j] == set[0] ) {
                                flag=1;
                                break;
                        }
                }
                if ( flag ) break;
        }
        printf("Pile Location: [%d][%d]\n", i, j);

        for ( j=j, cnt=0; j<n; j++, cnt++ ) {
                row[cnt] = array[i][j];
                array[i][j] = '\0';
        }
}

int checklegal(n, array, x, y)
int     n;
char    array[n][n];
{
        int     i, j, flag;
        char    setx[0];
        sprintf(setx, "%d", x);
        char    sety[0];
        sprintf(sety, "%d", y);

        for ( i=0; i<n; i++ ) {
                flag=0;
                for ( j=0; j<n; j++ ) {
                        if ( array[i][j] == setx[0] )
                                flag++;
                        if ( array[i][j] == sety[0] )
                                flag++;
                }
                if ( flag == 2 )
                        return ILLEGAL;
        }
        return LEGAL;
}

void printArray(int n, char array[][n])
{
        int i, j;
        for ( i=0; i<n; i++ ) {
                for ( j=0; j<n; j++ ) {
                        if ( array[i][j] == '\0' )
                                printf("[ ]", array[i][j]);
                        else
                                printf("[%c]", array[i][j]);
                }
                printf("\n");
        }
}
void printRow(int n, char row[n])
{
        int i;
        for ( i=0; i<n; i++ ) {
                printf("[%c]", row[i]);
        }
        printf("\n");
}

void insertArray(n, array)
int     n;
char    array[n][n];
{
        int i, j;
        memset(array, 0x00, sizeof(char[n][n]));
        for ( i=0; i<n; i++ ) {
                array[i][0] = 48+i;     //ANSI : char[0] = ANSI[48];
        }
}

ACM Q476 動態結構 malloc

By // 沒有留言:
題目詳見:http://luckycat.kshs.kh.edu.tw/homework/q476.htm

基本上單獨僅探討判斷"點"是在哪個"圖"裡面是非常容易的
但是這題會因不同的做法而有難度上的差異(對我而言啦)

其一
如果是將input寫好放在檔案裏面, 再用fpopen的方式讀檔案
只需要判斷字串為何再採取對應的動作即可

其二
直接讓user當下一行一行的input
這種方法好像比較直覺, 但是用C寫起來一點都不直覺
難點在於常用的scanf很難判別使用者輸入不同規則的值...
不過經過滴滴的指引, 透過這篇文章真的是收穫滿滿:
https://www.ptt.cc/man/C_and_CPP/DB9B/DD06/M.1280560435.A.F4A.html
太神辣

其三
當然也可以使用conio.h裡的getche()或getch()來達到判別使用者輸入
不過在Linux裡面並沒有這個函示庫..


本篇的方法著重於動態結構的方法
圖形的structure因為題目有規定最大size,因此宣告長度使用陣列結構的方式即可
點的structure因為沒有限制筆數,所以使用動態陣列


#include <stdio.h>
#include <stdlib.h>

#define size    10
int Q476();

struct shape {
        char    form[1];
        float   x1;
        float   y1;
        float   x2;
        float   y2;
};

struct point {
        float   x;
        float   y;
        struct  point *Next;
};

int Q476()
{
        int             i, j;
        int             scount=0, pcount=0;
        struct shape    shape[size];
        struct point    nolistpoint, *point, *tmppoint;
        char            c1[21+1];

        for ( i=0; i<size; i++ ) {
                gets(c1);
                if ( c1[0] == '*' )     break;
                else if ( c1[0] == 'r' ) {
                        sscanf(c1, "%s%f%f%f%f", &shape[i].form, &shape[i].x1, &shape[i].y1, &shape[i].x2, &shape[i].y2);
                        scount++;
                }
                else    return 0;
        }
        if ( i == 10 ) printf("*\n");
        point = &nolistpoint;
        while ( 0 == 0 ) {
                gets(c1);
                if ( !strcmp(c1, "9999.9 9999.9") ) {
                        break;
                }
                point->Next=malloc(sizeof(struct point));
                point=point->Next;
                sscanf(c1, "%f%f", &point->x, &point->y);
                pcount++;
        }

        int check;
        point = &nolistpoint;
        for ( i=0; i<pcount; i++ ) {
                tmppoint = point;
                free(tmppoint);
                point = point->Next;
                check = 0;
                for ( j=0; j<scount; j++ ) {
                        if ( shape[j].x1 < point->x && point->x < shape[j].x2
                          && shape[j].y2 < point->y && point->y < shape[j].y1 ){
                                printf("Point %d is contained in figure %d\n", i+1, j+1);
                                check = 1;
                        }
                }
                if ( check == 0 )       printf("Point %d is not contained in any figure\n", i+1);
        }
}

動態Cgi 傳值後端(三)

By // 1 則留言:
動態Cgi 傳值後端(一)與動態Cgi 傳值後端(二)的方法有一個傳值長度的限制, 
因此當有需要批次處理的資料要送往後端時, 這種方法的效能就會變得非常差.
並且如果後端回傳值是複數多筆的也不適合透過這種方法處理.

這篇的方法是後端回傳資料時將資料寫入file, 再由前端去讀file已取得多筆的資料.

QryUserList(qry, repfile)
st_IPv6Get              *qry;
char                    *repfile;
{
        int     offset;
        char    subfunction[MAX_FUNCTION_LEN+1];
        offset = 0;
        request.reqlen = MIN_REQ_LEN + MAX_FUNCTION_LEN + sizeof(st_IPv6Get);
        memcpy(&(request.databuf[offset]), "quserlist", MAX_FUNCTION_LEN);
        offset += MAX_FUNCTION_LEN;
        memcpy(&(request.databuf[offset]), qry, sizeof(st_IPv6Get));
        offset += sizeof(st_IPv6Get);

        if (do_request_large_aps(ZUserNameQuery) < 0)
                return(UNKNOW_NET_ERROR);
        if (respond.value == SUCCESS)
                strcpy(repfile, &(respond.databuf[0]));

        return(respond.value);
}

與之前的方法不同的地方在於接收資料時是要讀取repfille.
do_request_large_aps設定暫存檔
在連線的deamon的include/protocols.h中增加此連線的define
#define ZUserNameQuery                  531
do_request_large_aps(opcode)
int     opcode;
{
        int     count;
        int     st;
        char    filename[32], *strptr;

        request.opcode = opcode ;

        if (dbsid <= 0)
                do_tcpopen_aps();

        if ((st = TcpSend(dbsid, (char *) &request, request.reqlen)) !=
                                request.reqlen) {
                TcpClose(dbsid);
                return(-1);
        }
        if ((strptr=(char *)getenv("CGITMP"))!= NULL)
                sprintf(filename, "%s/%03d.cgi", strptr, ((int)getpid() % 1000));
        else
                sprintf(filename, "/tmp/%03d.cgi", (getpid() % 1000));

        if (TcpRecvFile(dbsid, &respond, filename) != 0)
                return(UNKNOW_NET_ERROR);
        strcpy(respond.databuf, filename);

        return(0);
}

../lib/common/tcputil.c
TcpRecvFile(connid, respond, filename)
int                     connid;
struct dbs_respond      *respond;
char                    *filename;
{
        int     totlen, count, len, rtnlen;
        char    buf[BUFSIZ+1];
        int     fd;

        if ((len=OneRecv(connid, respond, MIN_REP_LEN)) != MIN_REP_LEN) {
                return(-1);
        }

        if ((fd = open(filename, O_RDWR|O_CREAT, 0666)) < 0)
                return(-2);

        totlen = respond->replen - MIN_REP_LEN;
        while (totlen > 0) {
                len = (totlen > BUFSIZ) ? BUFSIZ : totlen;
                if ((rtnlen = OneRecv(connid, buf, len)) < 0 ) {
                        close(fd);
                        return(-1);
                }
                totlen -= rtnlen;
                if (write(fd, buf, len) != len) {
                        close(fd);
                        return(-1);
                }
        }
        close(fd);
        return(0);
}

../mklib/funspcA.f
需注意的是, 在用傳Buffer至後端的方式時, struct _DBService 的起始設定為"0"; 若為file則應設為"0x0F".
#ifndef _funspcA_
#define _funspcA_

extern  int     IP_do_ZUserNameQuery(struct dbs_request *, struct dbs_respond *);
extern  int     IP_do_IPStat(struct dbs_request *, struct dbs_respond *);

struct _DBService IP_DBServiceA[] =
{
        { "IPStat", PIPStat, IP_do_IPStat, 1, MAX_ACCESS,0},
        { "ZUserNameQuery", ZUserNameQuery, IP_do_ZUserNameQuery, 0x0F, MAX_ACCESS,0},
        NULL
}

../mklib/ServiceFunc.c
struct _DBService *
GetFunction(opcode)
int     opcode;
{
        int     fidx;
        int     SearchGo;
        struct _DBService * fun;

        SearchGo = TRUE;

        if(SearchGo == TRUE)
        {
                for (fidx=0;fidx < sizeof(IP_DBServiceA)/sizeof(struct _DBService); fidx++) {
                        if (opcode == IP_DBServiceA[fidx].opcode) {
                                fun = (struct _DBService *) &(IP_DBServiceA[fidx]);
                                SearchGo = FALSE;
                                break;
                        }
                }
        }
        return(fun);

}

../mklib/..pc
IP_do_ZUserNameQuery(in, out)
struct dbs_request *in;
struct dbs_respond *out;
{

        char            subfun[MAX_FUNCTION_LEN+1];
        char            buff[8192];
        int             fd;
        int             reclen;
        int             item;
        int             start;
        int             offset;
        st_IPv6Get      qry[1024];
        offset = 0;
        reclen = 0;

        memcpy(&subfun, &(in->databuf[offset]), MAX_FUNCTION_LEN);
        offset += MAX_FUNCTION_LEN;
        if (!strcmp(subfun, "quserlist"))
        {
                memcpy(&qry, &(in->databuf[offset]) , sizeof(st_IPv6Get));
                offset += sizeof(st_IPv6Get);
                fd = initlarge(out->databuf);
                out->value = DBQryUserName(&qry, fd, &reclen);
                out->replen = MIN_REP_LEN + reclen;
                endlarge(fd);
                return(SUCCESS);
        }
}

動態cgi 傳值後端(二)

By // 沒有留言:
前端資料習慣性是放在目錄:產品名/client 底下
這其中包括了開tcpopen、socket、tcprecv的lib
而.h檔案則都是放在 ../include
後端則是 ../mklib

../mklib/funspcA.f
宣告DB連線的STRUCTURE以及後端相對應的含式
#ifndef _funspcA_
#define _funspcA_

extern  int AL_do_OPUser(struct dbs_request *, struct dbs_respond *);

struct _DBService ALDBServiceA[] = 
{
        { "OPUser", OPUser, AL_do_OPUser, 1, MAX_ACCESS,0}
}

../mklib/ServiceFunc.c
尋找對應的含式
struct _DBService *
AL_GetFunction(opcode)
int     opcode;
{
        int     fidx;
        int     SearchGo;
        struct _DBService * fun;

        SearchGo = TRUE;
        if(SearchGo == TRUE)
        {
                for (fidx=0; fidx < sizeof(ALDBServiceA) / sizeof(struct _DBService) ; fidx++ ) {
                        if (opcode == ALDBServiceA[fidx].opcode) {
                                fun = (struct _DBService *) &(ALDBServiceA[fidx]);
                                SearchGo = FALSE;
                                break;
                        }
                }
        }
}

../mklib.pc/test.pc
AL_do_OPUser(in, out)
struct dbs_request *in;
struct dbs_respond *out;
{
        QRYITEM         qryitem;
        char            subfun[MAX_FUNCTION_LEN+1];
        char            buff[8192];
        int             reclen;
        int             item;
        int             start;
        sTESTSTRUCT     qqry;
        memset(&qqry, 0x00, sizeof(sTESTSTRUCT));
        memcpy(&subfun, &(in->databuf[offset]), MAX_FUNCTION_LEN);
        offset = 0;
        reclen = 0;

        memset(&qryitem, 0x00, sizeof(QRYITEM));
        offset += MAX_FUNCTION_LEN;
        if (!strcmp(subfun, "qusrlist"))
        {
                memcpy(&qryitem, &(in->databuf[offset]) , sizeof(QRYITEM));
                offset += sizeof(QRYITEM);
                memcpy(&qqry, &(in->databuf[offset]), sizeof(sTESTSTRUCT));
                offset += sizeof(sTESTSTRUCT);
                out->value = AL_DBQryUserList1(opid, &qryitem, &qqry, out->databuf, &reclen, in);

                out->replen = MIN_REP_LEN + reclen;
                return(SUCCESS);
        }
}

動態cgi 傳值後端(一)

By // 沒有留言:
目前的網頁都是以cgi + Pro*c + OracleDB來呈現
以下是做往後端傳值時的做法

首先需宣告兩個structure以及一些typedef.h,用來 往後端request 與 respond後端

#define       MAXREPLEN           8192
#define       MAX_USERNO_LEN          8
#define       MAX_IP_LEN              15
#define       MIN_REQ_LEN   33
#define       MAX_FUNCTION_LEN 10
struct dbs_request {
 unsigned int    reqlen;
 unsigned int    opcode;
 char            OPID[MAX_USERNO_LEN+1];
 char            IPADDR[MAX_IP_LEN+1];
 char            databuf[MAXREPLEN]; /* 0.0.0.0 */
};

struct dbs_respond {
 unsigned int    replen;
 unsigned int    opcode;
 int             value;
 int             datalen;
 char            databuf[MAXREPLEN];
};

前端cgi傳值如下:

struct dbs_request request;
struct dbs_respond respond;

QryUserList(qqry,qryitem,buff)
sTESTSTRUCT             *qqry;
QRYITEM                 *qryitem;
char                    *buff;
{
        int     offset;
        char    subfunction[MAX_FUNCTION_LEN+1];

        offset = 0;
        request.reqlen = MIN_REQ_LEN + MAX_FUNCTION_LEN +sizeof(QRYITEM)+sizeof(sTESTSTRUCT);
        memcpy(&(request.databuf[offset]), "qusrlist", MAX_FUNCTION_LEN);
        offset += MAX_FUNCTION_LEN;
        memcpy(&(request.databuf[offset]), qryitem, sizeof(QRYITEM));
        offset += sizeof(QRYITEM);
        memcpy(&(request.databuf[offset]), qqry, sizeof(sTESTSTRUCT));
        offset += sizeof(sTESTSTRUCT);

        if (do_request_aps(OPUser) < 0)
                return(UNKNOW_NET_ERROR);

        if (respond.value == SUCCESS)
                memcpy(buff, &(respond.databuf[0]), respond.replen - MIN_REP_LEN);
        return(respond.value);
}

程式分段:

request.reqlen 是用來記錄判斷傳送與接收的資料是否完整無缺, 以長度判斷 :
request.reqlen = MIN_REQ_LEN + MAX_FUNCTION_LEN +sizeof(QRYITEM)+sizeof(sTESTSTRUCT);

將 function名稱 以及 值(2個structure)塞入request.databuf :
memcpy(&(request.databuf[offset]), "qusrlist", MAX_FUNCTION_LEN);
offset += MAX_FUNCTION_LEN;
memcpy(&(request.databuf[offset]), qryitem, sizeof(QRYITEM));
offset += sizeof(QRYITEM);
memcpy(&(request.databuf[offset]), qqry, sizeof(sTESTSTRUCT));
offset += sizeof(sTESTSTRUCT);


do_request_aps() 寫在client/seedapi.c
do_request_aps(opcode)
int     opcode;
{
        int     count;
        int     st;

        request.opcode = opcode ;

        if ((dbsid <= 0) && do_tcpopen_aps() < 0)
                return(-1);

        if ((st = TcpSend(dbsid, (char *) &request, request.reqlen)) !=
                                request.reqlen) {
                fprintf(stdout, "Fatal Error : send request failure, value=%d\n", st);
                fprintf(stderr, "Fatal Error : send request failure, value=%d\n", st);
                TcpClose(dbsid);
                dbsid=0;
                return(-1);
        }
        if ((st = TcpRecv(dbsid, &respond, MAXREPLEN)) <= 0) {
                fprintf(stdout, "Fatal Error : recv respond failure, value=%d\n", st);
                fprintf(stderr, "Fatal Error : recv respond failure, value=%d\n", st);
                TcpClose(dbsid);
                dbsid=0;
                return(-1);
        }

        if ( respond.opcode != request.opcode)
                return(UNKNOW_NET_ERROR);

        return(0);
}
do_tcpopen_aps()會先去撈setenv.dat
連線至hostname:hostport => TcpOpen()
do_tcpopen_aps()
{
        int     count;
        char    hostname[24], *strptr;
        int     hostport;
        int     st;

        if ((strptr = (char *)getenv("REMOTE_USER")) != NULL)
                strcpy(request.OPID, strptr);

        if ((strptr = (char *)getenv("REMOTE_ADDR")) != NULL)
                strcpy(request.IPADDR, strptr);

        if ((strptr=(char *)getenv("APServer1"))!= NULL)
                strcpy(hostname, strptr);
        else
                strcpy(hostname, SeedHost);
        if ((strptr=(char *)getenv("SEEDALPORT"))!= NULL)
                hostport =  atoi(strptr);
        else
                hostport =  SeedMainPort;
        if ( (dbsid = TcpOpen(hostname, hostport)) < 0) {
                fprintf(stderr, "DBSOpen> Fatal Error : < %s - %d > cannot connect successful\n", hostname, hostport);
                return(-1);
        }
        return(0);
}

TcpOpen()、TcpSend() 與 TcpRecv() 寫在lib/common/tcputil.c
TcpOpen(hostname, port)
char    *hostname;
int     port;
{
        static  struct  sockaddr_in     mysock, tosock;
        static  struct  hostent         *tohost;
        int     count, connid;

        tosock.sin_family       = AF_INET ;
        tosock.sin_port         = htons(port) ;
        if ((tohost = gethostbyname(hostname)) == NULL) {
                tosock.sin_addr.s_addr = inet_addr(hostname);
        }
        else {
                memcpy(&tosock.sin_addr,tohost->h_addr,tohost->h_length);
        }

        if ((connid=socket(AF_INET, SOCK_STREAM, 0)) < 0) {
                fprintf(stderr,"%d:%s:Set Socket Entry failure\n",
                        errno, sys_errlist[errno]);
                return(-1);
        }

        if (connect(connid,(struct sockaddr *) &tosock,sizeof(tosock)) < 0) {
                fprintf(stderr,"%d:%s:Connecting failure\n",
                        errno,sys_errlist[errno]);
                close(connid);
                return(-1);
        }

        return(connid);
}

TcpSend(connid,databuf,len)
int     connid;
char    databuf[];
int     len;
{
        int     totlen, count;

        totlen = 0;
        count=0;
        while (len > 0) {
                count=0;
                if ((count=write(connid, &databuf[totlen], len)) > 0) {
                        len -= count;
                        totlen += count;
                }
                else
                        return(-1);
        }
        return(totlen);
}
TcpRecv(connid, databuf, len)
int     connid;
char    *databuf;
int     len;
{
        int     count, sum, length;
        int     i;

        for (i=0; i < TIMEOUT; i++)
                if (TcpPoll(connid) > 0)
                        break;

        if (i >= TIMEOUT)
                return(-1);

        sum = 0;
        if ((count = read(connid, &length, 4)) != 4) {
                if (count == 0)
                        return(0);
                else
                        return(-2);
        }

        memcpy(&(databuf[0]), &length, sizeof(int));
        length = ntohs(length) - 4;

        sum = 4;
        while (length > 0) {

                for (i=0; i < TIMEOUT; i++)
                        if (TcpPoll(connid) > 0)
                                break;
                if (i >= TIMEOUT)
                        return(-3);

                if ((count=read(connid,&(databuf[sum]),length)) < 0)
                        return(-4);
                length -= count;
                sum += count;
        }

        if (length != 0)
                return(-5);

        return(sum);
}


基本上在TcpRecv()將資料都回respond之後,
就已經是跑完了一套從前端傳值至後端並且再將資料丟回前端的流程了。
這篇記錄的是前端的程式碼,後端如何接收資料以及處理完資料如何傳回前端則是寫在下一篇。
技術提供:Blogger.

Latest Photos

Join the Team

Blogger news

Popular Posts