HTTP Server Program

From Web (09.06.1997)

Table of Contents

1. A Visual C++ Project

Create a Project (Application) in Visual C++ (we have used MSVC 4.0)

The files in the project

  • a1.cpp
  • wsock32.lib

2. First Step - a simple window

//a1.cpp

#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
      FILE *fp=fopen("z.txt","a+");
      fprintf(fp,"%s\n",p);
      fclose(fp);
}
WNDCLASS a;HWND b;MSG c;

long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
      a.lpszClassName="a1";
      a.hInstance=i;
      a.lpfnWndProc=zzz;
      a.hbrBackground=GetStockObject(WHITE_BRUSH);
      RegisterClass(&a);
      b=CreateWindow("a1","aaa",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
      ShowWindow(b,3);
      while ( GetMessage(&c,0,0,0) )
            DispatchMessage(&c);
      return 1;
}

long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
  if ( x == WM_LBUTTONDOWN)
  {
        MessageBox(0,"end","end",0);
  }
  if ( x == WM_DESTROY)
      PostQuitMessage(0);
  return DefWindowProc(w,x,y,z);
}

3. Listen on port 80

//a1.cpp

#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
        FILE *fp=fopen("z.txt","a+");
        fprintf(fp,"%s\n",p);
        fclose(fp);
}
WNDCLASS a;HWND b;MSG c;char aa[100];struct sockaddr_in A,A1;
WSAData ws;long d;SOCKET s,s1;int dw;
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
        a.lpszClassName="a1";
        a.hInstance=i;
        a.lpfnWndProc=zzz;
        a.hbrBackground=GetStockObject(WHITE_BRUSH);
        RegisterClass(&a);
        b=CreateWindow("a1","HTTP server",WS_OVERLAPPEDWINDOW,
                                                1,1,10,20,0,0,i,0);
        ShowWindow(b,3);
        while ( GetMessage(&c,0,0,0) )
                DispatchMessage(&c);
        return 1;
}
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
        if ( x == WM_LBUTTONDOWN)
        {
            d=WSAStartup(0x0101,&ws);
            sprintf(aa,"WSAStartup = %ld",d);
            abc(aa);
            MessageBox(0,aa,aa,0);
            s=socket(AF_INET,SOCK_STREAM,0);
            sprintf(aa,"socket = %ld",s);
            abc(aa);
            MessageBox(0,aa,aa,0);
            A.sin_family=AF_INET;
            A.sin_port = htons(80);
            A.sin_addr.s_addr = INADDR_ANY;
            d=bind(s,(struct sockaddr *)&A,sizeof(A));
            sprintf(aa,"bind = %ld",d);
            abc(aa);
            MessageBox(0,aa,aa,0);
            WSAAsyncSelect(s,b,WM_USER+1,FD_ACCEPT);
            d=listen(s,100);
            sprintf(aa,"listen = %ld",d);
            abc(aa);
            MessageBox(0,aa,aa,0);
            MessageBox(0,"hi","hi",0);
        }
        if (x==WM_USER+1)
        {
                dw=sizeof(A1);
                s1=accept(s,(struct sockaddr *)&A1,&dw);
                sprintf(aa,"accept s1=%ld",s1);
                MessageBox(0,aa,aa,0);
                MessageBox(0,"In Accept","USER+1",0);
        }
        if ( x == WM_DESTROY)
                PostQuitMessage(0);
        return DefWindowProc(w,x,y,z);
}

4. Read datas from client

//a1.cpp

#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
        FILE *fp=fopen("z.txt","a+");
        fprintf(fp,"%s\n",p);
        fclose(fp);
}
WNDCLASS a;HWND b;MSG c;char aa[100],bb[100];struct sockaddr_in A,A1;
WSAData ws;long d;SOCKET s,s1;int dw;
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
        a.lpszClassName="a1";
        a.hInstance=i;
        a.lpfnWndProc=zzz;
        a.hbrBackground=GetStockObject(WHITE_BRUSH);
        RegisterClass(&a);
        b=CreateWindow("a1","HTTP server",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
        ShowWindow(b,3);
        while ( GetMessage(&c,0,0,0) )
                DispatchMessage(&c);
        return 1;
}
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
        if ( x == WM_LBUTTONDOWN)
        {
                d=WSAStartup(0x0101,&ws);
                sprintf(aa,"WSAStartup = %ld",d);
                abc(aa);
                MessageBox(0,aa,aa,0);
                s=socket(AF_INET,SOCK_STREAM,0);
                sprintf(aa,"socket = %ld",s);
                abc(aa);
                MessageBox(0,aa,aa,0);
                A.sin_family=AF_INET;
                A.sin_port = htons(80);
                A.sin_addr.s_addr = INADDR_ANY;
                d=bind(s,(struct sockaddr *)&A,sizeof(A));
                sprintf(aa,"bind = %ld",d);
                abc(aa);
                MessageBox(0,aa,aa,0);
                d=listen(s,100);
                sprintf(aa,"listen = %ld",d);
                abc(aa);
                MessageBox(0,aa,aa,0);
                WSAAsyncSelect(s,b,WM_USER+1,FD_ACCEPT|FD_READ);
                MessageBox(0,"hi from server","hi",0);
        }
        if (x==WM_USER+1)
        {
                if ( (LOWORD(z)&FD_ACCEPT) == FD_ACCEPT)
                {
                        dw=sizeof(A1);
                        s1=accept(s,(struct sockaddr *)&A1,&dw);
                        sprintf(aa,"accept s1=%ld",s1);
                        MessageBox(0,aa,aa,0);
                        char *p=inet_ntoa(A1.sin_addr);
                        sprintf(aa,"Addr..p=%s",p);
                        MessageBox(0,aa,aa,0);
                }
                if ( (LOWORD(z)&FD_READ) == FD_READ)
                {
                        recv(y,bb,1000,0);
                        sprintf(aa,"%d..",y);
                        MessageBox(0,bb,aa,0);
                        send(y,"Hello how are you \r\n",14,0);
                        closesocket(y);
                }
        }
        if ( x == WM_DESTROY)
                PostQuitMessage(0);
        return DefWindowProc(w,x,y,z);
}

