
Опытный
 
Профиль
Группа: Участник Клуба
Сообщений: 503
Регистрация: 25.3.2002
Где: Зеленоград
Репутация: 3 Всего: 3
|
| Код | //My first FTP client #include "stdafx.h" #include "winsock.h" #include "malloc.h"
const WORD WINSOCK_VERSION = 0x0101; const int WM_READ = WM_USER+1; #define DEFAULT_PROTOCOL 0 #define PORT_ADDR 21
#define SCREEN_X (GetSystemMetrics(SM_CXFULLSCREEN)-640)/2 #define SCREEN_Y (GetSystemMetrics(SM_CYFULLSCREEN)-480)/2
HINSTANCE hInstance; HWND hWnd; WSADATA wsaData; SOCKET sock; char *text=NULL; int iAction=0;
LONG WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); void DrwText(); void SendNextCommand();
void SendData(LPSTR buff) { if (send(sock,(LPSTR)buff,lstrlen(buff),0) == SOCKET_ERROR) MessageBox(NULL, "Error Send", "Error!", MB_OK); else { if (text==NULL) text=strdup(buff); else { text=(char*)realloc(text,_msize(text)+sizeof(char)*strlen(buff)); strcat(text,buff); } } }
int APIENTRY WinMain(HINSTANCE hCurInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { if (WSAStartup(WINSOCK_VERSION, &wsaData)) { MessageBox(NULL,"Winsock not initialized !","Error",MB_OK); WSACleanup(); return 0; } sock = socket(PF_INET,SOCK_STREAM,DEFAULT_PROTOCOL); if (sock==INVALID_SOCKET) { MessageBox(NULL,"Error creating socket !","Error",MB_OK); return 0; } MSG msg; hInstance=hCurInst; MakeWindow(); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; }
LONG WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { static PAINTSTRUCT ps;
switch(uMsg) { case WM_SHOWWINDOW: LOGFONT logfont; HFONT hFont; logfont.lfWeight=0; logfont.lfHeight=15; logfont.lfEscapement=0; logfont.lfOrientation=0; logfont.lfWeight=FW_NORMAL; logfont.lfItalic=0; logfont.lfUnderline=0; logfont.lfStrikeOut=0; logfont.lfCharSet=ANSI_CHARSET; logfont.lfOutPrecision=OUT_DEFAULT_PRECIS; logfont.lfClipPrecision=CLIP_DEFAULT_PRECIS; logfont.lfQuality=PROOF_QUALITY; logfont.lfPitchAndFamily=VARIABLE_PITCH|FF_ROMAN; strcpy(logfont.lfFaceName,"Verdana"); hFont=CreateFontIndirect(&logfont); SelectObject(GetDC(hWnd),hFont);
SOCKADDR_IN socketaddr; socketaddr.sin_family = AF_INET; socketaddr.sin_addr.s_addr = INADDR_ANY; socketaddr.sin_port = PORT_ADDR; if (bind(sock,(LPSOCKADDR)&socketaddr,sizeof(socketaddr)) == SOCKET_ERROR) { MessageBox(NULL,"Error binding socket !","Error",MB_OK); return 0; } struct hostent* hp; if((hp=gethostbyname((LPTSTR)"ftp.avp.ru")) == NULL) { MessageBox(NULL,"Error resolving host !","Error",MB_OK); return 0; } memset(&socketaddr,0,sizeof(socketaddr)); socketaddr.sin_family = AF_INET; socketaddr.sin_addr.s_addr = ((LPIN_ADDR)hp->h_addr)->s_addr; socketaddr.sin_port = htons((u_short)PORT_ADDR); if (connect(sock,(LPSOCKADDR)&socketaddr,sizeof(socketaddr)) == SOCKET_ERROR) { MessageBox(NULL,"Error connecting !","Error",MB_OK); return 0; } if (WSAAsyncSelect(sock,hWnd,WM_READ,FD_READ) == SOCKET_ERROR) { MessageBox(NULL,"AsyncSelect BAD !!!","Error!",MB_OK); return 0; } return 0; case WM_PAINT: BeginPaint(hWnd,&ps); DrwText(); EndPaint(hWnd,&ps); return 0; case WM_SIZE: PostMessage(hWnd, WM_PAINT, 0, 0); return 0; case WM_CLOSE: closesocket(sock); WSACleanup(); PostQuitMessage(0); return 0; case WM_READ: if (WSAGETSELECTEVENT(lParam) == FD_READ) { char *buf=(char*)malloc(1000*sizeof(char)); int rc=recv(sock,buf,1000,0); buf[rc]='\0'; if (text==NULL) text=strdup(buf); else { text=(char*)realloc(text,_msize(text)+sizeof(char)*strlen(buf)); strcat(text,buf); } free(buf); DrwText(); } SendNextCommand(); return 0; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }
void DrwText() { if (text !=NULL) { RECT rect; POINT point; GetWindowRect(hWnd,&rect); point.x=rect.left; point.y=rect.top; ScreenToClient(hWnd,&point); rect.left=point.x; rect.top=point.y; point.x=rect.right; point.y=rect.bottom; ScreenToClient(hWnd,&point); rect.right=point.x; rect.bottom=point.y; OffsetRect(&rect,rect.left<0?-rect.left:0,rect.top<0?-rect.top:0); DrawText(GetDC(hWnd),text,strlen(text),&rect,DT_LEFT); } }
void SendNextCommand() { switch (iAction) { case 0: { SendData("USER anonymous\n"); break; } case 1: { SendData("PASS\n"); break; } case 2: { SendData("SYST\n"); break; } case 3: { SendData("PWD\n"); break; } case 4: { SendData("PORT 192,168,1,33,4,20\n"); break; } case 5: { SendData("TYPE A\n"); break; } case 6: { SendData("LIST\n"); break; } } iAction++; } |
В чём здесь дело, почему все комманды выполняются, а после LIST'а сервер отвечает: Can't build data connection: Connection refused
--------------------
-------------------------- Шеф всегда прав :)
|