📄 qftp2.cpp
字号:
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);
}
if (ReadFTPServerReply(hControlSocket) >= 400)
return(INVALID_SOCKET);
else
return(hControlSocket);
}
SOCKET 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);
}
SOCKET 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));
}
SOCKET 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
return(hDataSocket);
}
VOID DemonstrateCommand(SOCKET hControlChannel)
{
SOCKET hDataChannel; // Handle for socket to receive server data
SOCKET hListenSocket; // Handle for the listener socket
UINT nReplyCode; // FTP server reply code
lpszFunctionName = "DemonstrateCommand";
if ((hListenSocket = CreateListenSocket(hControlChannel))
== INVALID_SOCKET)
return;
// Hard code the NLST command for testing.
if (nReplyCode = SendFTPCommand(hControlChannel,
"NLST\r\n") >= 400)
return;
// Accept the data connection from the server. Note that
// AcceptDataConnection() closes the hListenSocket.
if ((hDataChannel = AcceptDataConnection(hListenSocket))
== INVALID_SOCKET)
return;
ReadDataChannel(hDataChannel, "NLST.TXT");
closesocket(hDataChannel);
return;
}
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 server reply code
lpszFunctionName = "WinMain";
if (WSAStartup(WINSOCK_VERSION, &wsaData))
{
MessageBeep(MB_ICONSTOP);
MessageBox(NULL, "Could not load Windows Sockets DLL.",
lpszFunctionName, MB_OK|MB_ICONSTOP);
return(NULL);
}
hControlChannel = ConnectFTPControlSocket((LPSTR)HOST_NAME);
if (hControlChannel != INVALID_SOCKET)
{
// If the control socket is valid, perform an anonymous FTP login.
nReplyCode = AnonymousFTPLogIn(hControlChannel);
if (nReplyCode == 230) // User logged in; okay to proceed.
{
DemonstrateCommand(hControlChannel);
SendFTPCommand(hControlChannel, "QUIT\r\n");
closesocket(hControlChannel);
}
}
WSACleanup();
MessageBeep(MB_ICONEXCLAMATION);
MessageBox(NULL, "THE END!!", PROG_NAME, MB_OK|MB_ICONEXCLAMATION);
return(NULL);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -