Author : Vijay Mukhi
Page : << Previous 3 Next >>
Sa;
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","time client",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)
{
e=WSAStartup(0x0101,&ws);
sprintf(aa,"e = %ld",e);
abc(aa);
s = socket(PF_INET,SOCK_DGRAM,0);
sprintf(aa,"s = %ld",s);
abc(aa);
Sa.sin_family=AF_INET;
Sa.sin_addr.s_addr = inet_addr("140.252.1.32");
Sa.sin_port=htons(13);
strcpy (bb,"hello how are you");
e=sendto(s,bb,100,0,(struct sockaddr *)&Sa,sizeof(Sa));
sprintf(aa,"SendTo %ld",e);
int dw = sizeof(Sa);
recvfrom(s,bb,100,0,(sockaddr *)&Sa,&dw);
MessageBox(0,bb,"data from server",0);
MessageBox(0,"end","end",0);
}
if ( x == WM_DESTROY)
PostQuitMessage(0);
return DefWindowProc(w,x,y,z);
}
This is it ! your very first Internet Client ! It doesn't seem like much, but it doesn't do much either !! It's just the bare code and the user interface is absolutely pukey. But it works and that's what counts.
To run this program, first create a project in Visual C++ ( We're using Visual C++ 4.0 ) and insert these files into the project :-
A1.cpp - This is the file displayed above.
Wsock32.lib - This is a lib file not displayed above.
Build the project. Before you run the .exe, be sure to log into your local Internet Service Provider. Now run the file. You will be confronted with a beautifully rendered and artistically designed blank window. Click anywhere in it and the Internet code in the Callback function is called.
The code itself is rather simple. WinMain() creates and initializes a window and we've associated a callback function with it. The window is assigned certain properties and then maximized. The callback, zzz() contains the real juicy bits. When you click in the window (WM_LBUTTONDOWN ), the function zzz() is called and it inturn kicks off the code for the Time Client. The header file, windows.h internally calls the file winsock.h.
In the first line, the function WSAStartup() is absolutely necessary. This is the function which initializes the whole code. It's first parameter is 0x0101 which symbolizes the Winsock's version number . 0x0101 stands for WinSock version 1.01 (you have to read it backwards and consider the zero to be a decimal point). &ws is the location in memory of the structure tag WSAData. The uses of &ws will be discussed later. If all went well, WSAStartup() will return a zero , a non-zero value implies that an error occurred.
#include <windows.h>
#include <stdio.h>
void abc(char *p)
{
FILE *fp=fopen("c:\\z.txt","a+");
fprintf(fp,"%s\n",p);
fclose(fp);
}
WNDCLASS a;HWND b;MSG c;char aa[200];char bb[20000];
WSADATA ws;DWORD e;
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)
{ abc("Before WSAStartup");
sprintf(aa,"wVersion....%d",ws.wVersion);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"wHighVersion....%d",ws.wHighVersion);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"szDescription....%s",&ws.szDescription);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"szSystemStatus....%s",&ws.szSystemStatus);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"iMaxSockets....%u",ws.iMaxSockets);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"iMaxUdpDg....%u",ws.iMaxUdpDg);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"lpVendorInfo....%s",ws.lpVendorInfo);
MessageBox(0,aa,aa,0);
abc(aa);
e=WSAStartup(0x0101,&ws);
sprintf(aa,"e..%ld",e);
MessageBox(0,aa,aa,0);
abc("After WSAStartup");
sprintf(aa,"wVersion....%d",ws.wVersion);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"wHighVersion....%d",ws.wHighVersion);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"szDescription....%s",&ws.szDescription);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"szSystemStatus....%s",&ws.szSystemStatus);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"iMaxSockets....%u",ws.iMaxSockets);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"iMaxUdpDg....%u",ws.iMaxUdpDg);
MessageBox(0,aa,aa,0);
abc(aa);
sprintf(aa,"lpVendorInfo....%s",ws.lpVendorInfo);
MessageBox(0,aa,aa,0);
abc(aa);
MessageBox(0,"end","end",0);
}
if ( x == WM_DESTROY)
Page : << Previous 3 Next >>