5. Send HTTP-Header to client

//a1.cpp

#include <windows.h>
#include <winsock.h>
#include <stdio.h>
void abc(char *p)
{       FILE *fp=fopen("z.txt","a+");
        fprintf(fp,"%s\n",p);
        fclose(fp);
}
WNDCLASS a;HWND b;MSG c;char aa[200];char bb[20000];
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
{
        a.lpszClassName="a1";
        a.hInstance=i;
        a.lpfnWndProc=zzz;
        a.hbrBackground=GetStockObject(WHITE_BRUSH);
        RegisterClass(&a);
        b=CreateWindow("a1","aaa",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
        ShowWindow(b,3);
        while ( GetMessage(&c,0,0,0) )
                DispatchMessage(&c);
        return 1;
}
WSADATA ws;SOCKET s,s1,s2;sockaddr_in A,A1;int d,d1=200;
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
        if ( x == WM_USER+1)
        {
        if ( LOWORD(z)&FD_ACCEPT == FD_ACCEPT)
        {
            d=sizeof(A1);
            s1=accept(s,(struct sockaddr *) &A1,&d);
            sprintf(aa,"accept s1 %ld",s1);
            abc(aa);
        }
        if(LOWORD(z)&FD_READ == FD_READ)
        {
            d=recv(y,bb,2000,0);
            sprintf(aa,"recv %ld",d);
            abc(bb);
            strcpy(aa,"HTTP/1.0 200 OK\r\n" );
            strcat(aa,"Date: Sunday Sun, 21 Oct 1995 19:38:46 GMT\r\n");
            strcat(aa,"Server: Webster/1.0\r\n");
            strcat(aa,"MIME-version: 1.0\r\n");
            strcat(aa,"Content-type: text/plain\r\n");
            strcat(aa,"Last-modified: Sunday Sun, 21 Oct 1995 19:38:46 GMT\r\n");
            strcat(aa,"Content-length: 1000\r\n\r\n");
            strcat(aa,"<html>Hello<hr><hr><hi>");
            d=send(y,bb,strlen(bb),0);
            sprintf(aa,"send %ld",d);
            abc(bb);
        }
        MessageBox(0,"WM_USER","WM_USER",0);
        }
        if ( x == WM_LBUTTONDOWN)
        {
                d=WSAStartup(0x0101,&ws);
                sprintf(aa,"WSAStartup %ld",d);
                abc(aa);
                s=socket(AF_INET, SOCK_STREAM,0);
                sprintf(aa,"socket %ld",s);
                abc(aa);
                A.sin_family=AF_INET;
                A.sin_port = htons(80);
                A.sin_addr.s_addr =INADDR_ANY;
                d=bind(s,(struct sockaddr *) &A,sizeof(A));
                sprintf(aa,"bind %ld",d);
                abc(aa);
                WSAAsyncSelect(s,b,WM_USER+1,FD_ACCEPT|FD_READ);
                d=listen(s,100);
                sprintf(aa,"listen %ld",d);
                abc(aa);
                MessageBox(0,"hi","hi",0);
        }
        if ( x == WM_DESTROY)
                PostQuitMessage(0);
        return DefWindowProc(w,x,y,z);
}

Have any suggestions, comments, ideas, cracked code, feedback. Feel free to get in touch with us.

Move back to the Vijay Mukhi's Technology Cornucopia Page to learn more about the other new Internet Technologies.


Vijay Mukhi's Computer Institute
VMCI, B-13, Everest Building, Tardeo, Bombay 400 034, India
E-mail:vmukhi@giasbm01.vsnl.net.in Tel : 91-22-496 4335 /6/7/8/9 Fax : 91-22-307 28 59
http://www.neca.com/~vmis