用浏览器浏览图片

赵刚豪
2023-12-01
#include <windowsx.h>
#include <Winsock2.h>
#include <ShlObj.h>
#include <assert.h>
#include <io.h>
#include <ctime>
using namespace std;
#include "resource.h"
#pragma comment (lib,"ws2_32")
 
#define WM_SOCKET WM_USER +1024
 
 
char g_szAllFileName[256][64];
char g_szDirPath[256];
int g_nFileCount=0;
int g_nNext=0;
 
LRESULT DlgProc(HWND hwnd,UINT uMessage,
            WPARAM wParam,LPARAM lParam);
DWORD WINAPI ThreadProc(LPVOID lParam);
SOCKET InitListenSock(UINT nPort);
int  InitDirFileName( char *szDirPath);
int EnumAllFile( char *szDirPath);
 
int WINAPI WinMain(IN HINSTANCE hInstance, IN HINSTANCE hPrevInstance,
               IN LPSTR lpCmdLine, IN int nShowCmd )
{
   ::DialogBox(hInstance,LPCTSTR(IDD_MYDLG),
      NULL,DLGPROC(DlgProc));
   return TRUE;
}
LRESULT DlgProc(HWND hwnd,UINT uMessage,
            WPARAM wParam,LPARAM lParam)
{
   static SOCKET listenSock;
   static WSADATA wsaData;
   static HANDLE pThread[64];
   static UINT nThreadCount=0;
   static int len= sizeof (SOCKADDR_IN);
   static UINT uPort=5555;
    static char szDirPath[MAX_PATH];
   static char szGetBuf[1024];
   static char szIP[32];
   static char szPort[6];
   static int nBufLen=0;
   static BROWSEINFO bi;
   static LPITEMIDLIST pidl;
 
   SOCKADDR_IN newSockaddr;  
   SOCKET newAccept;
   switch (uMessage) {
   case WM_INITDIALOG:
     
      srand(( unsigned int )time(NULL));
      SetWindowText(GetDlgItem(hwnd,IDC_EDT_PORT),"5555");
     
      SetWindowText(GetDlgItem(hwnd,IDC_EDT_FILEDIR),
         "F://NET");   
      SetWindowText(GetDlgItem(hwnd,IDC_EDT_IP),"http://127.0.0.1:5555");
      WSAStartup(MAKEWORD(2,2),&wsaData);        
      return TRUE;
     
   case WM_CLOSE: 
      closesocket(listenSock);
      WSACleanup();
      EndDialog(hwnd,wParam);
      PostQuitMessage(0);
      return TRUE;
   case WM_COMMAND:
      switch (LOWORD(wParam))
      {
      case IDC_BT_CHOOSE:        
         ZeroMemory(&bi, sizeof (bi));
         bi.hwndOwner=NULL;
         bi.lpszTitle="选择文件夹";
         bi.ulFlags=BIF_RETURNONLYFSDIRS;
         pidl = SHBrowseForFolder(&bi);      
         ZeroMemory(szDirPath,MAX_PATH);
         SHGetPathFromIDList(pidl, szDirPath);
         SetWindowText(GetDlgItem(hwnd,IDC_EDT_FILEDIR),
            szDirPath);
         return TRUE;
      case IDOK:           
 
         GetWindowText(GetDlgItem(hwnd,IDC_EDT_FILEDIR),szDirPath,256);
         GetWindowText(GetDlgItem(hwnd,IDC_EDT_PORT),szPort,6);
         strcpy(szIP,"http://127.0.0.1:");
         strcat(szIP,szPort);
         SetWindowText(GetDlgItem(hwnd,IDC_EDT_IP),szIP);
           
         if ((g_nFileCount =InitDirFileName(szDirPath))==0)
            exit(0);
          
         uPort=atoi(szPort);
         listenSock=InitListenSock(uPort);
         if (WSAAsyncSelect(listenSock,hwnd,WM_SOCKET,FD_ACCEPT|FD_CLOSE)
            ==SOCKET_ERROR )
         {
            MessageBox(NULL,"SAAsyncSelect",NULL,MB_OK);
            exit(0);
         }
         if (listen(listenSock,5)==SOCKET_ERROR)
         {
            MessageBox(NULL,"listen",NULL,MB_OK);
            exit(0);
         }
        
         ::EnableWindow(::GetDlgItem(hwnd,IDOK), false );
     
         return TRUE;
      case IDCANCEL:
         EndDialog(hwnd,0);
         return TRUE;
      }
      break ;
   case WM_SOCKET:
      if (WSAGETSELECTERROR(lParam))
      {
         closesocket((SOCKET)wParam);
         return TRUE;
      }
      switch (WSAGETSELECTEVENT(lParam)) {          
            case FD_ACCEPT:              
            if ((newAccept=accept(listenSock,(sockaddr*)&newSockaddr,&len))==
                INVALID_SOCKET)
            {              
                exit(0);
            }
            WSAAsyncSelect(newAccept,hwnd,WM_SOCKET,FD_CLOSE|FD_READ|FD_WRITE);
            return TRUE;
         case FD_READ:           
            nBufLen= recv((SOCKET)wParam,szGetBuf,1024,0);
            szGetBuf[nBufLen]='/0';          
               return TRUE;
         case FD_WRITE:
            if ((pThread[nThreadCount] = CreateThread(NULL,0,
                ThreadProc,(LPVOID)wParam, 0, NULL))!=NULL)
                ++nThreadCount;
            if (nThreadCount==64)
            {
                nThreadCount=0;
            }
            return TRUE;
         case FD_CLOSE:
            MessageBox(NULL,"CLOSE",NULL,MB_OK);
            closesocket((SOCKET)wParam);           
            return TRUE;
      }
      return TRUE;
   }
   return FALSE;
}
SOCKET InitListenSock(UINT uPort)
{
   SOCKET listenSock;
   int iRet=0;
   if ((listenSock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP))==INVALID_SOCKET)
   {
      MessageBox(NULL,"socket","错误",MB_OK);
      exit(1);
   }
 
   SOCKADDR_IN listenAddr;
   listenAddr.sin_family=AF_INET;
   listenAddr.sin_port=htons(uPort);
   listenAddr.sin_addr.s_addr=htonl(INADDR_ANY); 
  
   iRet=bind(listenSock,(sockaddr*)&listenAddr, sizeof (listenAddr));
   if (iRet==SOCKET_ERROR) 
   {
      MessageBox(NULL,"bind","错误",MB_OK);
      exit(1);
   }
   return listenSock;
}
 
