【软件设计师备考 专题 】因特网基础知识以及应用(一)https://developer.aliyun.com/article/1467661
4.3 文件传输协议(FTP)
文件传输协议(FTP)用于在因特网上传输文件。FTP客户端可以连接到FTP服务器,上传文件到服务器或从服务器下载文件。以下是一个C++示例代码,演示如何使用Winsock库实现FTP文件上传:
#include <iostream> #include <fstream> #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") int main() { // 初始化Winsock库 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { std::cerr << "Failed to initialize Winsock" << std::endl; return 1; } // 创建套接字 SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0); if (clientSocket == INVALID_SOCKET) { std::cerr << "Failed to create socket" << std::endl; WSACleanup(); return 1; } // 连接到FTP服务器 sockaddr_in serverAddress; serverAddress.sin_family = AF_INET; serverAddress.sin_port = htons(21); serverAddress.sin_addr.s_addr = inet_addr("ftp.example.com"); if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) { std::cerr << "Failed to connect to FTP server" << std::endl; closesocket(clientSocket); WSACleanup(); return 1; } // 登录FTP服务器 std::string username = "ftpuser"; std::string password = "ftppassword"; std::string loginRequest = "USER " + username + "\r\n"; loginRequest += "PASS " + password + "\r\n"; if (send(clientSocket, loginRequest.c_str(), loginRequest.length(), 0) == SOCKET_ERROR) { std::cerr << "Failed to send login request" << std::endl; closesocket(clientSocket); WSACleanup(); return 1; } // 上传文件 std::string filename = "example.txt"; std::ifstream file(filename, std::ios::binary); if (!file) { std::cerr << "Failed to open file" << std::endl; closesocket(clientSocket); WSACleanup(); return 1; } std::string uploadRequest = "STOR " + filename + "\r\n"; if (send(clientSocket, uploadRequest.c_str(), uploadRequest.length(), 0) == SOCKET_ERROR) { std::cerr << "Failed to send upload request" << std::endl; file.close(); closesocket(clientSocket); WSACleanup(); return 1; } char buffer[4096]; while (file.read(buffer, sizeof(buffer))) { if (send(clientSocket, buffer, file.gcount(), 0) == SOCKET_ERROR) { std::cerr << "Failed to upload file" << std::endl; file.close(); closesocket(clientSocket); WSACleanup(); return 1; } } // 关闭套接字并清理Winsock库 file.close(); closesocket(clientSocket); WSACleanup(); std::cout << "File uploaded successfully" << std::endl; return 0; }
4.4 远程登录协议(Telnet)
远程登录协议(Telnet)允许用户通过网络远程登录到远程主机,并在远程主机上执行命令。以下是一个C++示例代码,演示如何使用Winsock库实现Telnet客户端:
#include <iostream> #include <string> #include <winsock2.h> #pragma comment(lib, "ws2_32.lib") int main() { // 初始化Winsock库 WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { std::cerr << "Failed to initialize Winsock" << std::endl; return 1; } // 创建套接字 SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0); if (clientSocket == INVALID_SOCKET) { std::cerr << "Failed to create socket" << std::endl; WSACleanup(); return 1; } // 连接到Telnet服务器 sockaddr_in serverAddress; serverAddress.sin_family = AF_INET; serverAddress.sin_port = htons(23); serverAddress.sin_addr.s_addr = inet_addr("telnet.example.com"); if (connect(clientSocket, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) { std::cerr << "Failed to connect to Telnet server" << std::endl; closesocket(clientSocket); WSACleanup(); return 1; } // 发送登录信息 std::string username = "telnetuser"; std::string password = "telnetpassword"; std::string loginRequest = username + "\r\n" + password + "\r\n"; if (send(clientSocket, loginRequest.c_str(), loginRequest.length(), 0) == SOCKET_ERROR) { std::cerr << "Failed to send login request" << std::endl; closesocket(clientSocket); WSACleanup(); return 1; } // 接收并显示服务器响应 char buffer[4096]; std::string serverResponse; while (true) { int bytesRead = recv(clientSocket, buffer, sizeof(buffer) - 1, 0); if (bytesRead <= 0) { break; } buffer[bytesRead] = '\0'; serverResponse += buffer; } std::cout << "Server response:" << std::endl; std::cout << serverResponse << std::endl; // 关闭套接字并清理Winsock库 closesocket(clientSocket); WSACleanup(); return 0; }
【软件设计师备考 专题 】因特网基础知识以及应用(三)https://developer.aliyun.com/article/1467663