// SendFileServer.cpp : Defines the entry point for the application.
//
#include "stdafx.h";
#include <winsock2.h>
#include <commctrl.h>
#include <commdlg.h>
#include <utility.h>
#include <stdlib.h>
#include <winuser.h>
#define toip(l,s) sprintf(s,"%d.%d.%d.%d",*((BYTE*)&l),(*((BYTE*)&l+1)),(*((BYTE*)&l+2)),(*((BYTE*)&l+3)))
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
HINSTANCE hInst; // current instance
HFONT hFont;
HWND hWnd; // Main Window Handle
HWND txtReIp, txtRePort, cmdApply, cmbUnit, txtPackSize, cmdListen, txtStatus, cmdSend, cmdBrowse, txtPath, txtPort, Label1;
SOCKET sock, client;
sockaddr_in name;
char u[5];
char t[5];
char ts[1000];
char ts2[255];
char speedstr[100];
hostent* host;
bool ct = false;
BYTE recving = 0;
DWORD ret, ret2;
DWORD numbyte=0;
HANDLE file, hListen, hSend;
BYTE namelen,c;
char remname[255];
char path[300];
ULONG size, packetsize, ps;
float size2;
double speed, time;
LONG n = 0;
OPENFILENAME ofn;
char filetitle[254];
char filter[254];
void showerr (char* s)
{
char buf[150];
char buf2[200];
DWORD n = GetLastError();
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, n, LANG_NEUTRAL, buf, 200, NULL);
if (s)
sprintf (buf2, "%sError %d: %s", s, n, buf);
else
sprintf (buf2, "Error %d: %s", n, buf);
settext (txtStatus, &buf2[0]);
}
void count ()
{
time = 0;
ULONG prevbyte;
float left;
char u[5];
char t[5];
while (recving == 1 || hSend)
{
prevbyte = numbyte;
Sleep (1000);
time+=1;
speed = (double) numbyte - prevbyte;
left = (double) (size-numbyte) / speed;
if (left > 3600)
{
left = (double) left / 3600;
sprintf (&t[0], "hour");
}
else if (left > 60)
{
left = (double) left / 60;
sprintf (&t[0], "min");
}
else
sprintf (&t[0], "sec");
if (speed >= 1024 * 1024 * 1024)
{
speed = (double) speed / 1024 / 1024 / 1024;
sprintf (&u[0], "GB");
}
else if (speed >= 1024*1024)
{
speed = (double) speed / 1024 / 1024;
sprintf (&u[0], "MB");
}
else if (speed >= 1024)
{
speed = (double) speed / 1024;
sprintf (&u[0], "KB");
}
else
sprintf (&u[0], "byte");
sprintf (&speedstr[0], "%.1f %s/sec %.1f %s", speed, u, left, t);
};
}
void SendThread ()
{
char* buf = (char*) HeapAlloc (GetProcessHeap (), NULL, 5242879);
file = CreateFile (&ts2[0], GENERIC_READ, 0, 0, OPEN_EXISTING, 0, NULL);
if (file == INVALID_HANDLE_VALUE)
settext (txtStatus, "Cannot openfile");
else
{
numbyte = 0;
do
{
ret = ReadFile (file, &buf[0], packetsize, &ret2, NULL);
numbyte+=send (client, &buf[0], ret2, 0);
sprintf (&ts[0], "%.3f %% %s", (double) numbyte/size*100, speedstr);
settext (txtStatus, &ts[0]);
} while (!(ret && ret2 == 0));
CloseHandle (file);
file = 0;
speed = (double) size / time;
if (speed >= 1024 * 1024 * 1024)
{
speed = (double) speed / 1024 / 1024 / 1024;
sprintf (&u[0], "GB");
}
else if (speed >= 1024*1024)
{
speed = (double) speed / 1024 / 1024;
sprintf (&u[0], "MB");
}
else if (speed >= 1024)
{
speed = (double) speed / 1024;
sprintf (&u[0], "KB");
}
else
sprintf (&u[0], "byte");
if (time > 3600)
{
time = (double) time / 3600;
sprintf (&t[0], "hour");
}
else if (time > 60)
{
time = (double) time / 60;
sprintf (&t[0], "min");
}
else
sprintf (&t[0], "sec");
sprintf (&ts[0], "Complete %.1f %s %.1f %s/sec", time, t, speed, u);
settext (txtStatus, &ts[0]);
};
numbyte = 0;
hSend = 0;
};
void ListenThread ()
{
recving = 0;
name.sin_port = htons (atoi (&ts[0]));
name.sin_family = AF_INET;
sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
gethostname (&ts[0], 500);
host = gethostbyname (&ts[0]);
memcpy (&name.sin_addr.S_un.S_addr, host->h_addr_list[0], sizeof (name.sin_addr.S_un.S_addr));
toip (name.sin_addr.S_un.S_addr, &ts[0]);
if (bind (sock, (sockaddr*) &name, sizeof (name)))
showerr ("Error in bind");
else
{
settext (cmdListen, "Close");
settext (txtStatus, "Listening...");
ct = true;
if (listen (sock, 1))
{
ct = false;
showerr ("Error in listen");
}
else
{
ret = sizeof (name);
client = accept (sock, (sockaddr*) &name, (int*) &ret);
if (client == INVALID_SOCKET)
{
ct = false;
settext (cmdListen, "Listen");
if (WSAGetLastError () != 10004)
showerr ("Error in accept");
else
settext (txtStatus, "Disconnected");
}
else
{
closesocket (sock);
settext (txtStatus, "Client connected");
toip (name.sin_addr.S_un.S_addr, &ts[0]);
settext (txtReIp, &ts[0]);
sprintf (&ts[0], "%d", ntohs (name.sin_port));
settext (txtRePort, &ts[0]);
char* buf = (char*) HeapAlloc (GetProcessHeap (), NULL, 5242879);
do
{
n = recv (client, buf, 5242879, 0);
if (n > 0)
{
if (!recving)
{
namelen = buf[0];
memcpy (&remname[0], &buf[1], namelen);
remname[namelen] = 0;
memcpy (&size, &buf[1+namelen], 4);
if (size >= 1024 * 1024 * 1024)
{
size2 = (float) size / 1024 / 1024 / 1024;
sprintf (&ts[0], "%s\n%.3f GB\nReceive this file ?", remname, size2);
}
else if (size >= 1024*1024)
{
size2 = (float) size / 1024 / 1024;
sprintf (&ts[0], "%s\n%.3f MB\nReceive this file ?", remname, size2);
}
else if (size >= 1024)
{
size2 = (float) size / 1024;
sprintf (&ts[0], "%s\n%.3f KB\nReceive this file ?", remname, size2);
}
else
sprintf (&ts[0], "%s\n%d byte\nReceive this file ?", remname, size);
if (MessageBox (NULL, &ts[0], "SendFile", MB_YESNO) == IDYES)
{
memcpy (&path[0], &remname[0], namelen);
path[namelen] = 0;
sprintf (&filter[0], "All files (*.*)\x0*.*\x0\x0");
ofn.lStructSize = sizeof (ofn);
ofn.lpstrFilter = filter;
ofn.hwndOwner = hWnd;
ofn.lpstrFile = path;
ofn.nMaxFile = 255;
ofn.lpstrFileTitle = filetitle;
ofn.nMaxFileTitle = 255;
ofn.Flags = NULL;
if (GetSaveFileName (&ofn))
{
file = CreateFile (&path[0], GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, 0, NULL);
sprintf (&ts[0], "\x1Yes");
send (client, &ts[0], strlen (ts), 0);
CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) count, 0, 0, 0);
recving = 1;
}
else
{
sprintf (&ts[0], "\x1No");
send (client, &ts[0], strlen (ts), 0);
};
}
else
{
sprintf (&ts[0], "\x1No");
send (client, &ts[0], strlen (ts), 0);
};
}
else if (recving == 1)
{
WriteFile (file, &buf[0], n, &ret, NULL);
numbyte += n;
sprintf (&ts[0], "%.3f %% %s", (float) numbyte/size*100, speedstr);
settext (txtStatus, &ts[0]);
if (numbyte >= size)
{
settext (txtStatus, "Receive complete");
recving = 0;
CloseHandle (file);
file = 0;
numbyte = 0;
speed = (float) size / time;
if (speed >= 1024 * 1024 * 1024)
{
speed = (float) speed / 1024 / 1024 / 1024;
sprintf (&u[0], "GB");
}
else if (speed >= 1024*1024)
{
speed = (float) speed / 1024 / 1024;
sprintf (&u[0], "MB");
}
else if (speed >= 1024)
{
speed = (float) speed / 1024;
sprintf (&u[0], "KB");
}
else
sprintf (&u[0], "byte");
if (time > 3600)
{
time = (float) time / 3600;
sprintf (&t[0], "hour");
}
else if (time > 60)
{
time = (float) time