Write Your Own Time Server


Create a Project (Application) in Visual C++ (we have used MSVC 4.0)
The files in the project


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];char cc[2000];int xx;
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,d2,dd;
struct sockaddr_in B;
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
	if ( x == WM_LBUTTONDOWN)
	{
		d=WSAStartup(0x0101,&ws);
		sprintf(aa,"d = %ld",d);
		abc(aa);
		s=socket(AF_INET, SOCK_DGRAM,0);
		sprintf(aa,"socket %ld",s);
		abc(aa);
		A.sin_family=AF_INET;
		A.sin_port = htons(13);
		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);
		int dw=sizeof(A);
		d=recvfrom(s,bb,100,0,(sockaddr *)&A,&dw);
		sprintf(aa,"recvfrom =%ld..bb=%s",d,bb);
		abc(aa);
		MessageBox(0,aa,aa,0);
		MessageBox(0,"end","hi",0);
	}
	if ( x == WM_DESTROY)
		PostQuitMessage(0);
	return DefWindowProc(w,x,y,z);
}


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];char cc[2000];int xx;
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","In server",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,d2,dd;
struct sockaddr_in B;
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
	if ( x == WM_LBUTTONDOWN)
	{
		d=WSAStartup(0x0101,&ws);
		sprintf(aa,"d = %ld",d);
		abc(aa);
		s=socket(AF_INET, SOCK_DGRAM,0);
		sprintf(aa,"s = %ld",s);
		abc(aa);
		A.sin_family=AF_INET;
		A.sin_port = htons(13);
		A.sin_addr.s_addr =INADDR_ANY;
		d=bind(s,(struct sockaddr *) &A,sizeof(A));
		sprintf(aa,"d = %ld",d);
		abc(aa);
		int dw=sizeof(A);
		d=recvfrom(s,bb,100,0,(sockaddr *)&A,&dw);
		sprintf(aa,"d = %ld..bb=%s",d,bb);
		abc(aa);
		MessageBox(0,bb,"in server",0);
		strcpy(bb,"hello");
		d=sendto(s,bb,100,0,(sockaddr *)&A,sizeof(A));
		sprintf(aa,"in serv...d = %ld",d);
		abc(aa);
	}
	if ( x == WM_DESTROY)
		PostQuitMessage(0);
	return DefWindowProc(w,x,y,z);
}


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];char cc[2000];int xx;
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","In server",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,d2,dd;
struct sockaddr_in B;
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
	if (x == WM_USER+1)
	{
		strcpy(bb,"hello");
		d=sendto(s,bb,100,0,(sockaddr *)&A,sizeof(A));
		sprintf(aa,"in serv...d = %ld",d);
		abc(aa);
		MessageBox(0,aa,aa,0);
	}
	if ( x == WM_LBUTTONDOWN)
	{
		d=WSAStartup(0x0101,&ws);
		sprintf(aa,"d = %ld",d);
		abc(aa);
		s=socket(AF_INET, SOCK_DGRAM,0);
		sprintf(aa,"s = %ld",s);
		abc(aa);
		A.sin_family=AF_INET;
		A.sin_port = htons(13);
		A.sin_addr.s_addr =INADDR_ANY;
		d=bind(s,(struct sockaddr *) &A,sizeof(A));
		WSAAsyncSelect(s,b,WM_USER+1,FD_READ);
		int dw=sizeof(A);
		d=recvfrom(s,bb,100,0,(sockaddr *)&A,&dw);
		MessageBox(0,bb,"in server",0);
		abc(bb);
	}
	if ( x == WM_DESTROY)
		PostQuitMessage(0);
	return DefWindowProc(w,x,y,z);
}


Time Client

#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[200];SOCKET s;struct hostent h;
WSADATA ws;DWORD e;int ii,dw;char bb[100];struct sockaddr_in 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("127.0.0.1");
	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,"hi","hi",0);
}

if ( x == WM_DESTROY)
	PostQuitMessage(0);
return DefWindowProc(w,x,y,z);
}
DownLoad the Source code.


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.vijaymukhi.com