News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

How to show image from local directory into WebBrowser (via socket)?

Started by Dinadan Dinadan, March 30, 2014, 02:54:41 PM

Previous topic - Next topic

Dinadan Dinadan

Hi,

I used C to code this.
I though it's not too much different with MASM, right?  :biggrin:

In the following PHP' code, I can show image (from my local directory [C:\xampp\htdocs\key.bmp]) into my WebBrowser (Internet Explorer)
echo "<img src='C:\\xampp\\htdocs\\key.bmp'><br>";

I want to do the same thing but with another way.
I wrote the following C' code.
I think I already written it correctly, but the image isn't appear when I open my WebBrowser (Internet Explorer) in localhost:8888
I have a friend who has same problem in here:
http://stackoverflow.com/questions/18425167/how-do-i-send-a-jpg-image-from-my-http-server-over-to-the-browser
Do you think what's wrong with my code?


#include<io.h>
#include<stdio.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
    WSADATA wsa;
    SOCKET s , new_socket;
    struct sockaddr_in server , client;
    int c;
    char *message;

    printf("\nInitialising Winsock...");
    if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
    {
        printf("Failed. Error Code : %d",WSAGetLastError());
        return 1;
    }
     
    printf("Initialised.\n");
     
    //Create a socket
    if((s = socket(PF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
    {
        printf("Could not create socket : %d" , WSAGetLastError());
    }

    printf("Socket created.\n");
     
    //Prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );
     
    //Bind
    if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
    {
        printf("Bind failed with error code : %d" , WSAGetLastError());
    }
     
    puts("Bind done");

    //Listen to incoming connections
    listen(s , 3);
     
    //Accept and incoming connection
    puts("Waiting for incoming connections...");
     
    c = sizeof(struct sockaddr_in);
    new_socket = accept(s , (struct sockaddr *)&client, &c);
    if (new_socket == INVALID_SOCKET)
    {
        printf("accept failed with error code : %d" , WSAGetLastError());
    }
     
    while(1)
    {
       new_socket = accept(s , (struct sockaddr *)&client, &c);
       puts("Connection accepted");

       //Reply to client
       message =
                "HTTP/1.1 200 OK\n"
                "Date: Thu, 19 Feb 2009 12:27:04 GMT\n"
                "Server: Apache/2.2.3\n"
                "Last-Modified: Wed, 18 Jun 2003 16:05:58 GMT\n"
                "ETag: \"56d-9989200-1132c580\"\n"
                "Content-Type: text/html\n"
                "Content-Transfer-Encoding: binary\n"
                "Content-Length: 38\n"
                "\n"
                "<img src='C:\\xampp\\htdocs\\key.bmp'>";
       send(new_socket , message , strlen(message) , 0);

       //Receive from client
       char recvbuf[1024];
       int iResult;
       int recvbuflen = 1024;
       iResult = recv(new_socket, recvbuf, 1024, 0);
       printf("%s\r\n\r\n",recvbuf);
    }   
    getchar();

    closesocket(s);
    WSACleanup();
     
    return 0;
}

TouEnMasm


The best way to send an image to the webbrowser is ATL followed by the MSHTML interfaces.See msdn for that.
Fa is a musical note to play with CL

Dinadan Dinadan

Hi ToutEnMasm ,

Yes, I know it.
But what I want to know is: how to do it by socket?  :biggrin:

Dinadan Dinadan

Hi,

I got this work.
I guess it's only one step again.
How to put it on text/html, so I can change content-type in the following code into text/html and it's image also appear like
"<img src='C:\\xampp\\htdocs\\key.bmp'>"
Thank you.


//Reply to client
       FILE *f_in;                    // these are the i/o file handles
       unsigned long Size;
       f_in = fopen("image.bmp", "rb");
       if (!f_in)
       {
          printf("fopen failed\n");
       }
       fseek (f_in, 0, SEEK_END);
       Size = ftell(f_in);
       fseek(f_in,0,SEEK_SET);
       char Buffer[Size];            //Buffer = new char[Size];
       fread(Buffer, Size, 1, f_in);
       char cSize[MAX_PATH];
       sprintf(cSize, "%i", Size);
       fclose(f_in);
       message =
                "HTTP/1.1 200 OK\n"
                "Date: Thu, 19 Feb 2009 12:27:04 GMT\n"
                "Server: Apache/2.2.3\n"
                "Last-Modified: Wed, 18 Jun 2003 16:05:58 GMT\n"
                "ETag: \"56d-9989200-1132c580\"\n"
                "Content-Type: image/bmp\n"     [b]//-> how to change this into text/html and still show the image?[/b]
                "Content-Transfer-Encoding: binary\n"
                "Content-Length: 8688\n"        //-> 8688 is file size of key.bmp
                "\n";
       send(new_socket, message, strlen(message), 0); // Header
       send(new_socket, Buffer, Size, 0);             // File Binary
       free(Buffer);


TWell

for example ?
//Reply to client
if (iResult > 6 && !strncmp("GET / ", recvbuf, 6))
{
message = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n";
int len = strlen(message);
send(new_socket, message, len, 0);
//
char *page = "<html>text<img src=\"test.jpg\"></html>\r\n";
len = strlen(page);
int len2 = sprintf(recvbuf, "Content-Length: %d\r\n", len);
send(new_socket, recvbuf, len2, 0);
send(new_socket, "\r\n", 2, 0); // end of Request header
send(new_socket, page, len, 0);
}
else if (iResult > 14 && !strncmp("GET /test.jpg ", recvbuf, 14))
{
FILE *f_in; // these are the i/o file handles
unsigned long nSize;
f_in = fopen("test.jpg", "rb");
if (f_in)
{
fseek(f_in, 0, SEEK_END);
nSize = ftell(f_in);
fseek(f_in, 0, SEEK_SET);
char *Buffer = malloc(nSize);
fread(Buffer, nSize, 1, f_in);
fclose(f_in);
message = "HTTP/1.1 200 OK\r\n"
"Content-Type: image/jpeg\r\n";
int len = strlen(message);
send(new_socket, message, len, 0);
//
len = sprintf(recvbuf, "Content-Length: %d\r\n", nSize);
send(new_socket, recvbuf, len, 0);
send(new_socket, "\r\n", 2, 0); // end of Request header
send(new_socket, Buffer, nSize, 0);    // File Binary
free(Buffer);
}
}
closesocket(new_socket);

EDIT:
Quote//Accept and incoming connection
    puts("Waiting for incoming connections...");
     
    c = sizeof(struct sockaddr_in);
    new_socket = accept(s , (struct sockaddr *)&client, &c);
    if (new_socket == INVALID_SOCKET)
    {
        printf("accept failed with error code : %d" , WSAGetLastError());
    }
     
    while(1)
    {
       new_socket = accept(s , (struct sockaddr *)&client, &c);
       if (new_socket == INVALID_SOCKET) break;
       puts("Connection accepted");
       //Receive from client
       char recvbuf[1024];
       int iResult;
       int recvbuflen = 1024;
       iResult = recv(new_socket, recvbuf, 1024, 0);
       printf("%s\r\n\r\n",recvbuf);



Dinadan Dinadan

Hi TWell,

I already do that before in my code.
And it's sill show blank image like the attached file
Thank you.

Quote from: TWell on April 01, 2014, 01:58:37 AM
for example ?

//Reply to client
...
...
message = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n";
int len = strlen(message);
send(new_socket, message, len, 0);
//
char *page = "<html>text<img src=\"test.jpg\"></html>\r\n";
len = strlen(page);
int len2 = sprintf(recvbuf, "Content-Length: %d\r\n", len);
send(new_socket, recvbuf, len2, 0);
send(new_socket, "\r\n", 2, 0); // end of Request header
send(new_socket, page, len, 0);
...
...
...
...
...
[/quote]