int  InitDirFileName( char *szDirPath)
{  
   char *pStr=g_szDirPath;
   while (*szDirPath!='/0')
   {
      if (*szDirPath=='//')
      {
         *pStr='//';
         ++pStr;
         *pStr='//';
      }
      else
         *pStr=*szDirPath;
      ++szDirPath;
      ++pStr;
   }
   *pStr='//';
   ++pStr;
   *pStr='//';
   ++pStr;
   *pStr='/0';
 
   return EnumAllFile(g_szDirPath);
}
int EnumAllFile( char *szDirPath)
{
 
   int nCount=0;
   WIN32_FIND_DATA fd;
   ZeroMemory(&fd, sizeof (WIN32_FIND_DATA));
   BOOL bFind=TRUE;
   char szTempDirPath[256];
   strcpy(szTempDirPath,szDirPath);
   strcat(szTempDirPath,"*");
   HANDLE hFind=FindFirstFile(szTempDirPath,&fd);
   assert(hFind);
 
   while ( bFind &&nCount<256)
   {
      if ( FindNextFile(hFind, &fd) )
      {
         if ( (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            && strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
            continue ;
         else if ( strcmp(fd.cFileName, ".") && strcmp(fd.cFileName, "..") )
         {
            strcpy(g_szAllFileName[nCount],fd.cFileName);       
            ++nCount;
         }
      }
      else         
         bFind=FALSE;   
   } 
   return nCount;
}
DWORD WINAPI ThreadProc(LPVOID lParam)
{
   int iRet=0;
   char szFileName[256];
   strcpy(szFileName,g_szDirPath);
   if (g_nNext==g_nFileCount)
   {
      g_nNext=0;
   }
   strcat(szFileName,g_szAllFileName[g_nNext]);
   ++g_nNext;
 
   int file =open(szFileName,0x100|002);
 
   int len;
   char *buf=NULL;
 
   len=filelength(file);
   buf=( char *)malloc(len);
   if (buf==NULL)
   {
      free(buf); 
      exit(0);
   }
   read(file,buf,len);
 
 
   SOCKET newAccept=(SOCKET)lParam;
 
   if ((iRet=send(newAccept,buf,len,0))==SOCKET_ERROR)
   {
      free(buf); 
      exit(0);
   }
 
   shutdown(newAccept,SD_RECEIVE);
   closesocket(newAccept);
   free(buf); 
 
   return TRUE;
}
 
 类似资料: