Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello,
I've developed an embedded web-server using "Snorkel API" (http://snorkelembedded.webs.com)
in C++, and generated a QR code based on information submitted from the client (browser).

Now I'm trying to display the dynamic QR code (which is saved in a .jpg file) in the client-browser, but it's not working. The Snorkel embedded web-server is based on defining the HTML code within the C++ code as follows:

C++
#pragma hdrstop
#pragma argsused

#include <snorkel.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>

call_status_t index_htm(snorkel_obj_t http,     /*read environment from this object */
			snorkel_obj_t outstream)  /* write data to the output stream   */
  {
   if (snorkel_printf (outstream,"<html><body><img src="QR4.jpg" alt="QR code" width=178 height=178</body></html>\r\n")== SNORKEL_ERROR)

		 return HTTP_ERROR;

   return HTTP_SUCCESS;
   }

void syntax (char *pszProg)
	{
	 fprintf (stderr, "syntax error:\n");
	 fprintf (stderr, "%s [-p <port>]\n", pszProg);
	 exit (1);
	 }

void main (int argc, char *argv[])
 {
  int i = 1;
  int port = 80;
  snorkel_obj_t http = 0;
  char szExit[10];
  for (; i < argc; i++)
  {
   if (argv[i][0] == '-' || argv[i][0] == '/')
	 {
	  char carg = argv[i][1];
	  switch (carg)
	   {
		case 'p':          /* port number */
		port = atoi (argv[i + 1]);
		i++;
		break;
		default:
		syntax (argv[0]);
		break;
		}
	  }
   }

/*
 *
 * initialize API
 *
 */
 if (snorkel_init () != SNORKEL_SUCCESS)
  {
	perror ("could not initialize snorkel\n");
	exit (1);
   }
/*
 *
 * create a server object
 *
 */
 http = snorkel_obj_create (snorkel_obj_server, 2,     /* number of handler threads to create */
								 NULL       /* directory containing index.html     */);

  if (!http)
   {
	 perror ("could not create http server\n");
	 exit (1);
   }

/*
 *
 * create a listener
 *
 */
 if (snorkel_obj_set (http,    /* server object */
					  snorkel_attrib_listener, /* attribute   */
					  port,    /* port number */
					  0 /* SSL support */ )
					  != SNORKEL_SUCCESS)
	  {
	   fprintf (stderr, "could not create listener\n");
	   snorkel_obj_destroy (http);
	   exit (1);
	  }

 /*
  *
  * overload the URL index.html
  *
  */

 if (snorkel_obj_set (http,    /* server object */
					 snorkel_attrib_uri,      /* attribute type */
					 GET,     /* method */
					  "/index.html",   /* url */
					  contenttype_text, index_htm) != SNORKEL_SUCCESS)
		 {
		   perror ("could not overload index.html");
		   snorkel_obj_destroy (http);
		   exit (1);
		  }

 if (snorkel_obj_set(http, snorkel_attrib_ipvers, IPVERS_IPV4, SOCK_SET) != SNORKEL_SUCCESS)
		{
		  fprintf (stderr, "error could not set ip version\n");
		  exit (1);
		 }

 /*
  *
  * start the server
  *
  */
  fprintf (stderr, "\n\n[HTTP] starting embedded server\n");
  if (snorkel_obj_start (http) != SNORKEL_SUCCESS)
   {
	perror ("could not start server\n");
	snorkel_obj_destroy (http);
	exit (1);
	}

/*
 *
 * do something while server runs
 * as a separate thread
 *
 */
  fprintf (stderr, "\n[HTTP] started.\n\n"
					 "--hit enter to terminate--\n");
  fgets (szExit, sizeof (szExit), stdin);

  fprintf (stderr, "[HTTP] bye\n");

 /*
  *
  * graceful clean up
  *
  */
  snorkel_obj_destroy (http);
  exit (0);
  } 


I don't know, maybe the problem is within the HTML code i'm writing.
Can some body pleeeeeeease help me, I'm trying to solve this problem for a week now.

Best Regards.
Rania
Posted
Comments
anup.bhunia 8-Apr-14 14:44pm    
from you code snippet i did not find where are you sending the image to browser, other than the <img> tag. You need to send the image resource on browser request or you could write the image content in the same http response object.
enhzflep 8-Apr-14 14:56pm    
Looks like the problem to me, too.
anup.bhunia 8-Apr-14 14:44pm    
let me know if i am missing something.
pasztorpisti 8-Apr-14 16:39pm    
First create your content and get it work with a HTTP server that is proven to work. You can easily start up a test HTTP server on your machine by installing python and saying python -m SimpleHTTPServer [optional_port_number] and this command starts a http server on your machine serving the content of the current directory. When you know that your html file and the images are okay then start implementing the C++ http server that serves the html+image that are good for sure. If you don't understand something from the previous sentences then start learning at least some basics about how the HTTP protocol and HTTP servers work before trying to implement a HTTP server.
raniam 9-Apr-14 3:26am    
hello,
the following code snippet works fine, all I have to do, is to run the c++ program, and open the browser and write http://localhost:80

