getobs.c 4.97 KB
Newer Older
saeed's avatar
saeed committed
1
#include <stdio.h>      /* printf, sprintf */
saeed's avatar
saeed committed
2
#include <stdlib.h>      /* printf, sprintf */
saeed's avatar
saeed committed
3
4
5
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h>      /* struct hostent, gethostbyname */
saeed's avatar
saeed committed
6
#include"strcomp.h"      
saeed's avatar
saeed committed
7
#include"splitstring.h"      
saeed's avatar
saeed committed
8
#include"strtof.h"
saeed's avatar
saeed committed
9
#include"sort.h"
saeed's avatar
saeed committed
10
      
saeed's avatar
saeed committed
11
12
#define N (4096)
char response[N]  = {0};
saeed's avatar
saeed committed
13
14
15
int main(int argc,char *argv[])
{
  //http://oceandata.smhi.se/ssh/"+station+"/OBSERVATION",params = param
saeed's avatar
saeed committed
16
  
saeed's avatar
saeed committed
17
18
  int portno        = 80;
  char *host        = "oceandata.smhi.se";
saeed's avatar
saeed committed
19
  //char message[]   = "GET /ssh/viken/OBSERVATION?from=2017100100&too=2018032000 HTTP/1.1\r\nHost: oceandata.smhi.se\r\nConnection:close\r\n\r\n";
saeed's avatar
saeed committed
20
  char message[]   = "GET /ssh/goteborg/OBSERVATION?from=2017030100&too=2018030100 HTTP/1.0\r\nHost: oceandata.smhi.se\r\n\r\n";
saeed's avatar
saeed committed
21
22
23
24
  int M = sizeof(message);
  //printf("%s\n",message);
  //printf("%d\n",M);

saeed's avatar
saeed committed
25
26
  struct hostent *server;
  struct sockaddr_in serv_addr;
saeed's avatar
saeed committed
27
28
  int sockfd, bytes;//, received;
  //printf("Request:\n%s\n",message);
saeed's avatar
saeed committed
29

saeed's avatar
saeed committed
30
31
32
33
  //for (int i=0; i < (N-1); i++){
  //   message[i] = *message_fmt;
  //  if (*message_fmt != '\0') message_fmt++;
  //}
saeed's avatar
saeed committed
34
35
  /* create the socket */
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
saeed's avatar
saeed committed
36
37
38
39
  if (sockfd < 0){
    printf("ERROR opening socket\n");
    return 1;
  }
saeed's avatar
saeed committed
40
41
  /* lookup the ip address */
  server = gethostbyname(host);
saeed's avatar
saeed committed
42
43
44
45
46
47
48
49
50
51
52
  if (server == NULL){
    printf("ERROR, no such host server NULL\n");
    return 1;
  }
    /* fill in the structure */
  //memset(&serv_addr,0,sizeof(serv_addr));

  char *cs =  (char *)&serv_addr;
  for (int i=0; i < sizeof(serv_addr); ++i) {
    *(cs+i) = 0;
  }
saeed's avatar
saeed committed
53
54
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(portno);
saeed's avatar
saeed committed
55
56
57
58
59
60
61
  //memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);
  char *cb =  (char *)server->h_addr;
  char *ca =  (char *)&serv_addr.sin_addr.s_addr;
  for (int i=0; i<server->h_length; ++i){
    *(ca +i) = *(cb + i);
  }
  
saeed's avatar
saeed committed
62
  /* connect the socket */
saeed's avatar
saeed committed
63
64
65
66
  if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0){
    printf("ERROR connecting");
    return 1;
  }
saeed's avatar
saeed committed
67
68

  /* send the request */
saeed's avatar
saeed committed
69
  send(sockfd,message,M,0);
saeed's avatar
saeed committed
70
71
72
73
74
75
76
77
78

  //  Once you get that part working, your recv() logic is not parsing the server's response at all, which I assume is you simply not having gotten that far yet. But more importantly, the data being received is not null-terminated, but your call to printf() after recv() assumes that it is. You need to fix that as well, either like this:

  //int byte_count = recv(sockfd,response,sizeof(response)-1,0); // <-- -1 to leave room for a null terminator
  //response[byte_count] = 0; // <-- add the null terminator
  //printf("recv()'d %d bytes of data in buf\n",byte_count);
  
  /* close the socket */

saeed's avatar
saeed committed
79
80
  //memset(response, 0, sizeof(response));
  //total = sizeof(response)-1;
saeed's avatar
saeed committed
81
82
83
84
85

  FILE *fl = fopen("output.dat","w+");

  //received = 0;
  //bytes = 0;
