?? workingsocket.cpp
字號:
#include "WorkingSocket.h"
#include "UDPHandler.h"
#include <iostream>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
extern CUDPHandler m_UDPHandler;
struct FileInfo {
char szFileName[120];
unsigned short type;
FileInfo *Next;
};
typedef FileInfo * LPFileInfo;
CWorkingSocket::CWorkingSocket() : SocketError(false)
{
HKEY hkey;
DWORD disposition, length,type;
RegCreateKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\ImageServer", NULL,
NULL, KEY_QUERY_VALUE, 0, NULL, &hkey, &disposition);
if (disposition == REG_CREATED_NEW_KEY)
{
RegSetValueEx(hkey, "WorkPath", 0, REG_SZ, (BYTE *)"", 1);
RegSetValueEx(hkey, "FtpUser", 0, REG_SZ, (BYTE *)"anonymous", 10);
RegSetValueEx(hkey, "FtpPwd", 0, REG_SZ, (BYTE *)"img@723.com", 11);
szRootDir[0] = 0;
RegCloseKey(hkey);
}
else
{
RegCloseKey(hkey);
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\ImageServer", 0,
KEY_QUERY_VALUE | KEY_WRITE, &hkey);
length = 260;
if (RegQueryValueEx(hkey, "WorkPath", 0, &type, (BYTE*)&szRootDir, &length) != ERROR_SUCCESS)
{
RegSetValueEx(hkey, "WorkPath", 0, REG_SZ, (BYTE*)"", 1);
szRootDir[0] = 0;
}
length = 21;
if (RegQueryValueEx(hkey, "FtpUser", 0, &type, (BYTE*)&ftpUser, &length) != ERROR_SUCCESS)
{
RegSetValueEx(hkey, "FtpUser", 0, REG_SZ, (BYTE*)"anonymous", 10);
strcpy(ftpUser, "anonymous");
}
length = 21;
if (RegQueryValueEx(hkey, "FtpPwd", 0, &type, (BYTE*)&ftpPwd, &length) != ERROR_SUCCESS)
{
RegSetValueEx(hkey, "FtpPwd", 0, REG_SZ, (BYTE*)"img@723.com", 11);
strcpy(ftpPwd, "img@723.com");
}
RegCloseKey(hkey);
}
}
void CWorkingSocket::Run()
{
//首先接一個包以確定這次TCP連接要做的工作
int CmdFlag = 0;
char szIP[17];
char msg[80];
do
{
int i=Receive(szReceive, TCP_RECEIVE_MAX);
if (i<=0)
break;
CmdFlag = *szReceive;
switch(CmdFlag)
{
case CMD_UDP_PORT:
iUDPPort = atoi(szReceive + 1);
m_UDPHandler.InsertNode(GetRemoteIP(szIP), iUDPPort);
SendFtpAccount();
break;
case CMD_GET_INDEX:
if (!DoExchange()) SocketError = true;
break;
}
} while ((CmdFlag != CMD_EXIT) && (!SocketError));
strcpy(msg, "用戶從“");
strcat(msg, GetRemoteIP(szIP));
strcat(msg, "”退出!");
CSocketBase::SendUDP(msg, strlen(msg) + 1, "127.0.0.1", UDP_LISTEN_PORT);
m_UDPHandler.DeleteNode(szIP, iUDPPort);
Close();
delete this;
}
bool CWorkingSocket::DoExchange()
{
HANDLE hFile;
WIN32_FIND_DATA FindFileData;
int iCount;
char *p;
LPFileInfo lpfi1, lpfi2, pHead = NULL, pNew;
string FullPath;
bool FindNext = false;
FullPath = szRootDir;
FullPath += szReceive + 1;
FullPath += "*.*";
hFile = FindFirstFile(FullPath.c_str(), &FindFileData);
if (hFile == INVALID_HANDLE_VALUE)
{
szSend[0] = '1';
if (Send(szSend, 1) <= 0) return false;
}
else
FindNext = true;
while (FindNext)
{
if (strcmp(FindFileData.cFileName, ".") &&
strcmp(FindFileData.cFileName, ".."))
{
pNew = new FileInfo();
if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
// *(p++) = ITEM_ADD_DIR;
pNew->type = ITEM_ADD_DIR;
else
// *(p++) = ITEM_ADD_FILE;
pNew->type = ITEM_ADD_FILE;
// strcpy(p, FindFileData.cFileName);
// p += strlen(FindFileData.cFileName) + 1;
strcpy(pNew->szFileName, FindFileData.cFileName);
lpfi1 = pHead;
lpfi2 = NULL;
while(lpfi1 != NULL)
{
if (lpfi1->type > pNew->type) break;
if (lpfi1->type < pNew->type)
{
lpfi2 = lpfi1;
lpfi1 = lpfi1->Next;
continue;
}
if (_stricmp(lpfi1->szFileName, pNew->szFileName) < 0)
{
lpfi2 = lpfi1;
lpfi1 = lpfi1->Next;
}
else
break;
}
if (lpfi2 == NULL)
{
pHead = pNew;
pNew->Next = lpfi1;
}
else
{
lpfi2->Next = pNew;
pNew->Next = lpfi1;
}
}
FindNext = (FindNextFile(hFile, &FindFileData) != 0);
/* if (!FindNext)
{
*szSend = '1';
if (Send(szSend, p - szSend)<=0) return false;
}
else
{
if (iCount == EXCHANGE_ITEMS_PERTIME)
{
iCount = 0;
*szSend = '0';
if (Send(szSend, p - szSend)<=0) return false;
p = szSend + 1;
}
}*/
}
FindClose(hFile);
// FILE *file;
// file = fopen("c:\\overmas1.txt", "w");
lpfi1 = pHead;
p = szSend + 1;
iCount = 0;
while (lpfi1 != NULL)
{
// fwrite(lpfi1->szFileName, 1, strlen(lpfi1->szFileName), file);
// fwrite("\n", 1, 1, file);
*(p++) = lpfi1->type;
strcpy(p, lpfi1->szFileName);
p += strlen(lpfi1->szFileName) + 1;
lpfi1 = lpfi1->Next;
iCount++;
if (iCount == EXCHANGE_ITEMS_PERTIME)
{
iCount = 0;
if (lpfi1 != NULL)
*szSend = '0';
else
*szSend = '1';
if (Send(szSend, p - szSend) <= 0) return false;
p = szSend + 1;
}
}
if (iCount > 0)
{
*szSend = '1';
if (Send(szSend, p - szSend) <= 0) return false;
}
lpfi1 = pHead;
while (lpfi1 != NULL)
{
lpfi2 = lpfi1;
lpfi1 = lpfi1->Next;
delete lpfi2;
}
// fclose(file);
return true;
}
bool CWorkingSocket::SendFtpAccount()
{
int length;
szSend[0] = CMD_FTP_ACCOUNT;
length = 1;
strcpy(szSend + length, ftpUser);
length += strlen(ftpUser) + 1;
strcpy(szSend + length, ftpPwd);
length += strlen(ftpPwd) + 1;
if (Send(szSend, length) <= 0) return false;
return true;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -