Разработка программы для работы по TCP/IP

Страницы работы

Содержание работы

Задание: Разработь программу для работы по TCP/IP.

Дополнение: В качестве примера разработаем работу по протоколу sock4 с некоторыми ограничениями(сократим набор комманд до 1 / CONNECT  и прием / передача).

Исходный код программы:

#include <windows.h>

#define LETTER_HI_W                 (UINT)9

#define LETTER_H                    (UINT)16

#define LETTER_LO_W                 (UINT)6

#define LETTER_MAX                  (UINT)14

#define MAX_SIZE                    (UINT)1024

#define WINDOW_H                    (UINT)(4+2*LETTER_H+3)

#define WINDOW_W                    (UINT)(3+LETTER_MAX*LETTER_HI_W+3)

#define RECV_BUF_SIZE               MAX_SIZE//65535

#define SOCK_NUM 16

#define WM_SOCK_ACCEPT              (UINT)(WM_USER+1)

#define WM_SOCK_READ_CLOSE_CONNECT  (UINT)(WM_USER+2)

#define WM_SOCK_READ_CLOSE_FIRST    (UINT)(WM_USER+3)

//=============================================================================

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);

//=============================================================================

char        buf[MAX_SIZE+1];

HDC         hDC;

RECT        rect;

char        app_title[]="CL_PROXY_SOCK4";

DWORD       port=8888;

WSADATA     wsadata;

SOCKET      hsock_server;            // main sock

SOCKADDR_IN sa,st;

DWORD       q,r;

UINT        recv_size;

SOCKET      ar_socks_client[SOCK_NUM],\

ar_socks_local[SOCK_NUM]; // accepted sockets, proxy client & sockets

UCHAR       recv_buf[RECV_BUF_SIZE+1];

//=============================================================================

void fill_mem(void* output, int value, int len)

{

if (len)

{

do

{

*(unsigned char*)output=(unsigned char)value;

output=(void*)((unsigned int)output + 1);

}

while(--len != 0);

};

}

//=============================================================================

void main()

{

HINSTANCE   hInst;

UINT        Wwd,Wht;

MSG         msg;

HWND        hWnd;

Wwd=GetSystemMetrics(SM_CXMAXIMIZED);//SM_CXFULLSCREEN SM_CXSCREEN SM_CXMAXIMIZED

Wht=GetSystemMetrics(SM_CYMAXIMIZED);

rect.right=WINDOW_W; // w

rect.bottom=WINDOW_H; //h

rect.left=Wwd-rect.right-8; // x

rect.top=Wht-rect.bottom-8; //y

hInst=GetModuleHandle(0);

WNDCLASS wc;

fill_mem(&wc,0,sizeof(WNDCLASS));

wc.style=CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ;

wc.lpfnWndProc=(WNDPROC)MainWndProc;

wc.hInstance=hInst;

wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);//(COLOR_WINDOW);

wc.lpszClassName=app_title;

wc.lpszMenuName=NULL;

wc.hCursor=LoadCursor(NULL,IDC_ARROW);

wc.hIcon=LoadIcon(NULL,IDI_APPLICATION);

if (RegisterClass(&wc))

{

if  (hWnd=CreateWindowEx(8,wc.lpszClassName,wc.lpszClassName+3, 0x080000040,\

rect.left,rect.top,rect.right,rect.bottom, NULL, NULL, hInst, NULL))

{

ShowWindow(hWnd,SW_SHOW);

UpdateWindow(hWnd);

while (GetMessage(&msg,NULL,0,0))

{

TranslateMessage(&msg);

DispatchMessage(&msg);

};

};

};

}

//=============================================================================

void out(char* str)

{

lstrcpy(buf,str);

buf[LETTER_MAX-1]=0;

Rectangle(hDC,3-1,4+LETTER_H-1,3+LETTER_MAX*LETTER_HI_W+1,4+2*LETTER_H+1);

TextOut(hDC,3,4+LETTER_H,buf,lstrlen(buf));

return;

}

//=============================================================================

LRESULT CALLBACK MainWndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)

{

PAINTSTRUCT ps;

UINT lowParam;

switch (msg)

{

//=============================================================================

case WM_DESTROY:

{

for (r=0;r<SOCK_NUM;r++)

{

closesocket(ar_socks_client[r]);

ar_socks_client[r]=0;

closesocket(ar_socks_local[r]);

ar_socks_local[r]=0;

};

WSACleanup;

//PostQuitMessage(0);

ExitProcess(0);

break;

};

//=============================================================================

case WM_PAINT:

{

BeginPaint(hWnd, &ps);

Rectangle(hDC,0,0,rect.right,rect.bottom);

wsprintf(buf,"SOCK4[%lu] : %0.5lu",SOCK_NUM,port);

TextOut(hDC,2,2,buf,lstrlen(buf));

EndPaint(hWnd, &ps);

break;

}

//=============================================================================

case WM_NCHITTEST: return HTCAPTION;

//=============================================================================

case WM_CLOSE:

{

if(MessageBox(hWnd,"Please Confirm Exit",app_title,MB_YESNO)==IDYES)

Похожие материалы

Информация о работе

Тип:
Написанные программы на языках программирования
Размер файла:
54 Kb
Скачали:
0