📄 qftplib.cpp
字号:
if (nCharRecv == 0)
{
_lclose(hFile);
ReadFTPServerReply(hControlSocket);
}
return(nCharRecv);
}
extern "C" void _export FAR PASCAL ExtractFileName(LPSTR lpPathString,
LPSTR lpszFileName)
{
LPSTR lp; // General purpose pointer
UINT i; // General purpose index
UINT iExtLength = 0; // Length of file extension (following the dash)
// Find the last forward slash (assume UNIX/NT directory conventions)
if (lp = _fstrrchr(lpPathString, '/'))
lp++;
else
lp = lpPathString;
// Loop while the length of the name is less than 8 characters
// (a valid DOS length), the character is not a dot and the
// pointer is valid
for (i = 0; i < 8 && *lp != '.' && lp; i++)
*(lpszFileName+i) = *(lp++);
// Add a dot to mark the start of the file extension
*(lpszFileName+i) = '.';
i++;
// Find the last dot and use up to the next three characters for
// the new file name.
if (lp = _fstrrchr(lpPathString, '.'))
for (iExtLength = 0; iExtLength < 3 && (*(lp)); iExtLength++)
*(lpszFileName+(i++)) = *(++lp);
// Null-terminate the new file name
*(lpszFileName+i) = '\0';
return;
}
extern "C" HFILE _export FAR PASCAL CreateTransferFile(LPSTR lpszFileName)
{
HFILE hFile; // File handle for data file
OFSTRUCT openFileBuff; // The Windows open file data structure
lpszFunctionName = "CreateTransferFile";
if ((hFile = OpenFile(lpszFileName, (OFSTRUCT far *)&openFileBuff,
OF_CREATE)) == HFILE_ERROR)
{
_lclose(hFile);
wsprintf(gszCommandBuffer,"Error creating file: %s\n", lpszFileName);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName, MB_OK|MB_ICONSTOP);
}
return(hFile);
}
extern "C" SOCKET _export FAR PASCAL ConnectFTPControlSocket(LPSTR lpszHost)
{
LPHOSTENT lpHostEnt; // Internet host information structure
SOCKADDR_IN sockAddr; // Socket address structure
LPSERVENT lpServEnt; // Service information structure
short nProtocolPort; // Protocol port
int nConnect; // Socket connection results
SOCKET hControlSocket = INVALID_SOCKET;
lpszFunctionName = "ConnectFTPControlSocket";
if (!(lpHostEnt = gethostbyname(lpszHost)))
{
int iWinsockErr = WSAGetLastError();
wsprintf(gszCommandBuffer,
"Error #%d while resolving address for %s",
iWinsockErr, lpszHost);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName,
MB_OK|MB_ICONSTOP);
return(INVALID_SOCKET);
}
if ((hControlSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
int iWinsockErr = WSAGetLastError();
wsprintf(gszCommandBuffer,
"Error #%d occurred while creating socket!!",
iWinsockErr);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName,
MB_OK|MB_ICONSTOP);
return(INVALID_SOCKET);
}
lpServEnt = getservbyname("ftp", DEFAULT_PROTOCOL);
if (lpServEnt == NULL)
nProtocolPort = htons(IPPORT_FTP);
else
nProtocolPort = lpServEnt->s_port;
// Define the socket address
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = nProtocolPort;
sockAddr.sin_addr = *((LPIN_ADDR)*lpHostEnt->h_addr_list);
// Connect the socket
if( nConnect = connect(hControlSocket, (LPSOCKADDR)&sockAddr,
sizeof(sockAddr)))
{
int iWinsockErr = WSAGetLastError();
wsprintf(gszCommandBuffer,
"Error #%d occurred while connecting socket!!",
iWinsockErr);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName,
MB_OK|MB_ICONSTOP);
return(INVALID_SOCKET);
}
// Make the control socket non-blocking before returning.
IsReadyToRead(hControlSocket);
if (ReadFTPServerReply(hControlSocket) >= 400)
return(INVALID_SOCKET);
else
return(hControlSocket);
}
extern "C" SOCKET FAR PASCAL RequestDataConnection(SOCKET hControlSocket, SOCKET hListenSocket)
{
SOCKADDR_IN sockAddr; // Socket address structure
int iLength; // Length of the address structure
UINT nLocalPort; // Local port for listening
UINT nReplyCode; // FTP server reply code
lpszFunctionName = "RequestDataConnection";
// Get the address for the hListenSocket
iLength = sizeof(sockAddr);
if (getsockname(hListenSocket, (LPSOCKADDR)&sockAddr,
&iLength) == SOCKET_ERROR)
{
int iWinsockErr = WSAGetLastError();
wsprintf(gszCommandBuffer,
"Error #%d occurred while getting listen socket name!!",
iWinsockErr);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName,
MB_OK|MB_ICONSTOP);
return(INVALID_SOCKET);
}
// Extract the local port from the hListenSocket
nLocalPort = sockAddr.sin_port;
// Now, reuse the socket address structure to
// get the IP address from the control socket.
if (getsockname(hControlSocket, (LPSOCKADDR)&sockAddr,
&iLength) == SOCKET_ERROR)
{
int iWinsockErr = WSAGetLastError();
wsprintf(gszCommandBuffer,
"Error #%d occurred while getting control socket name!!",
iWinsockErr);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName,
MB_OK|MB_ICONSTOP);
return(INVALID_SOCKET);
}
// Format the PORT command with the correct numbers.
wsprintf(gszCommandBuffer, "PORT %d,%d,%d,%d,%d,%d\r\n",
sockAddr.sin_addr.S_un.S_un_b.s_b1,
sockAddr.sin_addr.S_un.S_un_b.s_b2,
sockAddr.sin_addr.S_un.S_un_b.s_b3,
sockAddr.sin_addr.S_un.S_un_b.s_b4,
// Remember, the port is in network byte order. The FTP server
// expects to see the high-order byte from the port (in terms of
// the network byte order) first. As such, from a PC, (which uses
// little endian byte order) the program must transmit the low-order
// byte first and then the high-order. (If this is confusing, see the
// "Internet Programming" chapter entitled "Time and the Network
// Byte Order".
nLocalPort & 0xFF,
nLocalPort >> 8);
// Tell the server which port to use for data.
if (nReplyCode = SendFTPCommand(hControlSocket, gszCommandBuffer)
!= 200)
{
wsprintf(gszCommandBuffer,
"Error %d from PORT command to server!",
nReplyCode);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName,
MB_OK|MB_ICONSTOP);
return(INVALID_SOCKET);
}
else
return(hListenSocket);
}
extern "C" SOCKET _export FAR PASCAL CreateListenSocket(SOCKET hControlSocket)
{
SOCKADDR_IN sockAddr; // Socket address structure
SOCKET hListenSocket; // Handle for the listener socket
lpszFunctionName = "CreateListenSocket";
if ((hListenSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
int iWinsockErr = WSAGetLastError();
wsprintf(gszCommandBuffer,
"Error #%d occurred while creating socket!!",
iWinsockErr);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName,
MB_OK|MB_ICONSTOP);
return(INVALID_SOCKET);
}
// Let the system assign a socket address
sockAddr.sin_family = AF_INET;
sockAddr.sin_port = htons(0); // htons() is just a reminder.
sockAddr.sin_addr.s_addr = INADDR_ANY;
// Bind the socket
if (bind(hListenSocket, (LPSOCKADDR)&sockAddr, sizeof(sockAddr)))
{
int iWinsockErr = WSAGetLastError();
wsprintf(gszCommandBuffer,
"Error #%d occurred while binding socket!!",
iWinsockErr);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName,
MB_OK|MB_ICONSTOP);
return(INVALID_SOCKET);
}
// Listen for the FTP server connection
if (listen(hListenSocket, QUEUE_SIZE))
{
int iWinsockErr = WSAGetLastError();
wsprintf(gszCommandBuffer,
"Error %d on listen socket!!",
iWinsockErr);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName,
MB_OK|MB_ICONSTOP);
return(INVALID_SOCKET);
}
// Ask the server to connect to the port monitored by the listener socket
return(RequestDataConnection(hControlSocket, hListenSocket));
}
extern "C" SOCKET _export FAR PASCAL AcceptDataConnection(SOCKET hListenSocket)
{
SOCKET hDataSocket; // Handle for socket that receives data
SOCKADDR_IN sockAddr; // Socket address structure
int iAddrLength; // The length of the socket address
lpszFunctionName = "AcceptDataConnection";
hDataSocket = accept(hListenSocket, (LPSOCKADDR)&sockAddr,
&iAddrLength);
// Close the listener socket since it is no longer needed
closesocket(hListenSocket);
if (hDataSocket == INVALID_SOCKET)
{
int iWinsockErr = WSAGetLastError();
wsprintf(gszCommandBuffer,
"Error %d while accepting a data socket connection!!",
iWinsockErr);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, lpszFunctionName,
MB_OK|MB_ICONSTOP);
return(INVALID_SOCKET);
}
else
{
// Make the data channel non-blocking before returning
IsReadyToRead(hDataSocket);
return(hDataSocket);
}
}
extern "C" BOOL _export FAR PASCAL LoadWinsock(VOID)
{
WSADATA wsaData; // Winsock implementation details
if (WSAStartup(WINSOCK_VERSION, &wsaData))
{
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, "Could not load Windows Sockets DLL.",
PROG_NAME, MB_OK|MB_ICONSTOP);
return(FALSE);
}
else
return(TRUE);
}
extern "C" BOOL _export FAR PASCAL CloseWinsock(VOID)
{
if (WSACleanup())
{
int iWinsockErr = WSAGetLastError();
wsprintf(gszCommandBuffer,
"WSACleanup() caused error# %d",
iWinsockErr);
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, gszCommandBuffer, PROG_NAME,
MB_OK|MB_ICONSTOP);
return(FALSE);
}
else
return(TRUE);
}
int _export FAR PASCAL LibMain(HANDLE hInstance, WORD wDataSeg,
WORD wHeapSize, LPSTR lpszCmdParam)
{
if (wHeapSize > 0) // Heap size defined in the DEF file.
UnlockData(0); // Unlocks the current data segment.
return(1);
}
int _export FAR PASCAL WEP(int nParam)
{
return(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -