使用Winsock2库实现、创建收发文件的服务器/客服端,实现如下功能:(35分) (1)客户端接受用户输入的传输文件名。 (2)客户端请求服务器端在指定文件夹下传输该文件名所指的文件。 如果指定文件存在,服务器端就将其发送给客户端;反之,则断开连接。
时间: 2024-03-01 18:53:17 浏览: 131
好的,这是一个关于网络编程的问题。我会尽力回答。
首先,使用Winsock2库可以轻松地实现客户端和服务器端之间的通信。下面是一个简单的代码示例,包括客户端和服务器端的代码。
服务器端代码:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#define PORT 12345
#define MAX_BUFF_SIZE 1024
int main()
{
WSADATA wsaData;
SOCKET serverSocket, clientSocket;
SOCKADDR_IN serverAddress, clientAddress;
char buffer[MAX_BUFF_SIZE];
int bufferLength = 0;
int clientAddressLength = sizeof(clientAddress);
// 初始化Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup failed!\n");
return 1;
}
// 创建服务器端socket
if ((serverSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
printf("Create server socket failed!\n");
WSACleanup();
return 1;
}
// 设置服务器端地址
ZeroMemory(&serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(PORT);
// 绑定服务器端socket和地址
if (bind(serverSocket, (SOCKADDR *)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
{
printf("Bind server socket failed!\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
// 监听客户端连接请求
if (listen(serverSocket, SOMAXCONN) == SOCKET_ERROR)
{
printf("Listen server socket failed!\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Server started!\n");
while (1)
{
// 等待客户端连接
clientSocket = accept(serverSocket, (SOCKADDR *)&clientAddress, &clientAddressLength);
if (clientSocket == INVALID_SOCKET)
{
printf("Accept client socket failed!\n");
closesocket(serverSocket);
WSACleanup();
return 1;
}
printf("Client connected!\n");
// 接收客户端发送的数据
bufferLength = recv(clientSocket, buffer, MAX_BUFF_SIZE, 0);
if (bufferLength == SOCKET_ERROR)
{
printf("Receive data failed!\n");
closesocket(clientSocket);
continue;
}
buffer[bufferLength] = '\0';
printf("Received file name: %s\n", buffer);
// 打开指定文件
FILE *file = fopen(buffer, "rb");
if (file == NULL)
{
printf("File not found!\n");
closesocket(clientSocket);
continue;
}
// 发送文件内容
while ((bufferLength = fread(buffer, sizeof(char), MAX_BUFF_SIZE, file)) > 0)
{
send(clientSocket, buffer, bufferLength, 0);
}
fclose(file);
closesocket(clientSocket);
printf("File sent!\n");
}
// 关闭socket和Winsock
closesocket(serverSocket);
WSACleanup();
return 0;
}
```
客户端代码:
```c++
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#define SERVER_ADDRESS "127.0.0.1"
#define PORT 12345
#define MAX_BUFF_SIZE 1024
int main()
{
WSADATA wsaData;
SOCKET clientSocket;
SOCKADDR_IN serverAddress;
char buffer[MAX_BUFF_SIZE];
int bufferLength = 0;
// 初始化Winsock
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
printf("WSAStartup failed!\n");
return 1;
}
// 创建客户端socket
if ((clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
{
printf("Create client socket failed!\n");
WSACleanup();
return 1;
}
// 设置服务器端地址
ZeroMemory(&serverAddress, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = inet_addr(SERVER_ADDRESS);
serverAddress.sin_port = htons(PORT);
// 连接服务器端
if (connect(clientSocket, (SOCKADDR *)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR)
{
printf("Connect to server failed!\n");
closesocket(clientSocket);
WSACleanup();
return 1;
}
printf("Connected to server!\n");
// 输入传输文件名
printf("Please enter the file name: ");
scanf("%s", buffer);
// 发送传输文件名
bufferLength = strlen(buffer);
send(clientSocket, buffer, bufferLength, 0);
// 接收服务器端发送的数据
FILE *file = fopen(buffer, "wb");
if (file == NULL)
{
printf("Create file failed!\n");
closesocket(clientSocket);
WSACleanup();
return 1;
}
while ((bufferLength = recv(clientSocket, buffer, MAX_BUFF_SIZE, 0)) > 0)
{
fwrite(buffer, sizeof(char), bufferLength, file);
}
fclose(file);
closesocket(clientSocket);
printf("File received!\n");
// 关闭Winsock
WSACleanup();
return 0;
}
```
以上代码中,服务器端会等待客户端连接,并接收客户端发送的文件名。如果文件存在,服务器端会将文件内容发送给客户端;如果文件不存在,则会断开连接。客户端会先输入要传输的文件名,然后连接服务器端并发送文件名。客户端会等待服务器端发送文件内容,并将其保存到本地。
当然,以上代码只是一个简单的示例,实际应用中还需要考虑数据的安全性、传输的效率等问题。
阅读全文