#pragma hdrstop
#pragma argsused

#include <snorkel.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>

call_status_t index_htm(snorkel_obj_t http, /*read environment from this object */
snorkel_obj_t outstream) /* write data to the output stream */
{
if (snorkel_printf (outstream,"<html><body>

Dynamic Content"
"

</body></html>\r\n") == SNORKEL_ERROR)
//if (snorkel_printf (outstream,"<html><body><img src="QR4.jpg" alt="QR code" width=178 height=178</html>\r\n")== SNORKEL_ERROR)

return HTTP_ERROR;

return HTTP_SUCCESS;
}

void syntax (char *pszProg)
{
fprintf (stderr, "syntax error:\n");
fprintf (stderr, "%s [-p <port>]\n", pszProg);
exit (1);
}

void main (int argc, char *argv[])
{
int i = 1;
int port = 80;
snorkel_obj_t http = 0;
char szExit[10];
for (; i < argc; i++)
{
if (argv[i][0] == '-' || argv[i][0] == '/')
{
char carg = argv[i][1];
switch (carg)
{
case 'p': /* port number */
port = atoi (argv[i + 1]);
i++;
break;
default:
syntax (argv[0]);
break;
}
}
}

/*
*
* initialize API
*
*/
if (snorkel_init () != SNORKEL_SUCCESS)
{
perror ("could not initialize snorkel\n");
exit (1);
}
/*
*
* create a server object
*
*/
http = snorkel_obj_create (snorkel_obj_server, 2, /* number of handler threads to create */
NULL /* directory containing index.html */);

if (!http)
{
perror ("could not create http server\n");
exit (1);
}

/*
*
* create a listener
*
*/
if (snorkel_obj_set (http, /* server object */
snorkel_attrib_listener, /* attribute */
port, /* port number */
0 /* SSL support */ )
!= SNORKEL_SUCCESS)
{
fprintf (stderr, "could not create listener\n");
snorkel_obj_destroy (http);
exit (1);
}

/*
*
* overload the URL index.html
*
*/

if (snorkel_obj_set (http, /* server object */
snorkel_attrib_uri, /* attribute type */
GET, /* method */
"/index.html", /* url */
contenttype_text, index_htm) != SNORKEL_SUCCESS)
{
perror ("could not overload index.html");
snorkel_obj_destroy (http);
exit (1);
}

if (snorkel_obj_set(http, snorkel_attrib_ipvers, IPVERS_IPV4, SOCK_SET) != SNORKEL_SUCCESS)
{
fprintf (stderr, "error could not set ip version\n");
exit (1);
}

/*
*
* start the server
*
*/
fprintf (stderr, "\n\n[HTTP] starting embedded server\n");
if (snorkel_obj_start (http) != SNORKEL_SUCCESS)
{
perror ("could not start server\n");
snorkel_obj_destroy (http);
exit (1);
}

/*
*
* do something while server runs
* as a separate thread
*
*/
fprintf (stderr, "\n[HTTP] started.\n\n"
"--hit enter to terminate--\n");
fgets (szExit, sizeof (szExit), stdin);

fprintf (stderr, "[HTTP] bye\n");

/*
*
* graceful clean up
*
*/
snorkel_obj_destroy (http);
exit (0);
}

So, I just want to change the HTML code which displays : Dynamic Content in the browser, to displaying my image.jpg

Regards.
Rania

1 solution

XML
if you are still struggeling; try to work with following code snippet to write the image content(bytes) in the output stream;

<img src="QR4.jpg" alt="QR code" width="178" height="178">
//write the bytes from the image file
</img>
 
Share this answer
 
Comments
raniam 22-Apr-14 11:37am    
Hello,

I've tried it your way, but got lots of syntax errors.
but when I write it this way:
if (snorkel_printf (outstream, "<html><body>

<img src=<c:\users\rania\documents\images\qr4.jpg alt="QRcode" width="178" height="178">"
"

</body></html>\r\n")== SNORKEL_ERROR)
return HTTP_ERROR;

return HTTP_SUCCESS;
}
no errors, and I get the Image box, but with a black X inside of it, and QRcode written inside of it, but no image, I wonder why?

Regards
raniam 22-Apr-14 11:41am    
Hello again,
What do you mean by "writing the bytes from the image file"?

Regards.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900