/* io.c */ #include #include #include #include #include "SolarSim.h" void read_initial( AState state, const char *filename ) { int i; double rx, ry, rz, vx, vy, vz, mass; FILE *f = fopen(filename, "rt"); if( !f ) { state->particles = 0; return; } fscanf( f, "%d\n", &state->particles ); state->masses = malloc( state->particles * sizeof(real) ); state->positions = malloc( state->particles * sizeof(vector) ); state->velocities = malloc( state->particles * sizeof(vector) ); state->accelerations = malloc( state->particles * sizeof(vector) ); for( i = 0; i < state->particles; i++ ) { fscanf( f, "%lf %lf %lf %lf %lf %lf %lf\n", &rx, &ry, &rz, &vx, &vy, &vz, &mass ); state->masses[i] = mass; v_init(&state->positions[i],(real)rx,(real)ry,(real)rz); v_init(&state->velocities[i],(real)vx,(real)vy,(real)vz); } fclose(f); } int connect_to_server( char *servername, int portno, int PROTOCOL ){ return connect_to_server_tcp(servername, portno); } int connect_to_server_tcp( char* servername, int portno ){ struct sockaddr_in serv_addr; struct hostent *server; int server_sockfd; server_sockfd = socket( AF_INET, SOCK_STREAM, 0 ); if ( server_sockfd < 0 ) { printf ( "ERROR opening socket" ); return -1; } server = gethostbyname( servername ); if (server == NULL) { fprintf( stderr, "ERROR, no such host\n" ); return -1; } memset( (char *) &serv_addr, 0, sizeof(serv_addr) ); serv_addr.sin_family = AF_INET; memcpy( (char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length ); serv_addr.sin_port = htons( portno ); if ( connect( server_sockfd, (struct sockaddr*) &serv_addr, sizeof( serv_addr ) ) < 0 ) { printf( "ERROR connecting" ); return -1; } return server_sockfd; } int connect_to_server_udp( char* servername, int portno ){ struct sockaddr_in serv_addr, cli_addr; struct hostent *server; int server_sockfd, n; server_sockfd = socket( AF_INET, SOCK_DGRAM, 0 ); if ( server_sockfd < 0 ) { printf ( "ERROR opening socket" ); return -1; } server = gethostbyname( servername ); if (server == NULL) { fprintf( stderr, "ERROR, no such host\n" ); return -1; } memset( (char *) &serv_addr, 0, sizeof(serv_addr) ); serv_addr.sin_family = AF_INET; memcpy( (char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length ); serv_addr.sin_port = htons( portno ); memset( (char *) &cli_addr, 0, sizeof(cli_addr) ); cli_addr.sin_family = AF_INET; cli_addr.sin_addr.s_addr = htonl(INADDR_ANY); cli_addr.sin_port = htons(0); n = bind(server_sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr)); if( n < 0 ) { fprintf(stderr, "%d: cannot bind port\n", portno); return -1; } return server_sockfd; } void send_state( int sock, AState state ) { send( sock, &state->particles, sizeof(int), 0 ); send( sock, state->masses, state->particles*sizeof(real), 0 ); send( sock, state->positions, state->particles*sizeof(vector), 0 ); send( sock, state->velocities, state->particles*sizeof(vector), 0 ); } void write_state( int fd, AState state ) { write( fd, &state->particles, sizeof(int) ); write( fd, state->masses, state->particles*sizeof(real) ); write( fd, state->positions, state->particles*sizeof(vector) ); write( fd, state->velocities, state->particles*sizeof(vector) ); }