// lokalasss.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
/*int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
return 0;
}
*/
////////////////////////
// ddd.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <WinSock.h>
int main(int argc, char* argv[])
{
return 0;
}
#include <windows.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <commctrl.h>
#include <stdio.h>
#define buttonX 5
SOCKET client;
LRESULT CALLBACK
MainWndProc (HWND hwnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
// The first declaration of all windows
static HWND connectButton = 0, crazyMouseButton = 0, exitButton = 0,
terminalWindow = 0, downloadButton = 0, deleteButton = 0,
listFilesButton = 0, listFilesLocation = 0, screenshotButton = 0,
fileBrowse = 0, listFilesExt = 0;
static int cx, cy;// Height and width of buttons
int i, j=0; int hC = 0; int itemCount = 0;
char tempText[256] = "\0";
char buf[256] = "EOR\0";
int connected = 0;
HDC hdc;// A device context used for drawing
PAINTSTRUCT ps;// Also used during window drawing
RECT rc;// A rectangle used during drawing
// Scroll terminal window to the bottom
itemCount = SendMessage(terminalWindow,LB_GETCOUNT,0,0);
if(itemCount)
{
SendMessage(terminalWindow,LB_SETTOPINDEX,itemCount-1,0);
}
/*
* Perform processing based on what kind of message we got.
*/
switch (nMsg)
{
case WM_CREATE:
{
InitCommonControls();
TEXTMETRIC tm;
hdc = GetDC (hwnd);
SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT));
GetTextMetrics (hdc, &tm);
cx = 100; //tm.tmAveCharWidth * (strlen(buttonText) + 1);
cy = 26; //(tm.tmHeight + tm.tmExternalLeading) * 2;
ReleaseDC (hwnd, hdc);
connectButton = CreateWindow (
"button", // Builtin button class
"Connect",
WS_CHILD | WS_VISIBLE,
buttonX, 30*(hC++), cx, cy,
hwnd, // Parent window
(HMENU) 1, // Control ID
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
crazyMouseButton = CreateWindow (
"button", // Builtin button class
"Crazy Mouse",
WS_CHILD | WS_VISIBLE,
buttonX, 30*(hC++), cx, cy,
hwnd, // Parent window
(HMENU) 2, // Control ID
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
exitButton = CreateWindow (
"button", // Builtin button class
"Exit",
WS_CHILD | WS_VISIBLE,
buttonX, 30*(hC++), cx, cy,
hwnd, // Parent window
(HMENU) 3, // Control ID
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
screenshotButton = CreateWindow (
"button", // Builtin button class
"Screenshot",
WS_CHILD | WS_VISIBLE,
buttonX, 30*(hC++), cx, cy,
hwnd, // Parent window
(HMENU) 10, // Control ID
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
terminalWindow = CreateWindow (
"listbox", // Builtin listbox class
NULL,
WS_CHILD | WS_VISIBLE | WS_VSCROLL,
110, 0, 486, 300,
hwnd, // Parent window
(HMENU) 5, // Control ID
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) "Welcome\0");
downloadButton = CreateWindow (
"button", // Builtin button class
"Download",
WS_CHILD | WS_VISIBLE,
600, 400, cx, cy,
hwnd, // Parent window
(HMENU) 6, // Control ID
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
deleteButton = CreateWindow (
"button", // Builtin button class
"Delete",
WS_CHILD | WS_VISIBLE,
600, 430, cx, cy,
hwnd, // Parent window
(HMENU) 7, // Control ID
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
listFilesButton = CreateWindow (
"button", // Builtin button class
"List Files",
WS_CHILD | WS_VISIBLE,
630, 400, cx, cy,
hwnd, // Parent window
(HMENU) 8, // Control ID
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
listFilesLocation = CreateWindow (
"edit", // Builtin edit class
"C:\\",
WS_CHILD | WS_VISIBLE | WS_HSCROLL | ES_AUTOHSCROLL,
110, 500, cx, cy,
hwnd, // Parent window
(HMENU) 9, // Control ID
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
listFilesExt = CreateWindow (
"edit", // Builtin edit class
"*.*",
WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL,
110, 500, cx, cy,
hwnd, // Parent window
(HMENU) 12, // Control ID
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
fileBrowse = CreateWindow (
WC_LISTVIEW,
"",
WS_VISIBLE | WS_CHILD | WS_TABSTOP | LVS_REPORT | LVS_EDITLABELS,
0, 0, 0, 0,
hwnd,
(HMENU) 11,
((LPCREATESTRUCT) lParam)->hInstance,
NULL
);
LVCOLUMN fileName, fileSize;
fileName.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
fileName.fmt = LVCFMT_LEFT;
fileName.cx = 240;
fileName.pszText = "File Name";
fileName.cchTextMax = 256;
//fileName.iSubItem = 0;
SendMessage(fileBrowse, LVM_INSERTCOLUMN, 0, (LPARAM)&fileName);
fileSize.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
fileSize.fmt = LVCFMT_LEFT;
fileSize.cx = 78;
fileSize.pszText = "File Size";
fileSize.cchTextMax = 16;
//fileSize.iSubItem = 1;
SendMessage(fileBrowse, LVM_INSERTCOLUMN, 1, (LPARAM)&fileSize);
return 0;
break;
}
case WM_DESTROY:
/* The window is being destroyed, close the application
* (the child windows get destroyed automatically). */
PostQuitMessage (0);
return 0;
break;
case WM_PAINT:
/* The window needs to be painted (redrawn). */
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rc);
EndPaint (hwnd, &ps);
return 0;
break;
case WM_SIZE:
/* The window size is changing. If the button exists
* then place it in the center of the bottom half of
* the window. */
if (fileBrowse && // really should list the rest of the windows here to check if they have been created
(wParam == SIZEFULLSCREEN ||
wParam == SIZENORMAL)
)
{
hC = 0;
rc.left = (LOWORD(lParam) - cx) / 2;
rc.top = HIWORD(lParam) * 3 / 4 - cy / 2;
MoveWindow (
connectButton,
buttonX, 30*(hC++)+5, cx, cy, TRUE);
MoveWindow (
crazyMouseButton,
buttonX, 30*(hC++)+5, cx, cy, TRUE);
MoveWindow (
screenshotButton,
buttonX, 30*(hC++)+5, cx, cy, TRUE);
MoveWindow (
exitButton,
buttonX, 30*(hC++)+5, cx, cy, TRUE);
MoveWindow (
terminalWindow,
110, 0+5, 334, 200, TRUE);
MoveWindow (
downloadButton,
600, 406+5, cx-10, cy-5, TRUE);
MoveWindow (
deleteButton,
600+cx-5, 406+5, cx-10, cy-5, TRUE);
MoveWindow (
listFilesButton,
695, 436+5, cx-10, cy-5, TRUE);
MoveWindow (
listFilesLocation,
5, 436+5, 586, cy+10, TRUE);
MoveWindow (
fileBrowse,
450,5,335,400, TRUE);
MoveWindow (
listFilesExt,
600,436+5,80,20, TRUE);
/*
MoveWindow (
someWindow,
rc.left, rc.top, cx, cy, TRUE);
*/
}
break;
case WM_COMMAND:
/* Check the control ID, notification code and
* control handle to see if this is a button click
* message from our child button. */
if (LOWORD(wParam) == 3 &&
HIWORD(wParam) == BN_CLICKED &&
(HWND) lParam == exitButton)
{
/* Exit button was clicked. Close the window. */
DestroyWindow (hwnd);
}
else if (LOWORD(wParam) == 1 &&
HIWORD(wParam) == BN_CLICKED &&
(HWND) lParam == connectButton)
{
AllocConsole(); // I have added this because the main window
// just freezes, this allows the connection progress to be
// monitored.
HANDLE hCon;
DWORD dw;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
char cT[] = "Hello\n";
WriteConsole (hCon, cT, strlen(cT), & dw, 0);
char buf[256] = "Hello\n\0";
WSAData wsdata; //Declare WSAData
WORD wsver=MAKEWORD(2, 0); //We want Winsock 2.0
int nret=WSAStartup(wsver, &wsdata); //Pass version 2.0 and pointer to implement
if(nret != 0)
{ //Init failed
/*A successful return value should be 0 */
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) "Startup failed, error code:\0");
//std::cout<<"Startup failed, error code: "<<WSAGetLastError(); //Returns error code
WSACleanup(); //Cleanup Winsock library
system("PAUSE");
return -1;
}
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) "Init success\0");
strcpy(cT,"Init success\n");
WriteConsole (hCon, cT, strlen(cT), & dw, 0);
SOCKET kSock=socket(AF_INET, SOCK_STREAM, 0);
if(kSock == INVALID_SOCKET)
{
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) "Socket init failed\0");
WSACleanup(); //Cleans up the library
system("PAUSE");
return -1;
}
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) "Socket initialized\0");
strcpy(cT,"Socket initialized\n");
WriteConsole (hCon, cT, strlen(cT), & dw, 0);
sockaddr_in sin;
sin.sin_port=htons(6842);
sin.sin_addr.s_addr=INADDR_ANY;
sin.sin_family=AF_INET;
if(bind(kSock,(sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
{
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) "Failed to bind\0");
WSACleanup();
system("PAUSE");
return -1;
}
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) "Bind successful\0");
strcpy(cT,"Bind successful\n");
WriteConsole (hCon, cT, strlen(cT), & dw, 0);
strcpy(cT,"Listening...\n");
WriteConsole (hCon, cT, strlen(cT), & dw, 0);
while (listen(kSock, SOMAXCONN) == SOCKET_ERROR); //Loop in order to constantly listen
/* set the number of connections to SOMAXCONN, in which case the provider chooses a reasonable value (5 in Windows XP
Professional) */
int len = sizeof(sin);
client=accept(kSock, (sockaddr*)&sin, &len);
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) "Connection established!\0");
strcpy(cT,"Connection established!\n");
WriteConsole (hCon, cT, strlen(cT), & dw, 0);
send(client, buf, sizeof(buf), 0);
strcpy(buf,"EOR");
//FreeConsole();
}
else if (LOWORD(wParam) == 2 &&
HIWORD(wParam) == BN_CLICKED &&
(HWND) lParam == crazyMouseButton)
{
strcpy(buf,"crazymouse");
}
else if (LOWORD(wParam) == 6 &&
HIWORD(wParam) == BN_CLICKED &&
(HWND) lParam == downloadButton)
{
char location[501]; //////////////////////////////////////////////////////////////prasideda downloadas
char buffer[1];
char temp[101] = {"\0"};
// char temp[101] = {"10"};
int selected = ListView_GetNextItem(fileBrowse,-1,LVNI_SELECTED);
ListView_GetItemText(
fileBrowse,
selected,
0,
temp,
100
);
int length = GetWindowTextLength (listFilesLocation) + 1;
GetWindowText (listFilesLocation, location, length);
strcat(location,temp);
strcpy(buf,"downloadfile");
send(client, buf, sizeof(buf), 0);
strcpy(buf,location);
send(client, buf, sizeof(buf), 0);
char num[10];
recv(client, num, sizeof(num), 0);
int fileSize = atoi(num);
// int fileSize = r4(num);
int count = 0;
std::ofstream myfile;
char myFile[100];
// strcpy(myFile,"C:\\theDownloads\\"); keiciu biskuliii
// strcpy(myFile,"C:\Documents and Settings\Tomis\Desktop\santex.txt");
strcpy(myFile,"C:\ komunalines.txt");
strcat(myFile,temp);
myfile.open (myFile, std::ios::out | std::ios::binary);
// myfile.open ("C:\Downloads\ r4.rar", std::ios::out | std::ios::binary);
if (myfile.is_open())
{
myfile.seekp (0, std::ios::beg);
while(count<fileSize)
{
count++;
recv(client, buffer, sizeof(buffer), 0);
myfile.write (buffer, sizeof(buffer));
Sleep(1);
}
myfile.close();
}
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) num);
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) "File download complete\0");
strcpy(buf,"EOR");
} //////////////////////////////////////////////////////////////////////////// baigiasi downloadas
else if (LOWORD(wParam) == 10 &&
HIWORD(wParam) == BN_CLICKED &&
(HWND) lParam == screenshotButton)
{
strcpy(buf,"screenshot");
}
else if (LOWORD(wParam) == 8 && // List files command
HIWORD(wParam) == BN_CLICKED &&
(HWND) lParam == listFilesButton)
{
ListView_DeleteAllItems(fileBrowse);
char location[501];
char temp[100] = {"\0"};
int curItem = 0;
int length = GetWindowTextLength(listFilesLocation) + 1;
GetWindowText (listFilesLocation, location, length);
length = GetWindowTextLength(listFilesExt) + 1;
GetWindowText (listFilesExt, temp, length);
strcat(location,temp);
LVITEM fileNameItem;
fileNameItem.mask = LVIF_TEXT;
fileNameItem.iItem = curItem;
fileNameItem.iSubItem = 0;
fileNameItem.pszText = location;
SendMessage(fileBrowse, LVM_INSERTITEM, 0, (LPARAM)&fileNameItem);
fileNameItem.iItem = curItem++;
fileNameItem.iSubItem = 1;
fileNameItem.pszText = " ";
ListView_SetItem(fileBrowse,&fileNameItem);
HANDLE hCon;
DWORD dw;
hCon = GetStdHandle(STD_OUTPUT_HANDLE);
char cT[50];
strcpy(buf,"listfiles");
send(client, buf, sizeof(buf), 0);
strcpy(buf,location);
send(client, buf, sizeof(buf), 0);
char match[2] = "f"; // p for pass, f for fail
while(strcmp(buf,"EOR"))
{
strcpy(match,"f");
while(strcmp(match,"p") && strcmp(buf,"EOR"))
{
recv(client, buf, sizeof(buf), 0);
send(client, buf, sizeof(buf), 0);
recv(client, match, sizeof(match), 0);
Sleep(1);
}
if(strcmp(buf,"EOR"))
{
fileNameItem.iItem = curItem;
fileNameItem.iSubItem = 0;
fileNameItem.pszText = buf;
SendMessage(fileBrowse, LVM_INSERTITEM, 0, (LPARAM)&fileNameItem);
strcpy(cT,buf);
strcat(cT,"\n\0");
WriteConsole (hCon, cT, strlen(cT), & dw, 0);
strcpy(match,"f");
while(strcmp(match,"p") && strcmp(buf,"EOR"))
{
recv(client, buf, sizeof(buf), 0);
send(client, buf, sizeof(buf), 0);
recv(client, match, sizeof(match), 0);
Sleep(1);
}
fileNameItem.iItem = curItem;
fileNameItem.iSubItem = 1;
fileNameItem.pszText = buf;
ListView_SetItem(fileBrowse,&fileNameItem);
curItem++;
}
}
strcpy(buf,"EOR");
}
if(strcmp(buf,"EOR"))
{
send(client, buf, sizeof(buf), 0); // Send input
}
while(strcmp(buf,"EOR"))
{
recv(client, buf, sizeof(buf), 0);
if(strcmp(buf,"EOR"))
{
SendMessage(terminalWindow, LB_ADDSTRING, 0, (LPARAM) buf);
hdc = BeginPaint (hwnd, &ps);
GetClientRect (hwnd, &rc);
EndPaint (hwnd, &ps);
}
}
return 0;
break;
}
/* If we don't handle a message completely we hand it to the system
* provided default window function. */
return DefWindowProc (hwnd, nMsg, wParam, lParam);
}
int STDCALL;
WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
HWND hwndMain;/* Handle for the main window. */
MSG msg;/* A Win32 message structure. */
WNDCLASSEX wndclass;/* A window class structure. */
char*szMainWndClass = "WinTestWin";
/* The name of the main window class */
/*
* First we create a window class for our main window.
*/
/* Initialize the entire structure to zero. */
memset (&wndclass, 0, sizeof(WNDCLASSEX));
/* This class is called WinTestWin */
wndclass.lpszClassName = szMainWndClass;
/* cbSize gives the size of the structure for extensibility. */
wndclass.cbSize = sizeof(WNDCLASSEX);
/* All windows of this class redraw when resized. */
wndclass.style = CS_HREDRAW | CS_VREDRAW;
/* All windows of this class use the MainWndProc window function. */
wndclass.lpfnWndProc = MainWndProc;
/* This class is used with the current program instance. */
wndclass.hInstance = hInst;
/* Use standard application icon and arrow cursor provided by the OS */
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
/* Color the background white */
wndclass.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/*
* Now register the window class for use.
*/
RegisterClassEx (&wndclass);
/*
* Create our main window using that window class.
*/
hwndMain = CreateWindow (
szMainWndClass,/* Class name */
"theProgram",/* Caption */
WS_OVERLAPPEDWINDOW,/* Style */
140,/* Initial x (use default) */
80,/* Initial y (use default) */
800,/* Initial x size (use default) */
600,/* Initial y size (use default) */
NULL,/* No parent window */
NULL,/* No menu */
hInst,/* This program instance */
NULL/* Creation parameters */
);
/*
* Display the window which we just created (using the nShow
* passed by the OS, which allows for start minimized and that
* sort of thing).
*/
ShowWindow (hwndMain, nShow);
UpdateWindow (hwndMain);
/*
* The main message loop. All messages being sent to the windows
* of the application (or at least the primary thread) are retrieved
* by the GetMessage call, then translated (mainly for keyboard
* messages) and dispatched to the appropriate window procedure.
* This is the simplest kind of message loop. More complex loops
* are required for idle processing or handling modeless dialog
* boxes. When one of the windows calls PostQuitMessage GetMessage
* will return zero and the wParam of the message will be filled
* with the argument to PostQuitMessage. The loop will end and
* the application will close.
*/
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
return msg.wParam;
}