I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly!
I wouldn't be surprised if my problems have something to do with my still existing confusion over arrays and pointers in C since I'm still a newbie, but anyhow, here's the code: -
#include <time.h>
-
#include <stdio.h>
-
-
#define WEBBUF_SIZE 32768
-
-
int webbuflen;
-
char webbuf[WEBBUF_SIZE];
-
-
static char OKheader[] = "HTTP/1.1 200 OK\r\n";
-
static char texthtmlheader[] = "Connection: close\r\nContent-Type: text/html\r\n";
-
static char contentLenheader[] = "Content-Length: ";
-
-
static char data[] = "<html><head><title>Testing</title></head><body>Test,test,test, testing, test.</body></html>\n";
-
-
int main()
-
{
-
http_response();
-
return 0;
-
}
-
-
int getTimeStamp(unsigned char* timestring)
-
{
-
struct tm *ptr;
-
time_t tm;
-
tm = time(NULL);
-
ptr = localtime(&tm);
-
strftime(timestring ,100 , "Date: %a, %d %b %Y %H:%M:%S GMT\r\n",ptr);
-
return 0;
-
}
-
-
int http_response()
-
{
-
char tmp[10];
-
char datestring[60];
-
char header[1048];
-
-
//Get content-length (length of data) as a string
-
sprintf(tmp,"%d%s",(strlen(data)+1),"\r\n\r\n");
-
strcat(contentLenheader,tmp);
-
-
getTimeStamp(datestring);
-
-
//write everything to header
-
sprintf(header, "%s%s%s%s", OKheader,datestring, texthtmlheader,contentLenheader);
-
-
//Appending data to header
-
strcat(header,data);
-
-
//Adding header to webbuf
-
webbuflen = sprintf(webbuf, "%s",header);
-
-
printf("\nWebbuflen is now: %d",webbuflen);
-
printf("\nAnd webbuf is: \n\n%s",webbuf);
-
-
return 0;
-
}
I get this printout when I run this program:
Webbuflen is now: 123
And webbuf is:
HTTP/1.1 200 OK
Date: Fri, 22 Feb 2008 09:14:59 GMT
Connection: close
Content-Type: text/html
Content-Length: 93
But I would like to get:
HTTP/1.1 200 OK
Date: Fri, 22 Feb 2008 09:14:59 GMT
Connection: close
Content-Type: text/html
Content-Length: 93
<html><head><title>Testing</title></head><body>Test,test,test, testing, test.</body></html>
Any help or pointers in the right direction is greatly appreciated!
Cheers!
4 3048
I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly!
I wouldn't be surprised if my problems have something to do with my still existing confusion over arrays and pointers in C since I'm still a newbie, but anyhow, here's the code: -
#include <time.h>
-
#include <stdio.h>
-
-
#define WEBBUF_SIZE 32768
-
-
int webbuflen;
-
char webbuf[WEBBUF_SIZE];
-
-
static char OKheader[] = "HTTP/1.1 200 OK\r\n";
-
static char texthtmlheader[] = "Connection: close\r\nContent-Type: text/html\r\n";
-
static char contentLenheader[] = "Content-Length: ";
-
-
static char data[] = "<html><head><title>Testing</title></head><body>Test,test,test, testing, test.</body></html>\n";
-
-
int main()
-
{
-
http_response();
-
return 0;
-
}
-
-
int getTimeStamp(unsigned char* timestring)
-
{
-
struct tm *ptr;
-
time_t tm;
-
tm = time(NULL);
-
ptr = localtime(&tm);
-
strftime(timestring ,100 , "Date: %a, %d %b %Y %H:%M:%S GMT\r\n",ptr);
-
return 0;
-
}
-
-
int http_response()
-
{
-
char tmp[10];
-
char datestring[60];
-
char header[1048];
-
-
//Get content-length (length of data) as a string
-
sprintf(tmp,"%d%s",(strlen(data)+1),"\r\n\r\n");
-
strcat(contentLenheader,tmp);
-
-
getTimeStamp(datestring);
-
-
//write everything to header
-
sprintf(header, "%s%s%s%s", OKheader,datestring, texthtmlheader,contentLenheader);
-
-
//Appending data to header
-
strcat(header,data);
-
-
//Adding header to webbuf
-
webbuflen = sprintf(webbuf, "%s",header);
-
-
printf("\nWebbuflen is now: %d",webbuflen);
-
printf("\nAnd webbuf is: \n\n%s",webbuf);
-
-
return 0;
-
}
I get this printout when I run this program:
Webbuflen is now: 123
And webbuf is:
HTTP/1.1 200 OK
Date: Fri, 22 Feb 2008 09:14:59 GMT
Connection: close
Content-Type: text/html
Content-Length: 93
But I would like to get:
HTTP/1.1 200 OK
Date: Fri, 22 Feb 2008 09:14:59 GMT
Connection: close
Content-Type: text/html
Content-Length: 93
<html><head><title>Testing</title></head><body>Test,test,test, testing, test.</body></html>
Any help or pointers in the right direction is greatly appreciated!
Cheers!
Dear:MimiMi
Increasing your contentLenheader[] size.Then you will find out your answer.Good luck :)
Dear:MimiMi
Increasing your contentLenheader[] size.Then you will find out your answer.Good luck :)
Thank you so much! That solved the problem!
I still don't understand why though.. Do you (or anybody else) mind trying to explain why my solution didn't work, please :) ?
Thank you so much! That solved the problem!
I still don't understand why though.. Do you (or anybody else) mind trying to explain why my solution didn't work, please :) ?
Dear:MimiMi
First,you need to know how array initiated in C.
Initiated automatically by compiler.It's look like your
solution.Or declare array by yourself.
What's difference between them ???
Then research strcat() function,and you will getting
something wrong on line 39 in your codes.
Finally,I am glad to answer your question.It's also help
me to practice me English.ha ha~~ :)
(Wish you understand what I say) 冏rz
Hi and thanks again for taking the time! I think I understand it a little bit better now, so again, thank you!
Cheers!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ian Stanley |
last post by:
Hi,
Having not done any C programming for a while I am trying to get back into
it by converting an old java assignment into C.
The problem is I am getting a segmentation fault at runtime which I...
|
by: Ian Stanley |
last post by:
Hi,
Continuing my strcat segmentation fault posting-
I have a problem which occurs when appending two sting literals using
strcat.
I have tried to fix it by writing my own function that does the...
|
by: Nicolas |
last post by:
On most implementations of the standard C library, the functions that
copy characters in a previously allocated buffer, like strcpy or strcat,
are returning the pointer to that buffer.
On my...
|
by: Stegano |
last post by:
I am learning C Programming after working with Java for 5 years. I
want to know where can I find the source files for C language itself.
For example strcat is a function, which concatenates two...
|
by: diego.arias.vel |
last post by:
Hi all
I have certain problem when I'm doing this:
void copy(char *filename)
{
char *log_file;
log_file=filename;
strcat(log_file,"-log.txt");
|
by: ramubdvt |
last post by:
hi,
i have written this strcat but sometime it is giving problem, while
handeling some strings containing
binary and if string containing zero ,
funtion which takes string 1 and its length...
|
by: Neal Barney |
last post by:
I have a C program which runs on a device using a Zilog Z180
microprocessor. While it can address 1MB of RAM, it can only address
64KB at any given time. And of that only 16KB can be used for...
|
by: gyan |
last post by:
Hi All,
I am using sprintf and getting starnge output in following case
char temp_rn;
memset(temp_rn,'\0',12);
sprintf(temp_rn,"0%s",temp_rn);
the final value in temp_rn is 00
how it...
|
by: insirawali |
last post by:
I have written this in C#.
I just need to convert it into C.
da problem comes when handling the String. In C the String doesn't
behave the same way as in C#.
it would be great help if...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| | |