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