|
|
|
I wanna change this C code from persistent to none persistent in HTML
please help me i don't know about that
<pre lang="c++"> int main(int argc, char *argv[])
{
struct sockaddr_in server_addr, client_addr;
socklen_t sin_len = sizeof(client_addr);
int fd_server, fd_client;
char buf[2048];
int fdimg;
fd_server = socket(AF_INET, SOCK_STREAM, 0);
if(fd_server < 0)
{
perror("socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(8080);
if(bind(fd_server, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1)
{
perror("bind");
close(fd_server);
exit(1);
}
if(listen(fd_server,10) == -1)
{
perror("listen");
close(fd_server);
exit(1);
}
while(1)
{
fd_client = accept(fd_server, (struct sockaddr *) &client_addr, &sin_len);
if(fd_client == -1)
{
perror("Connection Failed! Can't Conneect to Client .... \n");
continue;
}
printf("Accepted the Client Connection ..... \n");
if(!fork())
{
close(fd_server);
memset(buf, 0, 2047);
read(fd_client, buf, 2047);
printf("%s\n", buf);
if(!strncmp(buf, "GET /testicon.ico", 16))
{
fdimg = open("testicon.ico", O_RDONLY);
sendfile(fd_client, fdimg, NULL, 200000);
close(fdimg);
}
else if(!strncmp(buf, "GET /testpic.jpg", 16))
{
fdimg = open("testpic.jpg", O_RDONLY);
sendfile(fd_client, fdimg, NULL, 60000);
close(fdimg);
}
else
write(fd_client, webpage, sizeof(webpage)-1);
close(fd_client);
printf("Closing ... \n");
exit(0);
}
close(fd_client);
}
return 0;
}
|
|
|
|
|
What exactly do you mean by "change this C code from persistent to none persistent in HTML ". Your code looks to be a simple HTTP message handler.
|
|
|
|
|
Your server code doesn't even appear to be sending a header at the moment, so client will assume you are http 1.0 server and default of close connection.
Look at the rosseta code for a Web server .. goto the C section .. you should recognize the code
Hello world/Web server - Rosetta Code[^]
The moment it connects to the client it sends a header to the client
write(client_fd, response, sizeof(response) - 1);
The header is at top of code and looks like this
char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n\r\n"
"<!DOCTYPE html><html><head><title>Bye-bye baby bye-bye</title>"
"<style>body { background-color: #111 }"
"h1 { font-size:4cm; text-align: center; color: black;"
" text-shadow: 0 0 2mm red}</style></head>"
"<body><h1>Goodbye, world!</h1></body></html>\r\n";
So they are sending a header telling client the server is HTML 1.1 and will have persistent connections.
The alternative is to send a 1.0 header with the keep-alive tag
Then you simply don't close the connection until it times out. It's designed as a start point you need to
flesh out the rest from there.
In vino veritas
modified 17-Oct-18 10:16am.
|
|
|
|
|
My app is C++ based (no .net) and I need to know where on earth my app is started. This is in order to access a website (e.g. www.xyz.cn/info for china; www.xyz.com/info for the rest) with the correct region.
I've tried with ILocation Com interface but it tells me that there are 'no devices detected'.
Any thaughts?
|
|
|
|
|
jung-kreidler wrote: 've tried with ILocation Com interface but it tells me that there are 'no devices detected'.
What method of ILocation gives you this error?
|
|
|
|
|
spLocation->GetReportStatus(IID_ILatLongReport, &status) returns REPORT_NOT_SUPPORTED.
The code is from one of the Microsoft samples.
|
|
|
|
|
Well, from MSDN:
Quote: REPORT_NOT_SUPPORTED
The requested report type is not supported by the API. No location providers of the requested type are installed.
Does the PC have any means to locate GPS posituion?
|
|
|
|
|
No, this is a normal PC without GPS.
I just need the country where my app was started in order to access the right webaddress provided for the country.
|
|
|
|
|
Then you need some device or some tool giving you current PC geo-coordinates....
|
|
|
|
|
As I do only need the country where my app was started the following code does the job:
std::string get_country_code()
{
HINTERNET net = InternetOpen("IP retriever",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL,
NULL,
0);
HINTERNET conn = InternetOpenUrl(net,
"http://ip-api.com/csv/?fields=countryCode",
NULL,
0,
INTERNET_FLAG_RELOAD,
0);
char buffer[12096];
DWORD read;
InternetReadFile(conn, buffer, sizeof(buffer) / sizeof(buffer[0]), &read);
InternetCloseHandle(net);
return std::string(buffer, read);
}
|
|
|
|
|
G lad you have solved your problem!
|
|
|
|
|
Hi
Below code does some memory corruption due to which access violation happens at sub subsequent steps
If I remove this block of code every thing is working fine. Unable to figure out the issue in the below code. Please help
while ((int)tax % 10 != 0)
{
tax *= 10;
decimals++;
}
decimalpoints = decimals-1
|
|
|
|
|
There is nothing in that code that will cause memory corruption, unless decimals and decimalpoints are pointers to a block of memory and go beyond its limit. You need to show some more of your code.
|
|
|
|
|
Both are not pointers. one issue I found with this is the loop iterates for 19 times for the value of tax = 8.995.
by end of the loop decimal = 19.
I except decimal = 3 by end of the loop
|
|
|
|
|
You are unlikely to get a valid answer doing it like that. You should use something like sprintf to convert the double value to a string and set the actual number of decimal places that you want in the format string.
|
|
|
|
|
This is not going to work as you might expect. For example, if tax has an initial value of 8.0 , it may actually be stored in memory as 8.00000000001 or something similar.
What exactly are you trying to accomplish? A better solution may be just an explanation away.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I want to find the number of decimal point. for example 8.995 is the tax amount. I want to count number of digits after decimal point; in this case I want to get 3
|
|
|
|
|
I want to find the number of decimal point. for example 8.995 is the tax amount. I want to count number of digits after decimal point; in this case I want to get 3
|
|
|
|
|
ptr_Electron wrote: for example 8.995 is the tax amount. Which could be stored in memory as 8.9949999999999992 or something similar. Then you would get 16 (or maybe 15 , I forget) as an answer.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
modified 16-Oct-18 9:34am.
|
|
|
|
|
May I know the correct approach please.
|
|
|
|
|
It's still unclear as to what you are trying to accomplish. Once you have the number of digits in the mantissa, then what?
Again, if you would provide details for your end-game, we may be able to offer a better solution. Manipulating floating point numbers on a binary computer is not a trivial task.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Thanks for response. a given decimal number. I would the count of decimal points.
For example
6.67 -? I need 2
6.9876 I need 4
1.67890 I need 5
Count of digits after decimal point
|
|
|
|
|
ptr_Electron wrote:
6.67 -? I need 2
6.9876 I need 4
1.67890 I need 5 Which are stored in memory as:
6.6699999999999999
6.9875999999999996
1.6789000000000001 or something very similar. Notice all the mantissas are the same length?
Is this just a trivial exercise for you, or are you ultimately planning on doing something with the result?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
modified 16-Oct-18 15:02pm.
|
|
|
|