Socket Programming

Basics


Create a Project in VC++ 4.0.
The files in the project will be


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);
}

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[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) { e=WSAStartup(0x0101,&ws); sprintf(aa,"e..%ld",e); MessageBox(0,aa,aa,0); abc(aa); MessageBox(0,"end","end",0); } if ( x == WM_DESTROY) PostQuitMessage(0); return DefWindowProc(w,x,y,z); }
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[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) PostQuitMessage(0); return DefWindowProc(w,x,y,z); }
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[200];SOCKET s; 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) { 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); MessageBox(0,aa,aa,0); MessageBox(0,"end","end",0); } if ( x == WM_DESTROY) PostQuitMessage(0); return DefWindowProc(w,x,y,z); }
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[200]; WSADATA ws;DWORD e;long dd; 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) { dd = inet_addr("140.252.1.32"); sprintf(aa,"dd=%ld",dd); abc(aa); MessageBox(0,aa,aa,0); MessageBox(0,"end","end",0); } if ( x == WM_DESTROY) PostQuitMessage(0); return DefWindowProc(w,x,y,z); }
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[200]; WSADATA ws;DWORD e;long dd;struct in_addr 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","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) { sa.s_addr = inet_addr("140.252.1.32"); sprintf(aa,"sa.s_addr=%ld",sa.s_addr); abc(aa); MessageBox(0,aa,aa,0); char *p=inet_ntoa(sa); sprintf(aa,"p=%s",p); MessageBox(0,aa,aa,0); MessageBox(0,"hi","hi",0); } if ( x == WM_DESTROY) PostQuitMessage(0); return DefWindowProc(w,x,y,z); }
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[200];SOCKET s; WSADATA ws;DWORD e;long ii; 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) { ii=htons(2); sprintf(aa,"ii.htons(2)=..%ld",ii); abc(aa); MessageBox(0,aa,aa,0); ii=htons(512); sprintf(aa,"ii.htons(512)=..%ld",ii); abc(aa); MessageBox(0,aa,aa,0); MessageBox(0,"end","end",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.vijaymukhi.com