Solved

Small web client

Posted on 2000-05-07
1
199 Views
Last Modified: 2010-04-02
Hello,
I need to build as small as possible client that will connect to some URL and will parse short response from it.
What will be the best way to do it?
(I don't want to use MFC).

Thanks for any suggestion,
arnon@zzn.com
0
Comment
Question by:arnonayal
1 Comment
 
LVL 2

Accepted Solution

by:
arikka earned 150 total points
ID: 2785881
// This is simple WEB client without MFC
// It uses WinSock ( add wsock32.lib in project settings)


#include <windows.h>
#include <winsock.h>
#include <stdio.h>

void TRACE(char *str)
{
   FILE *fp=fopen("browser.log","a+");
   fprintf(fp,"%s\n",str);
   fclose(fp);
}

/////////////////////////////////////////////////////////////

WNDCLASS wndCls;
HWND hWnd;
MSG msg;

long _stdcall wndProc (HWND,UINT,WPARAM,LPARAM);

int _stdcall WinMain(HINSTANCE inst,HINSTANCE j,char *cmdLine,int l)
{
        wndCls.lpszClassName ="something";
        wndCls.hInstance = inst;
        wndCls.lpfnWndProc = wndProc;

        wndCls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

        RegisterClass(&wndCls);
        hWnd = CreateWindow("something","WEB Client",
                                 WS_OVERLAPPEDWINDOW,
                                           CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             CW_USEDEFAULT,
                             NULL,
                             NULL,
                             inst,
                             NULL);

        ShowWindow(hWnd,3);
      UpdateWindow(hWnd);


        while (GetMessage(&msg,0,0,0) )
               DispatchMessage(&msg);

        return 1;
}
/////////////////////////////////////////////////////////////

WSADATA wsdata;
SOCKET s;
sockaddr_in sa;
struct hostent *host;

int d;

char buf   [200];
char buf_2 [1000];

long _stdcall wndProc (HWND hwind,UINT message, WPARAM wParam,LPARAM lParam)
{

        if ( message == WM_LBUTTONDOWN)
        {

          d = WSAStartup(0x0101,&wsdata);
          s = socket(AF_INET,SOCK_STREAM,0);
          sa.sin_family = AF_INET;
          sa.sin_port = htons(80);

        host = gethostbyname("WEB SERVER NAME HERE");
        sa.sin_addr.s_addr=*((unsigned long *) host->h_addr);

          d = connect(s,(struct sockaddr *)&sa,sizeof(sa));
// here insert your URL
          strcpy(buf,"GET aaa/bbb/ccc/html.htm \r\n");
          d = send(s, buf, strlen(buf),0);
          d = 1;
          while(d !=0)
        {
                strset(buf_2,' ');
                d = recv(s,buf_2, sizeof(buf_2),0);
                TRACE(buf_2);
        }
          MessageBox(0,"All","Over",0);

        }

        if ( message == WM_DESTROY)
            PostQuitMessage(0);

        return DefWindowProc(hwind, message, wParam,lParam);
}

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now