[BACK]Return to test1-tcp.c CVS log [TXT][DIR] Up to [local] / OpenXM / doc / oxlib

Annotation of OpenXM/doc/oxlib/test1-tcp.c, Revision 1.3

1.3     ! takayama    1: /* $OpenXM: OpenXM/doc/oxlib/test1-tcp.c,v 1.2 2000/09/07 21:55:48 takayama Exp $ */
1.1       takayama    2: /* A sample code to explain how to use ox_asir by TCP/IP and
                      3:    OpenXM control protocol.
                      4:    It computes the gcd of 12 and 8 by calling ox_asir server.
                      5:    */
1.3     ! takayama    6:
        !             7: /*
        !             8:    Asir code that does the same computation on ox_asir server.
        !             9:    Try the following lines on asir.
        !            10:
        !            11:    P = ox_launch();
        !            12:    ox_push_cmo(P,12);
        !            13:    ox_push_cmo(P,8);
        !            14:    ox_push_cmo(P,ntoint32(2));
        !            15:    ox_push_cmo(P,"igcd");
        !            16:    ox_push_cmd(P,269);
        !            17:    ox_pop_string(P);
        !            18:
        !            19:    Then, you get
        !            20:      4
        !            21:
        !            22:    */
        !            23:
1.1       takayama   24: #include <stdio.h>
                     25: #include <sys/types.h>
                     26: #include <sys/socket.h>
                     27: #include <sys/time.h>
                     28: #include <netinet/in.h>
                     29: #include <netdb.h>
                     30:
                     31: #define OX_COMMAND          513
                     32: #define OX_DATA             514
                     33: #define SM_executeFunction  269
                     34: #define SM_popString        263
                     35: #define CMO_STRING            4
                     36:
                     37: #define DATAPORT 4321
                     38: #define CONTROLPORT 1234
                     39:
                     40: static int Serial = 0;
                     41:
                     42: main() {
                     43:   int dataPort, controlPort;
                     44:   struct hostent *servhost;
                     45:   struct sockaddr_in cServer;
                     46:   struct sockaddr_in dServer;
                     47:   char s[1];
                     48:
                     49:   /* Starting the ox_asir server */
                     50:   system("xterm -geometry 80x25+0+0 -e ../../bin/ox -ox ../../bin/ox_asir -control 1234 -data 4321 &");
                     51:   sleep(3);
                     52:   if ((servhost = gethostbyname("localhost")) == NULL) {
                     53:     fprintf(stderr,"bad server name.\n"); exit(1);
                     54:   }
                     55:
                     56:   /* Connecting to the control port */
                     57:   bzero((char *)&cServer,sizeof(cServer));
                     58:   cServer.sin_family = AF_INET;
                     59:   cServer.sin_port = htons(CONTROLPORT);
                     60:   bcopy(servhost->h_addr,
                     61:         (char *)&cServer.sin_addr,servhost->h_length);
                     62:   if ((controlPort = socket(AF_INET,SOCK_STREAM,0)) <0) {
                     63:     fprintf(stderr,"socket allocation is failed.\n");
                     64:   }
                     65:   fprintf(stderr,"Trying to connect port %d  ",ntohs(cServer.sin_port));
                     66:
                     67:   if (connect(controlPort,(struct sockaddr *)&cServer,sizeof(cServer)) == -1) {
                     68:     fprintf(stderr,"cannot connect\n");
                     69:   }else{  fprintf(stderr,"Connected\n"); }
                     70:   sleep(1);
                     71:
                     72:   /* Connecting to the data port */
                     73:   bzero((char *)&dServer,sizeof(dServer));
                     74:   dServer.sin_family = AF_INET;
                     75:   dServer.sin_port = htons(DATAPORT);
                     76:   bcopy(servhost->h_addr,
                     77:         (char *)&dServer.sin_addr,servhost->h_length);
                     78:   if ((dataPort = socket(AF_INET,SOCK_STREAM,0)) <0) {
                     79:     fprintf(stderr,"socket allocation is failed.\n");
                     80:   }
                     81:   fprintf(stderr,"Trying to connect port %d  ",ntohs(dServer.sin_port));
                     82:
                     83:   if (connect(dataPort,(struct sockaddr *)&dServer,sizeof(dServer)) == -1) {
                     84:     fprintf(stderr,"cannot connect\n");
                     85:   }else{  fprintf(stderr,"Connected\n"); }
                     86:
                     87:   /* Byte order negotiation */
                     88:   read(controlPort,s,1);
                     89:   read(dataPort,s,1);
                     90:   s[0] = 0;
                     91:   write(controlPort,s,1);
                     92:   write(dataPort,s,1);
                     93:   fflush(NULL);
                     94:
                     95:   /* Computation by the engine */
                     96:   hoge(dataPort);
                     97:
                     98:   printf("\nType in ctrl-C to quit.\n");
                     99:   sleep(10000);
                    100: }
                    101:
                    102: ox_push_cmo(int dataPort,char *cmo,int length) {
                    103:   extern int Serial;
                    104:   char oxtag[4];
                    105:   char oxserial[4];
                    106:   *((int *)oxtag) = (int) htonl(OX_DATA);
                    107:   *((int *)oxserial) = (int) htonl(Serial++);
                    108:   write(dataPort,oxtag,4);
                    109:   write(dataPort,oxserial,4);
                    110:   write(dataPort,cmo,length);
                    111:   fflush(NULL);
                    112: }
                    113:
                    114: ox_push_cmd(int dataPort, int cmd) {
                    115:   extern int Serial;
                    116:   char oxtag[4];
                    117:   char oxserial[4];
                    118:   char oxcmd[4];
                    119:   *((int *)oxtag) = (int) htonl(OX_COMMAND);
                    120:   *((int *)oxserial) = (int) htonl(Serial++);
                    121:   *((int *)oxcmd) = (int) htonl(cmd);
                    122:   write(dataPort,oxtag,4);
                    123:   write(dataPort,oxserial,4);
                    124:   write(dataPort,oxcmd,4);
                    125:   fflush(NULL);
                    126: }
                    127:
                    128: ox_pop_int32(int dataPort) {
                    129:   char s[4];
                    130:   read(dataPort,s,4);
                    131:   return((int) ntohl( *((int *)s)));
                    132: }
                    133:
                    134: ox_pop_string(int dataPort,char *buf,int limit) {
                    135:   int oxtag, serial, cmotag, length ;
                    136:
                    137:   ox_push_cmd(dataPort,SM_popString);
                    138:   oxtag = ox_pop_int32(dataPort);  /* It must be CMO_DATA */
                    139:   serial = ox_pop_int32(dataPort); /* Read the serial number */
                    140:   cmotag = ox_pop_int32(dataPort); /* Read the CMO tag */
                    141:   if (cmotag != CMO_STRING) {
                    142:        fprintf(stderr,"The tag should be CMO_STRING.\n");
                    143:        exit(1);
                    144:   }
                    145:   length = ox_pop_int32(dataPort);
1.2       takayama  146:   if (length > limit-1) {
1.1       takayama  147:        fprintf(stderr,"Too long string to read.\n");
                    148:        exit(1);
                    149:   }
1.2       takayama  150:   read(dataPort,buf,length);
                    151:   buf[length]=0;
1.1       takayama  152: }
                    153:
                    154: #define SIZE 1024
                    155: hoge(int dataPort) {
                    156:   char s[SIZE];
                    157:   /*  (CMO_ZZ,12); */
                    158:   unsigned char cmo0[]=
                    159:   {00, 00, 00, 0x14,
                    160:    00, 00, 00, 01, 00, 00, 00, 0xc};
                    161:
                    162:   /* (CMO_ZZ,8) */
                    163:   unsigned char cmo1[] =
                    164:   {00, 00, 00, 0x14,
                    165:    00, 00, 00, 01, 00, 00, 00, 8};
                    166:
                    167:   /* (CMO_INT32,2); */
                    168:   unsigned char cmo2[] =
                    169:   { 00, 00, 00, 02, 00, 00, 00, 02};
                    170:
                    171:   /* (CMO_STRING,"igcd") */
                    172:   unsigned char cmo3[] =
                    173:   {00, 00, 00, 04, 00, 00, 00, 04,
                    174:    0x69,0x67,0x63,0x64 };
                    175:
                    176:
                    177:   ox_push_cmo(dataPort,cmo0,12);
                    178:   ox_push_cmo(dataPort,cmo1,12);
                    179:   ox_push_cmo(dataPort,cmo2,8);
                    180:   ox_push_cmo(dataPort,cmo3,12);
                    181:
                    182:   ox_push_cmd(dataPort,SM_executeFunction);
                    183:   ox_pop_string(dataPort,s,SIZE);
                    184:
                    185:   printf("gcd of 12 and 8, in the string format, is \n");
                    186:   printf("%s",s);
                    187:   printf("\n");
                    188: }
                    189:
                    190:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>