Windows下用c语言实现ftp的客户端和服务端的代码
时间: 2023-06-25 20:03:35 浏览: 199
ftp客户端的C语言实现
4星 · 用户满意度95%
以下是一个简单的基于C语言实现的FTP客户端和服务端的代码示例,运行在Windows系统上。
FTP客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 1024
#define PORT 21
int main(int argc, char* argv[]) {
if (argc != 3) {
printf("Usage: %s <IP> <filename>\n", argv[0]);
exit(1);
}
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed with error code %d\n", WSAGetLastError());
exit(1);
}
// 创建套接字
SOCKET sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET) {
printf("socket failed with error code %d\n", WSAGetLastError());
exit(1);
}
// 填充服务端地址
SOCKADDR_IN serverAddr;
memset(&serverAddr, 0, sizeof(serverAddr));
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
serverAddr.sin_port = htons(PORT);
// 连接服务端
if (connect(sock, (SOCKADDR*)&serverAddr, sizeof(serverAddr)) == SOCKET_ERROR) {
printf("connect failed with error code %d\n", WSAGetLastError());
closesocket(sock);
WSACleanup();
exit(1);
}
// 接收服务端欢迎信息
char buf[BUF_SIZE];
memset(buf, 0, BUF_SIZE);
recv(sock, buf, BUF_SIZE, 0);
printf("%s", buf);
// 发送用户名和密码
char user[] = "USER anonymous\r\n";
send(sock, user, strlen(user), 0);
memset(buf, 0, BUF_SIZE);
recv(sock, buf, BUF_SIZE, 0);
printf("%s", buf);
char pass[] = "PASS \r\n";
send(sock, pass, strlen(pass), 0);
memset(buf, 0, BUF_SIZE);
recv(sock, buf, BUF_SIZE, 0);
printf("%s", buf);
// 发送请求下载的文件名
char retr[BUF_SIZE];
sprintf(retr, "RETR %s\r\n", argv[2]);
send(sock, retr, strlen(retr), 0);
memset(buf, 0, BUF_SIZE);
recv(sock, buf, BUF_SIZE, 0);
printf("%s", buf);
// 接收文件内容
FILE* file = fopen(argv[2], "wb");
int len;
while ((len = recv(sock, buf, BUF_SIZE, 0)) > 0) {
fwrite(buf, sizeof(char), len, file);
}
fclose(file);
// 关闭套接字
closesocket(sock);
WSACleanup();
return 0;
}
```
FTP服务端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
#define BUF_SIZE 1024
#define PORT 21
int main() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
printf("WSAStartup failed with error code %d\n", WSAGetLastError());
exit(1);
}
// 创建套接字
SOCKET listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (listenSock == INVALID_SOCKET) {
printf("socket failed with error code %d\n", WSAGetLastError());
exit(1);
}
// 填充本地地址
SOCKADDR_IN localAddr;
memset(&localAddr, 0, sizeof(localAddr));
localAddr.sin_family = AF_INET;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(PORT);
// 绑定套接字到本地地址
if (bind(listenSock, (SOCKADDR*)&localAddr, sizeof(localAddr)) == SOCKET_ERROR) {
printf("bind failed with error code %d\n", WSAGetLastError());
closesocket(listenSock);
WSACleanup();
exit(1);
}
// 开始监听
if (listen(listenSock, SOMAXCONN) == SOCKET_ERROR) {
printf("listen failed with error code %d\n", WSAGetLastError());
closesocket(listenSock);
WSACleanup();
exit(1);
}
printf("FTP server is listening on port %d...\n", PORT);
// 接收连接请求并处理
SOCKET connSock;
SOCKADDR_IN clientAddr;
int addrLen = sizeof(clientAddr);
while (1) {
connSock = accept(listenSock, (SOCKADDR*)&clientAddr, &addrLen);
if (connSock == INVALID_SOCKET) {
printf("accept failed with error code %d\n", WSAGetLastError());
closesocket(listenSock);
WSACleanup();
exit(1);
}
printf("FTP client %s:%d connected\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
// 发送欢迎信息
char welcome[] = "220 Welcome to FTP server\r\n";
send(connSock, welcome, strlen(welcome), 0);
// 接收用户名和密码
char buf[BUF_SIZE];
memset(buf, 0, BUF_SIZE);
recv(connSock, buf, BUF_SIZE, 0);
printf("%s", buf);
memset(buf, 0, BUF_SIZE);
recv(connSock, buf, BUF_SIZE, 0);
printf("%s", buf);
// 发送文件内容
FILE* file = fopen("test.txt", "rb");
int len;
while ((len = fread(buf, sizeof(char), BUF_SIZE, file)) > 0) {
send(connSock, buf, len, 0);
}
fclose(file);
// 关闭连接套接字
closesocket(connSock);
printf("FTP client %s:%d disconnected\n", inet_ntoa(clientAddr.sin_addr), ntohs(clientAddr.sin_port));
}
// 关闭监听套接字并清理Winsock环境
closesocket(listenSock);
WSACleanup();
return 0;
}
```
阅读全文