// Display client for MPI mesh program #include #include #include #include #include #include #include #define SRVPORT 10099 int cfd; // communication file descriptor (handle on socket) /* 3D visualization routines: - active only when GRAPHICS is 1*/ void visualsetup() { // declare structure to hold socket address info: struct sockaddr_in serveraddr; int retval; // many functions return values to indicate success/failure serveraddr.sin_family = AF_INET; // always for IPv4 serveraddr.sin_addr.s_addr = inet_addr(srvip); serveraddr.sin_port = htons(SRVPORT); cfd = socket(AF_INET,SOCK_STREAM,0); // register tcp socket with OS // make connection to server. retval = connect(cfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr)); // note the type cast to more generic type (polymorphism) if (retval != 0) { perror("socket failure!"); exit(1); } else printf("socket to display server established\n"); // send value of N: int n = htonl(N); // N is a macro, so can't have a pointer to it! write(cfd,&n,sizeof(int)); } //setup // function to determine byte ordering (1 for network byte ordering) int byteorder() { unsigned int x = 1; return *((char*)&x+3); } // function to swap byte ordering of doubles, returns static char* unsigned char* swapbytes(double* n) { int i; static unsigned char y[8]; unsigned char* x = (unsigned char*)n; for(i=0;i<8;i++) y[i] = x[7-i]; return y; } void visualize(NROW *BB) { int i; unsigned char* sp; // for converting double if (byteorder()) // already using network byte ordering { write(cfd,BB,(N+2)*(N+2)*8); return; } for (i=0;i<(N+2)*(N+2);i++) { sp = swapbytes(((double*)BB)+i); write(cfd,sp,8); } } //visualize void visualcleanup() { int n = 0; write(cfd,&n,sizeof(int)); close(cfd); }