// web.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "shlobj.h"
#include "windows.h"
#include "winsock2.h"
#include "stdio.h"
#include "fcntl.h"
#include "io.h"
#include "string.h"
#include "stdlib.h"
BROWSEINFO lpbrowseinfo;
HWND hwndport;
HWND hwndpath;
HWND hwndstart;
struct _ITEMIDLIST *folder;
char path[100];
char cport[6];
int iport;
int stflag=0;
int response(char *url,char *head,char *webbuf)
{
char addr[200];
int weblen;
FILE *fp;
char *buf=(char*)malloc(500000*sizeof(char));
strcpy(addr,path);
strcat(addr,url);
fp=fopen(addr,"rb");
if(fp==NULL)
{
fp=fopen("notfound.htm","rb");
sprintf(head,"HTTP/1.0 404 Not Found/n/n");
}
else
sprintf(head,"HTTP/1.0 200 OK/n/n");
weblen=fread(webbuf,sizeof(char),500000,fp);
fclose(fp);
return weblen;
}
DWORD WINAPI ThreadProc(LPVOID socket)
{
char buf[1024];
int buflen=1024;
int headlen;
char url[100];
char get[5];
char head[100];
int weblen=512000;
char *webbuf=(char*)malloc(sizeof(char)*512000);
recv((SOCKET)socket,buf,buflen,0);
sscanf(buf,"%s%s",get,url);
weblen=response(url,head,webbuf);
headlen=strlen(head);
send((SOCKET)socket,head,headlen,0);
send((SOCKET)socket,webbuf,weblen,0);
closesocket((SOCKET)socket);
return 1;
}
DWORD WINAPI Web(LPVOID lpParameter)
{
WSADATA wsadata;
sockaddr_in addr;
sockaddr_in client;
SOCKET threadsock;
int threadaddr;
int lis;
int wsa=WSAStartup(0x0202,&wsadata);
if(wsa!=0)
MessageBox(0,"wsa error","2",MB_OK);
SOCKET tcpsock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
addr.sin_family=AF_INET;
addr.sin_port=htons(iport);
addr.sin_addr.S_un.S_addr=htonl(INADDR_ANY);
int soc=bind(tcpsock,(LPSOCKADDR)&addr,sizeof(addr));
if(soc!=0)
MessageBox(0,"sock error","2",MB_OK);
while(stflag==1){
lis=listen(tcpsock,SOMAXCONN);
if(lis!=0)
MessageBox(0,"listen error","2",MB_OK);
threadaddr=sizeof(struct sockaddr);
threadsock=accept(tcpsock,(struct sockaddr *)&client,&threadaddr);
if(stflag==1)
CreateThread(0,0,ThreadProc,(void*)threadsock,0,0);
}
WSACleanup();
return 1;
}
void WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
if(uMsg==WM_DESTROY)
PostQuitMessage(NULL);
else if(uMsg==WM_COMMAND){
if(lParam!=0)
if(wParam==20)
{
if(stflag==0)
{
SendMessage(hwndport,WM_GETTEXT,6,(long)cport);
EnableWindow(hwndstart,0);
iport=atoi(cport);
stflag=1;
CreateThread(0,0,Web,NULL,0,0);
}
}
else if(wParam==30)
{
EnableWindow(hwndstart,1);
stflag=0;
}
else if(wParam==10)
{
folder=SHBrowseForFolder(&lpbrowseinfo);
SHGetPathFromIDList(folder,path);
SendMessage(hwndpath,WM_SETTEXT,0,(long)path);
}
}
else
DefWindowProc(hWnd,uMsg,wParam,lParam);
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wc;
MSG msg;
HWND hwnd;
char ClassName[20]="SimpleWinClass";
int quit;
wc.cbSize=sizeof(WNDCLASSEX);
wc.style=CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc=(WNDPROC)WndProc;
wc.cbClsExtra=NULL;
wc.cbWndExtra=NULL;
wc.hInstance=hInstance;
wc.hbrBackground=(HBRUSH)(1);
wc.lpszMenuName=NULL;
wc.lpszClassName=ClassName;
wc.hIcon=0;
wc.hIconSm=wc.hIcon;
wc.hCursor=0;
RegisterClassEx(&wc);
hwnd=CreateWindowEx(NULL,ClassName, "Web Server By zw", WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, 500,180, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);
hwndpath=CreateWindowEx(WS_EX_CLIENTEDGE, "edit",NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_AUTOHSCROLL, 60,23,350,25,hwnd,NULL,hInstance,NULL);
hwndport=CreateWindowEx(WS_EX_CLIENTEDGE, "edit",cport, WS_CHILD | WS_VISIBLE | WS_BORDER | ES_RIGHT | ES_AUTOHSCROLL, 60,60,50,25,hwnd,NULL,hInstance,NULL);
CreateWindowEx(0, "static","Port:", WS_CHILD | WS_VISIBLE, 18,62,40,25,hwnd,NULL,hInstance,NULL);
CreateWindowEx(0, "static","Path:", WS_CHILD | WS_VISIBLE, 18,25,40,25,hwnd,NULL,hInstance,NULL);
CreateWindowEx(NULL, "button","Browse",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,423,23,60,25,hwnd,(struct HMENU__ *)10,hInstance,NULL);
hwndstart=CreateWindowEx(NULL, "button","Start",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,320,110,60,25,hwnd,(struct HMENU__ *)20,hInstance,NULL);
CreateWindowEx(NULL, "button","Stop",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,410,110,60,25,hwnd,(struct HMENU__ *)30,hInstance,NULL);
lpbrowseinfo.hwndOwner=hwnd;
lpbrowseinfo.lpszTitle="请选择目录:";
lpbrowseinfo.pidlRoot=0;
lpbrowseinfo.ulFlags=1;
while(1)
{
quit=GetMessage(&msg,NULL,0,0);
if(quit==0)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}