Commit 59304851 authored by saeed's avatar saeed
Browse files

a good version of getobs.c

parent 21cd5887
#include <stdio.h> /* printf, sprintf */
#include <unistd.h> /* read, write, close */
#include <stdlib.h> /* printf, sprintf */
#include <sys/socket.h> /* socket, connect */
#include <netinet/in.h> /* struct sockaddr_in, struct sockaddr */
#include <netdb.h> /* struct hostent, gethostbyname */
#include"strcomp.h"
#define N (1024)
int main(int argc,char *argv[])
{
//http://oceandata.smhi.se/ssh/"+station+"/OBSERVATION",params = param
char response[N] = {0};
int portno = 80;
char *host = "oceandata.smhi.se";
char *message_fmt = "GET /ssh/viken/OBSERVATION?from=2017100100&too=2018032000 HTTP/1.0\r\nHost: oceandata.smhi.se\r\n\r\n";
//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";
char message[] = "GET /ssh/viken/OBSERVATION?from=2017100100&too=2018032000 HTTP/1.0\r\nHost: oceandata.smhi.se\r\n\r\n";
int M = sizeof(message);
//printf("%s\n",message);
//printf("%d\n",M);
struct hostent *server;
struct sockaddr_in serv_addr;
int sockfd, bytes, received;
//int total, sent;
char response[N] = {0};
char message[N] = {0};
printf("Request:\n%s\n",message_fmt);
for (int i=0; i < (N-1); i++){
message[i] = *message_fmt;
if (*message_fmt != '\0') message_fmt++;
}
message[N-1] = '\0';
printf("%s\n",message);
int sockfd, bytes;//, received;
//printf("Request:\n%s\n",message);
//for (int i=0; i < (N-1); i++){
// message[i] = *message_fmt;
// if (*message_fmt != '\0') message_fmt++;
//}
/* create the socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0){
......@@ -45,13 +44,11 @@ int main(int argc,char *argv[])
char *cs = (char *)&serv_addr;
for (int i=0; i < sizeof(serv_addr); ++i) {
printf("%p\n",cs+i);
*(cs+i) = 0;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
//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){
......@@ -65,7 +62,7 @@ int main(int argc,char *argv[])
}
/* send the request */
send(sockfd,message,N,0);
send(sockfd,message,M,0);
// 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:
......@@ -77,8 +74,11 @@ int main(int argc,char *argv[])
//memset(response, 0, sizeof(response));
//total = sizeof(response)-1;
received = 0;
bytes = 0;
FILE *fl = fopen("output.dat","w+");
//received = 0;
//bytes = 0;
do {
// HANDLE RESPONSE CHUCK HERE BY, FOR EXAMPLE, SAVING TO A FILE.
//memset(response, 0, sizeof(response));
......@@ -87,22 +87,47 @@ int main(int argc,char *argv[])
}
bytes = recv(sockfd, response, N-1, 0);
response[N-1] = 0;
//if (bytes > 0 ){
if (bytes > 0 ){
// response[bytes] = '\0';
printf("%s", response);
//}
//printf("%s", response);
fprintf(fl,"%s",response);
}
if (bytes < 0 )return 1;
if (bytes == 0)break;
//received+=bytes;
//printf("here \n");
} while(1);
//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;
}
printf("%s",content);
//close(sockfd);
printf("\n");
/* process response */
//printf("Response:\n%s\n",response);
fclose(fl);
return 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment