if (ReadFTPServerReply(hControlSocket) >= 400)
return(INVALID_SOCKET);
else
return(hControlSocket);
}
int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
WSADATA wsaData; // Winsock implementation details
SOCKET hControlChannel; // Socket handle for the control channel
UINT nReplyCode; // FTP reply code
lpszFunctionName = "WinMain";
if (WSAStartup(WINSOCK_VERSION, &wsaData))
{
MessageBeep(MB_ICONHAND);
MessageBox(NULL, "Could not load Windows Sockets DLL.",
lpszFunctionName, MB_OK ¦MB_ICONSTOP);
return(NULL);
}
hControlChannel = ConnectFTPControlSocket((LPSTR)HOST_NAME);
// Note that from a DLL, here we would return
// our control channel handle (if valid) for
// use by other program modules.
if (hControlChannel != INVALID_SOCKET)
{
// If we have a control channel, then login.
nReplyCode = AnonymousFTPLogIn(hControlChannel);
if (nReplyCode == 230) // User logged in; we can proceed.
{
SendFTPCommand(hControlChannel, "QUIT\r\n");
closesocket(hControlChannel);
}
}
WSACleanup();
MessageBeep(MB_ICONEXCLAMATION);
MessageBox(NULL, "THE END!!", PROG_NAME, MB_OK ¦MB_ICONEXCLAMATION);
return(NULL);
}
do
{
nReplyCode = SendFTPCommand(hControlSocket,
(LPSTR)LoginCommand[iMsg++]);
}
while (LoginCommand[iMsg] && nReplyCode < 400);
return(nReplyCode);
}
SOCKET 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; // Control socket handle
lpszFunctionName = "ConnectFTPControlSocket";
if (!(lpHostEnt = gethostbyname(lpszHost)))
{
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer,
"Error #%d while resolving address for %s",
iWinsockErr, lpszHost);
MessageBeep(MB_ICONHAND);
MessageBox(NULL, szCommandBuffer, lpszFunctionName,
MB_OK ¦MB_ICONSTOP);
return(INVALID_SOCKET);
}
if ((hControlSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))
== INVALID_SOCKET)
{
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer,
"Error #%d occurred while creating socket!!",
iWinsockErr);
MessageBeep(MB_ICONHAND);
MessageBox(NULL, szCommandBuffer, lpszFunctionName,
MB_OK ¦MB_ICONSTOP);
// Send the FTP command
if ((send(hControlChannel, (LPSTR)szCommandBuffer,
lstrlen(szCommandBuffer), NO_FLAGS)) == SOCKET_ERROR)
{
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer, "Error %d from the send() function!!",
#define HOST_NAME "NIC.DDN.MIL" // FTP server host
#define PASSWORD "PASS guest\r\n" // Password for FTP server host
#define WINSOCK_VERSION 0x0101 // Program requires Winsock version 1.1
#define DEFAULT_PROTOCOL 0 // No protocol specified, use default
#define NO_FLAGS 0 // No special flags specified
char szCommandBuffer[100]; // Buffer for FTP commands
LPSTR lpszFunctionName; // Pointer for function names
UINT GetReplyCode(LPSTR lpszServerReply)
{
UINT nCode; // Reply code as a number
char c; // Temporary storage
// lpszFunctionName = "GetReplyCode";
c = *(lpszServerReply+3); // Save the character
*(lpszServerReply+3) = '\0'; // Terminate the code
nCode = atoi((const char *)lpszServerReply); // Convert code to number
*(lpszServerReply+3) = c; // Restore the character
return(nCode); // Return the reply code
}
UINT ReadFTPServerReply(SOCKET hControlChannel)
{
char sReceiveBuffer[1024]; // Data-storage buffer for FTP server reply
int iLength; // Length of data received from FTP server
lpszFunctionName = "ReadFTPServerReply";
if ((iLength = recv(hControlChannel, (LPSTR)sReceiveBuffer,
sizeof(sReceiveBuffer), NO_FLAGS)) == SOCKET_ERROR)
{
int iWinsockErr = WSAGetLastError();
wsprintf(szCommandBuffer, "Error %d from the recv() function!!",
iWinsockErr);
MessageBeep(MB_ICONHAND);
MessageBox(NULL, szCommandBuffer, lpszFunctionName,
MB_OK ¦MB_ICONSTOP);
// Return 999 to indicate an error has occurred
return(999);
}