?? windows internet programming part1.html
字號:
Here we have chosen to display the result in a messagebox tough rather than a text file.
The result should be a message box with caption, "Port" and text saying Port 80 = 20480.
5.3 EXPLORING WINSOCK FUNCTIONS
=======================================
/* <---- SOURCE CODE STARTS HERE ----> */
#include <windows.h>
#include <stdio.h>
WSADATA ws;
DWORD wsock;
char msge[200];
LRESULT CALLBACK recall (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Interface");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = recall;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "Interface";
RegisterClass (&wndclass);
hwnd = CreateWindow (szAppName, // Windows Class Name
"Interface", // Windows Caption
WS_OVERLAPPEDWINDOW, // Windows Style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
200, // initial x size
200, // initial y size
0, // parent window handle
0, // parent menu handle
hInstance, // program instance handle
0); // creation parameters
ShowWindow(hwnd, iCmdShow);
while (GetMessage (&msg, NULL, 0, 0))
{
DispatchMessage (&msg);
}
return 1;
}
LRESULT CALLBACK recall (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_LBUTTONDOWN)
{
wsock = WSAStartup(0x0101,&ws);
sprintf(msge,"wsock..%ld",wsock);
MessageBox(0,msge,msge,0);
sprintf(msge,"wVersion....%d",ws.wVersion);
MessageBox(0,msge,msge,0);
sprintf(msge,"wHighVersion....%d",ws.wHighVersion);
MessageBox(0,msge,msge,0);
sprintf(msge,"szDescription....%s",&ws.szDescription);
MessageBox(0,msge,msge,0);
sprintf(msge,"szSystemStatus....%s",&ws.szSystemStatus);
MessageBox(0,msge,msge,0);
sprintf(msge,"iMaxSockets....%u",ws.iMaxSockets);
MessageBox(0,msge,msge,0);
sprintf(msge,"iMaxUdpDg....%u",ws.iMaxUdpDg);
MessageBox(0,msge,msge,0);
sprintf(msge,"lpVendorInfo....%s",ws.lpVendorInfo);
MessageBox(0,msge,msge,0);
MessageBox(0,"Finished","End",0);
WSACleanup();
}
if (message == WM_DESTROY)
PostQuitMessage(0);
return DefWindowProc(hwnd, message, wParam, lParam);
}
/* <---- SOURCE CODE ENDS HERE ----> */
This program calls several functions of winsock and displays the results to us in message box's.
These can be handy if, for example I wanted to tell some-1 what version of winsock there running I
would call &ws.szDescription which in my case would say Winsock 2.0, in a nicely human readable value.
[ EXERCISES ]
Look trough the file winsock.h in your compiler include folder. See what else there is in winsock
that can be demonstrated in these kinds of programs.
________________________________________________________________________________________________________
6.0 Common Internet programs
=======================================
In this section were going to build a few common internet applications using the information that
we have learnt so far.
These programs are:
1. DNS - Gets a hostname and returns its IP address.
2. Portscan - Gets port numbers and tells user whether there open or not.
3. Nuke - Sends OOB data to a listening port.
6.1 DNS
=======================================
/* <---- SOURCE CODE STARTS HERE ----> */
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)
{
TCHAR szBuffer [1024];
va_list pArgList;
va_start (pArgList, szFormat);
_vsntprintf (szBuffer, sizeof (szBuffer) / sizeof (TCHAR),
szFormat, pArgList);
va_end (pArgList);
return MessageBox (NULL, szBuffer, szCaption, 0);
}
WSADATA wsdata;
SOCKET sock;
DWORD wsock;
LRESULT CALLBACK recall (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Interface");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = recall;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "Interface";
RegisterClass (&wndclass);
hwnd = CreateWindow (szAppName, // Windows Class Name
"Interface", // Windows Caption
WS_OVERLAPPEDWINDOW, // Windows Style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
200, // initial x size
200, // initial y size
0, // parent window handle
0, // parent menu handle
hInstance, // program instance handle
0); // creation parameters
ShowWindow(hwnd, iCmdShow);
while (GetMessage (&msg, NULL, 0, 0))
{
DispatchMessage (&msg);
}
return 1;
}
LRESULT CALLBACK recall (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_LBUTTONDOWN)
{
wsock = WSAStartup(0x0101,&wsdata);
struct hostent *h;
h = gethostbyname("localhost");
MessageBoxPrintf(TEXT ("Hostname"),h->h_name);
MessageBoxPrintf(TEXT ("IP Address"),inet_ntoa(*((struct in_addr *)h->h_addr)));
WSACleanup();
}
if (message == WM_DESTROY)
PostQuitMessage(0);
return DefWindowProc(hwnd, message, wParam, lParam);
}
/* <---- SOURCE CODE ENDS HERE ----> */
This program utilizes the hostent structure, like we discussed earlier, and uses the gethostbyname();
function to get the name of the host were looking for.
The hostent structure is filled up and we access the members h_name and h_addr and display them in Messagebox's.
These aren't normal message box's as normal ones wouldn't be able to display the info for them so we added
some extra code to create MessageBoxPrintf(); to emulate the consoles printf() function.
6.2 Portscan
=======================================
/* <---- SOURCE CODE STARTS HERE ----> */
#include <windows.h>
#include <stdio.h>
int CheckPortUDP(short int Port)
{
struct sockaddr_in client;
WSADATA wsaData;
int Busy = 0;
int sock;
if(WSAStartup(0x0101, &wsaData) == 0)
{
client.sin_family = AF_INET;
client.sin_port = htons(Port);
client.sin_addr.s_addr = inet_addr("127.0.0.1");
/* Check UDP Protocol */
sock = socket(AF_INET, SOCK_DGRAM, 0);
Busy = (bind(sock, (SOCKADDR FAR *) &client,sizeof(SOCKADDR_IN)) == SOCKET_ERROR);
if(Busy)
WSACleanup();
}
return(Busy);
}
int CheckPortTCP(short int Port)
{
struct sockaddr_in client;
WSADATA wsaData;
int Busy = 0;
int sock;
if(WSAStartup(0x0101, &wsaData) == 0)
{
client.sin_family = AF_INET;
client.sin_port = htons(Port);
client.sin_addr.s_addr = inet_addr("127.0.0.1");
sock = socket(AF_INET, SOCK_STREAM, 0);
Busy = (connect(sock, (struct sockaddr *) &client,sizeof(client)) == 0);
if(Busy)
WSACleanup();
}
return(Busy);
}
LRESULT CALLBACK recall (HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Interface");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = recall;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "Interface";
RegisterClass (&wndclass);
hwnd = CreateWindow (szAppName, // Windows Class Name
"Interface", // Windows Caption
WS_OVERLAPPEDWINDOW, // Windows Style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
200, // initial x size
200, // initial y size
0, // parent window handle
0, // parent menu handle
hInstance, // program instance handle
0); // creation parameters
ShowWindow(hwnd, iCmdShow);
if(CheckPortTCP(80))
MessageBox(0,"HTTP Server is running","HTTP",0);
else
MessageBox(0,"HTTP Server is down","HTTP",0);
if(CheckPortUDP(13))
MessageBox(0,"DayTime Server is running","DayTime",0);
else
MessageBox(0,"DayTime Server is down","DayTime",0);
while (GetMessage (&msg, NULL, 0, 0))
{
DispatchMessage (&msg);
}
return 1;
}
LRESULT CALLBACK recall (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -