Click here to Skip to main content
15,891,884 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello

I'm trying to display an image file (.jpg) using the snorkel web-server, by using the GET method, but its not displaying the image.
all I get is a very small box with an X inside of it.

so, I was wondering do I need to use MIME or what shall I do to display my image?

here's my code:

C++
#pragma hdrstop
#pragma argsused

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

call_status_t
view_image(snorkel_obj_t http,
			snorkel_obj_t outstream)
  {

	snorkel_printf (outstream, "<html><body><img src = QR4.jpg>" 
								                           "</body></html>\r\n");
	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 = 8090;
  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, 5, NULL);      
  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, view_image) != 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);
		 }



  snorkel_obj_set(http, snorkel_attrib_mime, "jpg",
				  "image/jpeg",
				  contenttype_text,
				  view_image);
 /*
  *
  * 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] good bye\n");

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



can you please help me:confused:
Best Regards.
Rania
Posted
Comments
enhzflep 12-Jan-15 14:46pm    
Snorkel is a project authored by a member at CodeProject, Walter E. Capers. There's an article about it here at CP.
You can find a couple of article regarding it here: Using C and an embedded web server to provide browser based source editing
and here: The Quick and Easy Way to Add Web Interfaces to C/C++ Applications

Asking the author a question in the comment section of either article may well be your best bet. :)

1 solution

There is sample code in this article for displaying an image. You should be able to modify it for JPEG.

How To Add Simple Web-enabled 2D/3D Dashboards to your Natively Built Application[^]
 
Share this answer
 

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