source: proiecte/SolarSim/C/Serial/src/io.c @ 152

Last change on this file since 152 was 152, checked in by (none), 14 years ago
File size: 3.5 KB
Line 
1/* io.c */
2#include <sys/types.h>
3#include <sys/socket.h>
4#include <arpa/inet.h>
5#include <netdb.h>
6
7#include "SolarSim.h"
8
9void
10read_initial( AState state, const char *filename ) {
11        int i;
12        double rx, ry, rz, vx, vy, vz, mass;
13
14        FILE *f = fopen(filename, "rt");
15        if( !f ) {
16                state->particles = 0;
17                return;
18        }
19
20        fscanf( f, "%d\n", &state->particles );
21        state->masses = malloc( state->particles * sizeof(real) );
22        state->positions = malloc( state->particles * sizeof(vector) );
23        state->velocities = malloc( state->particles * sizeof(vector) );
24        state->accelerations = malloc( state->particles * sizeof(vector) );
25       
26        for( i = 0; i < state->particles; i++ ) {
27                fscanf( f, "%lf %lf %lf %lf %lf %lf %lf\n", &rx, &ry, &rz, &vx, &vy, &vz, &mass );
28                state->masses[i] = mass;
29                v_init(&state->positions[i],(real)rx,(real)ry,(real)rz);
30                v_init(&state->velocities[i],(real)vx,(real)vy,(real)vz);
31        }
32
33        fclose(f);
34}
35
36int
37connect_to_server( char *servername, int portno, int PROTOCOL ){
38        return connect_to_server_tcp(servername, portno);
39}
40
41int
42connect_to_server_tcp( char* servername, int portno ){
43        struct sockaddr_in serv_addr;
44        struct hostent *server;
45        int server_sockfd;
46
47        server_sockfd = socket( AF_INET, SOCK_STREAM, 0 );
48        if ( server_sockfd < 0 ) {
49                printf ( "ERROR opening socket" );
50                return -1;
51        }
52   
53        server = gethostbyname( servername );
54        if (server == NULL) {
55                fprintf( stderr, "ERROR, no such host\n" );
56                return -1;
57        }
58   
59        memset( (char *) &serv_addr, 0, sizeof(serv_addr) );
60        serv_addr.sin_family = AF_INET;
61        memcpy( (char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length );
62        serv_addr.sin_port = htons( portno );
63   
64        if ( connect( server_sockfd, (struct sockaddr*) &serv_addr, sizeof( serv_addr ) ) < 0 ) {
65                printf( "ERROR connecting" );
66                return -1;
67        }
68
69        return server_sockfd;
70}
71
72
73int
74connect_to_server_udp( char* servername, int portno ){
75        struct sockaddr_in serv_addr, cli_addr;
76        struct hostent *server;
77        int server_sockfd, n;
78
79        server_sockfd = socket( AF_INET, SOCK_DGRAM, 0 );
80        if ( server_sockfd < 0 ) {
81                printf ( "ERROR opening socket" );
82                return -1;
83        }
84   
85        server = gethostbyname( servername );
86        if (server == NULL) {
87                fprintf( stderr, "ERROR, no such host\n" );
88                return -1;
89        }
90   
91        memset( (char *) &serv_addr, 0, sizeof(serv_addr) );
92        serv_addr.sin_family = AF_INET;
93        memcpy( (char *)&serv_addr.sin_addr.s_addr, (char *)server->h_addr, server->h_length );
94        serv_addr.sin_port = htons( portno );
95
96        memset( (char *) &cli_addr, 0, sizeof(cli_addr) );
97        cli_addr.sin_family = AF_INET;
98        cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
99        cli_addr.sin_port = htons(0);
100
101        n = bind(server_sockfd, (struct sockaddr *) &cli_addr, sizeof(cli_addr));
102        if( n < 0 ) {
103                fprintf(stderr, "%d: cannot bind port\n", portno);
104                return -1;
105        }
106
107        return server_sockfd;
108}
109
110void
111send_state( int sock, AState state ) {
112        send( sock, &state->particles, sizeof(int), 0 );
113        send( sock, state->masses, state->particles*sizeof(real), 0 );
114        send( sock, state->positions, state->particles*sizeof(vector), 0 );
115        send( sock, state->velocities, state->particles*sizeof(vector), 0 );
116}
117
118void
119write_state( int fd, AState state ) {
120        write( fd, &state->particles, sizeof(int) );
121        write( fd, state->masses, state->particles*sizeof(real) );
122        write( fd, state->positions, state->particles*sizeof(vector) );
123        write( fd, state->velocities, state->particles*sizeof(vector) );
124}
Note: See TracBrowser for help on using the repository browser.