saeed's avatar
saeed committed
86
  while((bytes = recv(sockfd, response, N, 0)) > 0){
saeed's avatar
saeed committed
87
    // HANDLE RESPONSE CHUCK HERE BY, FOR EXAMPLE, SAVING TO A FILE.
saeed's avatar
saeed committed
88
    //memset(response, 0, sizeof(response));
saeed's avatar
saeed committed
89
    fprintf(fl,"%s",response);
saeed's avatar
saeed committed
90
    for (int i=0; i < N; ++i) {
saeed's avatar
saeed committed
91
      *(response+i) = 0;
saeed's avatar
saeed committed
92
    }
saeed's avatar
saeed committed
93
94
    //bytes = recv(sockfd, response, N-1, 0);
    //if (bytes > 0 ){
saeed's avatar
saeed committed
95
    //   response[bytes] = '\0';
saeed's avatar
saeed committed
96
97
      //printf("%s", response);

saeed's avatar
saeed committed
98
99
100
    //}
    //if (bytes < 0 )return 1;
    //if (bytes == 0)break;
saeed's avatar
saeed committed
101
102
    //received+=bytes;
    //printf("here \n");
saeed's avatar
saeed committed
103
  }; //while(1); 
saeed's avatar
saeed committed
104

saeed's avatar
saeed committed
105
  //close(sockfd);
saeed's avatar
saeed committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  //fclose(fl);
  
  //fl = fopen("output.dat","r");

  fseek(fl,0,SEEK_END);

  long int numc = ftell(fl);

  fseek(fl,0,SEEK_SET);

  char *content = (char *)malloc(numc * sizeof(char)); 

  char cf = '0';

  long int il = 0L;
  while((cf=fgetc(fl)) != EOF){
    *(content+il) = cf;
    il += 1;
  }
  
saeed's avatar
saeed committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  fclose(fl);

  int ir = 0;

  for (long int i=0L; i < numc;++i){
    
    if (content[i] == '\r') ir =i;
  }

  //printf("%c\n",content[ir]);
  //printf("%c\n",content[ir+1]);
  
  //for (int i=(ir+3); i < (numc-1);++i){
  //  printf("%c",content[i]);
  //}
saeed's avatar
saeed committed
141
  //printf("\n");
saeed's avatar
saeed committed
142

saeed's avatar
saeed committed
143
  //printf("%ld\n",numc - ir - 2);
saeed's avatar
saeed committed
144
145
146
147
148

  char *st = &content[ir+3];

  int icom = 0, iicom = 0; 
  char **cout =split(st, ',', &icom);
saeed's avatar
saeed committed
149
150
151
152
  if ( icom > 1){ 
    char **ccout  = NULL;
    int   *atime = (int *)malloc(sizeof(int)*icom);
    float *adata = (float *)malloc(sizeof(float)*icom);
saeed's avatar
saeed committed
153
  
saeed's avatar
saeed committed
154
155
156
157
158
    for (int i=0; i < (icom); ++i){ 
      ccout  = split(cout[i], ':', &iicom);
      rtrim(ccout[2],'}');
      ltrim(ccout[0],'\"');
      rtrim(ccout[0],'\"');
saeed's avatar
saeed committed
159
    //printf("%d\t%s\t%f\t%f\n",stoi(ccout[0]),ccout[2],atof(ccout[2]),stof(ccout[2]));
saeed's avatar
saeed committed
160
161
162
163
164
165
166
      //printf("%d\t%f\n",stoi(ccout[0]) ,stof(ccout[2]));
      atime[i] = stoi(ccout[0]);
      adata[i] = stof(ccout[2]); 
      free(ccout);
    }
  
  
saeed's avatar
saeed committed
167
168
#if sort
    sortfull(atime,adata,icom);
saeed's avatar
saeed committed
169
170
#endif
  
saeed's avatar
saeed committed
171
172
173
174
175
176
  for (int i=0; i < (icom); ++i){
    printf("%d\t%f\n",atime[i] ,adata[i]);
  }

  free(atime);
  free(adata);
saeed's avatar
saeed committed
177
  }
saeed's avatar
saeed committed
178
  //printf("%s",content);
saeed's avatar
saeed committed
179
  
saeed's avatar
saeed committed
180
181
182
  free(content);
  free(cout);

saeed's avatar
saeed committed
183
    
saeed's avatar
saeed committed
184
185
186
  /* process response */
  //printf("Response:\n%s\n",response);

saeed's avatar
saeed committed
187
  
saeed's avatar
saeed committed
188

saeed's avatar
saeed committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  return 0;
}