IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++的文件传输 -> 正文阅读

[C++知识库]C++的文件传输

C++的客户端有两种,一种是界面的MFC的版本,另外一种是自己参照mfc修改的命令框的形式的,基本的功能是一样的。

mfc的版本主要是图形框的选择一般是系统自己生成的函数如下:

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
?? ?CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
?? ?enum { IDD = IDD_ABOUTBOX };
#endif

?? ?protected:
?? ?virtual void DoDataExchange(CDataExchange* pDX); ? ?// DDX/DDV 支持

// 实现
protected:
?? ?DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
?? ?CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CFileTranslaterClientDlg 对话框

CFileTranslaterClientDlg::CFileTranslaterClientDlg(CWnd* pParent /*=nullptr*/)
?? ?: CDialogEx(IDD_FILETRANSLATERCLIENT_DIALOG, pParent)
{
?? ?m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileTranslaterClientDlg::DoDataExchange(CDataExchange* pDX)
{
?? ?CDialogEx::DoDataExchange(pDX);
?? ?// ?DDX_Control(pDX, IDC_IPADDRESS1, serverIP);
?? ?DDX_Control(pDX, IDC_LIST1, messageList);
?? ?DDX_Control(pDX, IDC_IPADDRESS1, severIP);
?? ?DDX_Control(pDX, IDC_EDIT1, UpLoadName);
?? ?DDX_Control(pDX, IDC_EDIT2, download_path);
}

BEGIN_MESSAGE_MAP(CFileTranslaterClientDlg, CDialogEx)
?? ?ON_WM_SYSCOMMAND()
?? ?ON_WM_PAINT()
?? ?ON_WM_QUERYDRAGICON()
?? ?ON_BN_CLICKED(IDOK, &CFileTranslaterClientDlg::OnBnClickedOk)
?? ?ON_BN_CLICKED(IDC_BUTTON1, &CFileTranslaterClientDlg::OnBnClickedButton1)
?? ?ON_BN_CLICKED(IDC_BUTTON2, &CFileTranslaterClientDlg::OnBnClickedButton2)
?? ?ON_BN_CLICKED(IDC_BUTTON3, &CFileTranslaterClientDlg::OnBnClickedButton3)
?? ?ON_BN_CLICKED(IDC_BUTTON5, &CFileTranslaterClientDlg::OnBnClickedButton5)
?? ?ON_EN_CHANGE(IDC_EDIT2, &CFileTranslaterClientDlg::OnEnChangeEdit2)
?? ?ON_EN_CHANGE(IDC_EDIT1, &CFileTranslaterClientDlg::OnEnChangeEdit1)
?? ?ON_BN_CLICKED(IDC_BUTTON4, &CFileTranslaterClientDlg::OnBnClickedButton4)
END_MESSAGE_MAP()


// CFileTranslaterClientDlg 消息处理程序

BOOL CFileTranslaterClientDlg::OnInitDialog()
{
?? ?CDialogEx::OnInitDialog();

?? ?// 将“关于...”菜单项添加到系统菜单中。

?? ?// IDM_ABOUTBOX 必须在系统命令范围内。
?? ?ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
?? ?ASSERT(IDM_ABOUTBOX < 0xF000);

?? ?CMenu* pSysMenu = GetSystemMenu(FALSE);
?? ?if (pSysMenu != nullptr)
?? ?{
?? ??? ?BOOL bNameValid;
?? ??? ?CString strAboutMenu;
?? ??? ?bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
?? ??? ?ASSERT(bNameValid);
?? ??? ?if (!strAboutMenu.IsEmpty())
?? ??? ?{
?? ??? ??? ?pSysMenu->AppendMenu(MF_SEPARATOR);
?? ??? ??? ?pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
?? ??? ?}
?? ?}

?? ?// 设置此对话框的图标。 ?当应用程序主窗口不是对话框时,框架将自动
?? ?// ?执行此操作
?? ?SetIcon(m_hIcon, TRUE);?? ??? ??? ?// 设置大图标
?? ?SetIcon(m_hIcon, FALSE);?? ??? ?// 设置小图标

?? ?// TODO: 在此添加额外的初始化代码

?? ?return TRUE; ?// 除非将焦点设置到控件,否则返回 TRUE
}

void CFileTranslaterClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
?? ?if ((nID & 0xFFF0) == IDM_ABOUTBOX)
?? ?{
?? ??? ?CAboutDlg dlgAbout;
?? ??? ?dlgAbout.DoModal();
?? ?}
?? ?else
?? ?{
?? ??? ?CDialogEx::OnSysCommand(nID, lParam);
?? ?}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
// ?来绘制该图标。 ?对于使用文档/视图模型的 MFC 应用程序,
// ?这将由框架自动完成。

void CFileTranslaterClientDlg::OnPaint()
{
?? ?if (IsIconic())
?? ?{
?? ??? ?CPaintDC dc(this); // 用于绘制的设备上下文

?? ??? ?SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

?? ??? ?// 使图标在工作区矩形中居中
?? ??? ?int cxIcon = GetSystemMetrics(SM_CXICON);
?? ??? ?int cyIcon = GetSystemMetrics(SM_CYICON);
?? ??? ?CRect rect;
?? ??? ?GetClientRect(&rect);
?? ??? ?int x = (rect.Width() - cxIcon + 1) / 2;
?? ??? ?int y = (rect.Height() - cyIcon + 1) / 2;

?? ??? ?// 绘制图标
?? ??? ?dc.DrawIcon(x, y, m_hIcon);
?? ?}
?? ?else
?? ?{
?? ??? ?CDialogEx::OnPaint();
?? ?}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CFileTranslaterClientDlg::OnQueryDragIcon()
{
?? ?return static_cast<HCURSOR>(m_hIcon);
}

void CFileTranslaterClientDlg::OnBnClickedOk()
{
?? ?CDialogEx::OnOK();
}

然后就是客户端对服务端的socket的服务的连接

void CFileTranslaterClientDlg::OnBnClickedButton1()
{
?? ?//初始化 socket dll
?? ?WSADATA wsaData;
?? ?WORD socketVersion = MAKEWORD(2, 2);
?? ?assert(WSAStartup(socketVersion, &wsaData) == 0);

?? ?//创建socket
?? ?client_socket = socket(AF_INET, SOCK_STREAM, 0);
?? ?assert(SOCKET_ERROR != client_socket);

?? ?//指定服务端的地址
?? ?sockaddr_in server_addr;
?? ?server_addr.sin_family = AF_INET;
?? ?CString SERVER_IP;
?? ?
?? ?severIP.GetWindowTextA(SERVER_IP);

?? ?server_addr.sin_addr.S_un.S_addr = inet_addr(SERVER_IP);
?? ?server_addr.sin_port = htons(PORT);
?? ?if (SOCKET_ERROR != connect(client_socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)))
?? ?{
?? ??? ?messageList.AddString("连接成功");
?? ?}
?? ?else
?? ?{
?? ??? ?messageList.AddString("连接失败");
?? ?}

?? ?//BOOL result = connectSever("127.0.0.1", 1982);
?? ?
}

之后就是对文件路径的选择此处属于mfc一些专属函数:

void CFileTranslaterClientDlg::OnBnClickedButton2()
{
?? ?CFileDialog opendlg(true, NULL, NULL, 0, "All Files(*.*)|*.*||");
?? ?if (opendlg.DoModal() == IDOK) { //显示对话框并允许用户进行选择。
?? ??? ?upload_file_location = opendlg.GetPathName(); //文件路径
?? ??? ?UpLoadName.SetWindowTextA(upload_file_location); //在编辑框显示文件路径
?? ??? ?messageList.AddString("打开文件成功");
?? ?}
}

文件的上传的步骤主要是,打开文件,用二进制读取文件的内容,及用函数来获取文件的路径进行验证

void CFileTranslaterClientDlg::OnBnClickedButton3()
{
?? ?//标识上传 send(client_socket,"1", sizeof("1"), 0);
?? ?char FileLocation[FILE_NAME_MAX_SIZE];
?? ?strcpy(FileLocation, upload_file_location);
?? ?int file_Location_size = strlen(FileLocation);
?? ?while (FileLocation[file_Location_size] != '\\'&&file_Location_size > 0) { file_Location_size--; }
?? ?char real_name[FILE_NAME_MAX_SIZE];
?? ?memset(real_name, 0, sizeof(real_name));
?? ?strcpy(real_name, FileLocation + file_Location_size + 1); ? ? ? ? ? ? ? ? ? ? ?//在这里存放真实名字
?? ?cout << "发送到服务器的真实名字:\t" << real_name << endl;?? ??? ??? ??? ??? ??? ?//这个语句是用来测试的

?? ?messageList.AddString(CString("要上传的文件是:")+CString(real_name)); ? ? ? ? ?//把名字显示在右边

?? ?char buffer[BUFFER_SIZE];
?? ?memset(buffer, 0, BUFFER_SIZE);
?? ?
?? ?if (send(client_socket, real_name, sizeof(real_name), 0) < 0) ?//标记
?? ?{
?? ??? ?messageList.AddString("在发送文件名字时出错" + WSAGetLastError());
?? ??? ?MessageBox("发送文件名时失败");
?? ?}
?? ?CString test;
?? ?FILE *fp = fopen(FileLocation, "rb"); ?//以只读,二进制的方式打开一个文件


?? ?if (NULL == fp)
?? ?{
?? ??? ?messageList.AddString("这个文件不存在");
?? ?}
?? ?else
?? ?{
?? ??? ?memset(buffer, 0, BUFFER_SIZE);
?? ??? ?int length = 0;

?? ??? ?while ((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
?? ??? ?{
?? ??? ??? ?if (send(client_socket, buffer, length, 0) < 0)
?? ??? ??? ?{
?? ??? ??? ??? ?messageList.AddString("文件发送失败"+WSAGetLastError());
?? ??? ??? ??? ?MessageBox("发送文件时失败");
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?memset(buffer, 0, BUFFER_SIZE);
?? ??? ?}

?? ??? ?fclose(fp);
?? ?}

?? ?messageList.AddString("文件发送成功");
?? ?std::cout << "文件: " << real_name << std::endl << "发送成功 !" << std::endl;
?? ?closesocket(client_socket);
?? ?//释放 winsock 库
?? ?WSACleanup();

}

文件路径的选择对话框:

void CFileTranslaterClientDlg::OnBnClickedButton5()
{
?? ?//打开路径
?? ?BROWSEINFO bi;
?? ?char Buffer[MAX_PATH] = { 0 };
?? ?//初始化入口参数bi开始
?? ?bi.hwndOwner = NULL;
?? ?bi.pidlRoot = NULL;
?? ?bi.pszDisplayName = Buffer;//此参数如为NULL则不能显示对话框
?? ?bi.lpszTitle = "打开路径";
?? ?bi.ulFlags = BIF_EDITBOX;//包括文件
?? ?bi.lpfn = NULL;
?? ?bi.iImage = IDR_MAINFRAME;

?? ?LPITEMIDLIST pIDList = SHBrowseForFolder(&bi);//调用显示选择对话框
?? ?if (pIDList)
?? ?{
?? ??? ?SHGetPathFromIDList(pIDList, Buffer);//取得文件夹路径到Buffer里
?? ?}
?? ?store_file_location = CString(Buffer);
?? ?messageList.AddString("选择存放路径成功:"+store_file_location);
}

void CFileTranslaterClientDlg::OnEnChangeEdit2()
{
?? ?download_path.GetWindowTextA(download_file_location);
}


void CFileTranslaterClientDlg::OnEnChangeEdit1()
{
}

最后就是服务器的文件的接受

void CFileTranslaterClientDlg::OnBnClickedButton4()
{
?? ?//send(client_socket, "2", sizeof("2"), 0);标识下载
?? ?//这是从服务器上下载东西的代码
?? ?//输入文件名
?? ?char file_name[FILE_NAME_MAX_SIZE + 1];
?? ?memset(file_name, 0, FILE_NAME_MAX_SIZE + 1);
?? ?std::cout << "输入你想要在服务器上获取的名字:" << std::endl; ?//测试
?? ?strcpy(file_name, download_file_location);
?? ?messageList.AddString(CString("要下载的路径:") + download_file_location);
?? ?char buffer[BUFFER_SIZE];
?? ?memset(buffer, 0, BUFFER_SIZE);
?? ?strncpy(buffer, file_name, strlen(file_name) > BUFFER_SIZE ? BUFFER_SIZE : strlen(file_name)); ?//取较短的

?? ?//向服务器发送文件名
?? ?assert(send(client_socket, buffer, BUFFER_SIZE, 0) >= 0);//>=0

?? ?//打开文件,准备写入
?? ?//这里可以用filename代替
?? ?char receive[FILE_NAME_MAX_SIZE];
?? ?cout << "你想放在哪里?" << endl;
?? ?strcpy(receive, store_file_location);
?? ?int file_name_size = strlen(file_name);

?? ?while (file_name[file_name_size] != '\\'&&file_name_size > 0) { file_name_size--; }
?? ?char real_name[FILE_NAME_MAX_SIZE];
?? ?memset(real_name, 0, sizeof(real_name));
?? ?strcpy(real_name, file_name + file_name_size + 1);

?? ?strcat(receive, "\\");
?? ?cout << "接收到的真实名字" << real_name << endl;
?? ?strcat(receive, real_name);
?? ?FILE *fp = fopen(receive, "wb"); ?//以只写,二进制的方式打开一个文件wb
?? ?assert(NULL != fp);
?? ?memset(buffer, 0, BUFFER_SIZE);//初始化
?? ?int length = 0;
?? ?while ((length = recv(client_socket, buffer, BUFFER_SIZE, 0)) >0)//>0
?? ?{
?? ??? ?assert(fwrite(buffer, sizeof(char), length, fp) >= (size_t)length);
?? ??? ?memset(buffer, 0, BUFFER_SIZE);
?? ?}
?? ?messageList.AddString("下载成功");
?? ?std::cout << "接受文件 : " << file_name << endl << " 接收成功 !" << std::endl;
?? ?fclose(fp);
?? ?closesocket(client_socket);
}

下面还有一个自己的简易版本就是命令框的

//#include "KeHu.h"
//
//BOOL CFileTranslaterClientDlg::OnInitDialog()
//{
//?? ?return 0;
//}
//?? ?
//void CFileTranslaterClientDlg::OnSysCommand(UINT nID, LPARAM lParam)
//{
//}
//
//void CFileTranslaterClientDlg::OnPaint()
//{
//}
//
//
//
//
//BOOL ?CFileTranslaterClientDlg::connectSever(string ?ip, int port)
//{
//?? ?//初始化 socket dll
//?? ?WSADATA wsaData;
//?? ?WORD socketVersion = MAKEWORD(2, 2);
//?? ?assert(WSAStartup(socketVersion, &wsaData) == 0);
//
//?? ?//创建socket
//?? ?client_socket = socket(AF_INET, SOCK_STREAM, 0);
//?? ?assert(SOCKET_ERROR != client_socket);
//
//?? ?//指定服务端的地址
//?? ?sockaddr_in server_addr;
//?? ?server_addr.sin_family = AF_INET;
//?? ?//CString SERVER_IP;
//
//?? ?//severIP.GetWindowTextA(SERVER_IP);
//
//?? ?server_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
//?? ?server_addr.sin_port = htons(port);
//?? ?if (SOCKET_ERROR != connect(client_socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)))
//?? ?{
//?? ??? ?messageList.AddString("连接成功");
//?? ?}
//?? ?else
//?? ?{
//?? ??? ?messageList.AddString("连接失败");
//?? ?}
//}
//BOOL ?CFileTranslaterClientDlg::SendFile(string filePath)
//{
//?? ?//标识上传 send(client_socket,"1", sizeof("1"), 0);
//?? ?char FileLocation[FILE_NAME_MAX_SIZE];
//?? ?strcpy(FileLocation, upload_file_location);
//?? ?int file_Location_size = strlen(FileLocation);
//?? ?while (FileLocation[file_Location_size] != '\\'&&file_Location_size > 0) { file_Location_size--; }
//?? ?char real_name[FILE_NAME_MAX_SIZE];
//?? ?memset(real_name, 0, sizeof(real_name));
//?? ?strcpy(real_name, FileLocation + file_Location_size + 1); ? ? ? ? ? ? ? ? ? ? ?//在这里存放真实名字
//?? ?cout << "发送到服务器的真实名字:\t" << real_name << endl;?? ??? ??? ??? ??? ??? ?//这个语句是用来测试的
//
//?? ?messageList.AddString(CString("要上传的文件是:") + CString(real_name)); ? ? ? ? ?//把名字显示在右边
//
//?? ?char buffer[BUFFER_SIZE];
//?? ?memset(buffer, 0, BUFFER_SIZE);
//
//?? ?if (send(client_socket, real_name, sizeof(real_name), 0) < 0) ?//标记
//?? ?{
//?? ??? ?messageList.AddString("在发送文件名字时出错" + WSAGetLastError());
//?? ??? ?MessageBox("发送文件名时失败");
//?? ?}
//?? ?CString test;
//?? ?FILE *fp = fopen(FileLocation, "rb"); ?//以只读,二进制的方式打开一个文件
//
//
//?? ?if (NULL == fp)
//?? ?{
//?? ??? ?messageList.AddString("这个文件不存在");
//?? ?}
//?? ?else
//?? ?{
//?? ??? ?memset(buffer, 0, BUFFER_SIZE);
//?? ??? ?int length = 0;
//
//?? ??? ?while ((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
//?? ??? ?{
//?? ??? ??? ?if (send(client_socket, buffer, length, 0) < 0)
//?? ??? ??? ?{
//?? ??? ??? ??? ?messageList.AddString("文件发送失败" + WSAGetLastError());
//?? ??? ??? ??? ?MessageBox("发送文件时失败");
//?? ??? ??? ??? ?break;
//?? ??? ??? ?}
//?? ??? ??? ?memset(buffer, 0, BUFFER_SIZE);
//?? ??? ?}
//
//?? ??? ?fclose(fp);
//?? ?}
//
//?? ?messageList.AddString("文件发送成功");
//?? ?std::cout << "文件: " << real_name << std::endl << "发送成功 !" << std::endl;
//?? ?closesocket(client_socket);
//?? ?//释放 winsock 库
//?? ?WSACleanup();
//}
//class CFileTranslaterClientDlg : public CDialogEx
//{
//
//protected:
//?? ?HICON m_hIcon;
//
//?? ?// 生成的消息映射函数
//?? ?virtual BOOL OnInitDialog();
//?? ?afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
//?? ?afx_msg void OnPaint();
//?? ?BOOL connectSever(string ip, int port);
//?? ?BOOL SendFile(string filePath);
//?? ?afx_msg HCURSOR OnQueryDragIcon();
//?? ?DECLARE_MESSAGE_MAP()
//public:
//?? ?// 服务器IP地址
?? ?CIPAddressCtrl serverIP;
//?? ?afx_msg void OnBnClickedOk();
//?? ?afx_msg void OnBnClickedButton1();
//?? ?// 存放信息
//?? ?CListBox messageList;
//?? ?CIPAddressCtrl severIP;
//?? ?// 上传文件的名字
//?? ?CEdit UpLoadName;
//?? ?afx_msg void OnBnClickedButton2();
//?? ?afx_msg void OnBnClickedButton3();
//?? ?SOCKET client_socket = NULL;
//?? ?CString upload_file_location = NULL;
//?? ?CEdit download_path;
//?? ?CString download_file_location = NULL;
//?? ?CString store_file_location = NULL;
//?? ?afx_msg void OnBnClickedButton5();
//?? ?afx_msg void OnEnChangeEdit2();
//?? ?afx_msg void OnEnChangeEdit1();
//?? ?afx_msg void OnBnClickedButton4();
//
//?? ?//函数封装
//?? ?/*BOOL connectSever(string ?ip, int port);
//?? ?BOOL SendFile(string filePath);*/
//};
//
//
//


#define ?_WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <WinSock2.h>
#include <cassert>
#include <iostream>
#include ?<direct.h>
#include "process.h"
#include <string>
#include "KeHu.h"
using namespace std;


//#include <afx.h>

#define PORT 8087
#define SERVER_IP "127.0.0.1"
#define BUFFER_SIZE 1024
#define FILE_NAME_MAX_SIZE 512
#pragma comment(lib, "WS2_32")


#ifdef WIN32
#include <shlobj.h>
#endif

//SOCKET InitNewSocket()
//{
//?? ?//声明地址结构
//?? ?sockaddr_in server_addr;
//?? ?server_addr.sin_family = AF_INET;
//?? ?server_addr.sin_addr.S_un.S_addr = INADDR_ANY;
//?? ?server_addr.sin_port = htons(PORT);
//
//?? ?//初始化 socket dll
//?? ?WSADATA wsaData;
//?? ?WORD socketVersion = MAKEWORD(2, 2);
//?? ?assert(WSAStartup(socketVersion, &wsaData) == 0);
//
//?? ?//创建socket
//?? ?SOCKET m_socket = socket(AF_INET, SOCK_STREAM, 0);
//?? ?assert(SOCKET_ERROR != m_socket);
//
//?? ?//绑定
//?? ?assert(SOCKET_ERROR != bind(m_socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)));
//
//?? ?//监听
//?? ?assert(SOCKET_ERROR != listen(m_socket, 5));
//
//?? ?return m_socket;
//}

//连接服务器
//void ?CFileTranslaterClientDlg::connectSever(string ?ip, int port)
//{
//?? ?//初始化 socket dll
//?? ?WSADATA wsaData;
//?? ?WORD socketVersion = MAKEWORD(2, 2);
//?? ?assert(WSAStartup(socketVersion, &wsaData) == 0);
//
//?? ?//创建socket
//?? ?client_socket = socket(AF_INET, SOCK_STREAM, 0);
//?? ?assert(SOCKET_ERROR != client_socket);
//
//?? ?//指定服务端的地址
//?? ?sockaddr_in server_addr;
//?? ?server_addr.sin_family = AF_INET;
//?? ?//CString SERVER_IP;
//
//?? ?//severIP.GetWindowTextA(SERVER_IP);
//
//?? ?server_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
//?? ?server_addr.sin_port = htons(port);
//?? ?if (SOCKET_ERROR != connect(client_socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)))
//?? ?{
//?? ??? ?cout<<"连接成功";
//?? ?}
//?? ?else
//?? ?{
//?? ??? ?cout<<"连接失败";
//?? ?}
//}

void CFileTranslaterClientDlg::OnBnClickedButton1()
{
?? ?//初始化 socket dll
?? ?WSADATA wsaData;
?? ?WORD socketVersion = MAKEWORD(2, 2);
?? ?assert(WSAStartup(socketVersion, &wsaData) == 0);

?? ?//创建socket
?? ?client_socket = socket(AF_INET, SOCK_STREAM, 0);
?? ?assert(SOCKET_ERROR != client_socket);

?? ?//指定服务端的地址
?? ?sockaddr_in server_addr;
?? ?server_addr.sin_family = AF_INET;
?? ?//CString SERVER_IP;
?? ?//char SERVER_IP;

?? ?//severIP.GetWindowTextA(SERVER_IP);
?? ?char* ip;
?? ?ip = new char[100];
?? ?cout << "请输入IP地址" << endl;
?? ?cin >>ip;

?? ?//server_addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
?? ?server_addr.sin_addr.S_un.S_addr = inet_addr(ip);
?? ?server_addr.sin_port = htons(PORT);
?? ?if (SOCKET_ERROR != connect(client_socket, (LPSOCKADDR)&server_addr, sizeof(server_addr)))
?? ?{
?? ??? ?//messageList.AddString("连接成功");
?? ??? ?cout << "连接成功";
?? ?}
?? ?else
?? ?{
?? ??? ?//messageList.AddString("连接失败");
?? ??? ?cout << "连接失败";
?? ?}

?? ?//string result = OnBnClickedButton1("127.0.0.1", 1982);

}

//文件的选择及路径的获取
void ?CFileTranslaterClientDlg::OnBnClickedButton2()
{
?? ?//BROWSEINFO bi;
?? ?//bi.hwndOwner = NULL;
?? ?//bi.pidlRoot = CSIDL_DESKTOP;//文件夹的根目录,此处为桌面
?? ?//bi.pszDisplayName = NULL;
?? ?//bi.lpszTitle = NULL;//显示位于对话框左上部的提示信息
?? ?//bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;//有新建文件夹按钮
?? ?//bi.lpfn = NULL;
?? ?//bi.iImage = 0;
?? ?//LPITEMIDLIST pidl = SHBrowseForFolder(&bi);//调用选择对话框
?? ?//if (pidl == NULL)
?? ?//{
?? ?//?? ?std::cout << "没有选择目录" << std::endl;
?? ?//?? ?return;
?? ?//}
?? ?//TCHAR strFolder[MAX_PATH];
?? ?//SHGetPathFromIDList(pidl, strFolder);
?? ?//std::string sFolder = StringUtil::WStringToString(strFolder);

?? ?
?? ?


}


void CFileTranslaterClientDlg::OnBnClickedButton3()
{
?? ?char* upload_file_location;
?? ?upload_file_location = new char[100];
?? ?//标识上传 send(client_socket,"1", sizeof("1"), 0);
?? ?char FileLocation[FILE_NAME_MAX_SIZE];
?? ?

?? ?
?? ?cin >> upload_file_location;
?? ?//char* upload_file_location =&upload_file_location1;
?? ?strcpy(FileLocation,upload_file_location);
?? ?//strcpy(FileLocation, "D:\\1.txt");

?? ?int file_Location_size = strlen(FileLocation);
?? ?while (FileLocation[file_Location_size] != '\\'&&file_Location_size > 0) { file_Location_size--; }
?? ?char real_name[FILE_NAME_MAX_SIZE];
?? ?::memset(real_name, 0, sizeof(real_name));
?? ?strcpy(real_name, FileLocation + file_Location_size + 1); ? ? ? ? ? ? ? ? ? ? ?//在这里存放真实名字
?? ?cout << "发送到服务器的真实名字:\t" << real_name << endl;?? ??? ??? ??? ??? ??? ?//这个语句是用来测试的

?? ?//messageList.AddString(CString("要上传的文件是:") + CString(real_name)); ? ? ? ? ?//把名字显示在右边
?? ?cout << "要上传的文件是:" << real_name << endl;

?? ?char buffer[BUFFER_SIZE];
?? ?::memset(buffer, 0, BUFFER_SIZE);

?? ?if (send(client_socket, real_name, sizeof(real_name), 0) < 0) ?//标记
?? ?{
?? ??? ?//messageList.AddString("在发送文件名字时出错" + WSAGetLastError());

?? ??? ?//MessageBox("发送文件名时失败");
?? ??? ?cout << "发送文件时失败";
?? ?}
?? ?//CString test;
?? ?FILE *fp = fopen(FileLocation, "rb"); ?//以只读,二进制的方式打开一个文件


?? ?if (NULL == fp)
?? ?{
?? ??? ?//messageList.AddString("这个文件不存在");
?? ??? ?cout << "这个文件不存在";
?? ?}
?? ?else
?? ?{
?? ??? ?::memset(buffer, 0, BUFFER_SIZE);
?? ??? ?int length = 0;

?? ??? ?while ((length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0)
?? ??? ?{
?? ??? ??? ?if (send(client_socket, buffer, length, 0) < 0)
?? ??? ??? ?{
?? ??? ??? ??? ?//messageList.AddString("文件发送失败" + WSAGetLastError());
?? ??? ??? ??? ?//MessageBox("发送文件时失败");
?? ??? ??? ??? ?cout << "发送文件时失败";
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ??? ?::memset(buffer, 0, BUFFER_SIZE);
?? ??? ?}

?? ??? ?fclose(fp);
?? ?}

?? ?//messageList.AddString("文件发送成功");
?? ?cout << "文件发送成功";
?? ?std::cout << "文件: " << real_name << std::endl << "发送成功 !" << std::endl;
?? ?closesocket(client_socket);
?? ?//释放 winsock 库
?? ?WSACleanup();

}


void ?CFileTranslaterClientDlg::OnBnClickedButton4()
{
?? ?//send(client_socket, "2", sizeof("2"), 0);标识下载
?? ?//这是从服务器上下载东西的代码
?? ?//输入文件名
?? ?char* download_file_location;
?? ?download_file_location = new char[100];

?? ?char file_name[FILE_NAME_MAX_SIZE + 1];
?? ?memset(file_name, 0, FILE_NAME_MAX_SIZE + 1);
?? ?std::cout << "输入你想要在服务器上获取的名字:" << std::endl; ?//测试
?? ?cin >> download_file_location;
?? ?strcpy(file_name, download_file_location);
?? ?//strcpy(file_name,"D:\\1.txt");
?? ?//messageList.AddString(CString("要下载的路径:") + download_file_location);
?? ?cout << "要下载的路径:" << download_file_location << endl;

?? ?char buffer[BUFFER_SIZE];
?? ?memset(buffer, 0, BUFFER_SIZE);
?? ?strncpy(buffer, file_name, strlen(file_name) > BUFFER_SIZE ? BUFFER_SIZE : strlen(file_name)); ?//取较短的

?? ?//向服务器发送文件名
?? ?assert(send(client_socket, buffer, BUFFER_SIZE, 0) >= 0);//>=0

?? ?//打开文件,准备写入
?? ?//这里可以用filename代替
?? ?char* store_file_location;
?? ?store_file_location = new char[100];
?? ?char receive[FILE_NAME_MAX_SIZE];
?? ?cout << "你想放在哪里?" << endl;
?? ?cin >> store_file_location;
?? ?strcpy(receive, store_file_location);
?? ?//strcpy(receive, "E:\\");
?? ?int file_name_size = strlen(file_name);

?? ?while (file_name[file_name_size] != '\\'&&file_name_size > 0) { file_name_size--; }
?? ?char real_name[FILE_NAME_MAX_SIZE];
?? ?memset(real_name, 0, sizeof(real_name));
?? ?strcpy(real_name, file_name + file_name_size + 1);

?? ?strcat(receive, "\\");
?? ?cout << "接收到的真实名字" << real_name << endl;
?? ?strcat(receive, real_name);
?? ?FILE *fp = fopen(receive, "wb"); ?//以只写,二进制的方式打开一个文件wb
?? ?assert(NULL != fp);
?? ?memset(buffer, 0, BUFFER_SIZE);//初始化
?? ?int length = 0;
?? ?while ((length = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0)//>0
?? ?{
?? ??? ?assert(fwrite(buffer, sizeof(char), length, fp) >= (size_t)length);
?? ??? ?memset(buffer, 0, BUFFER_SIZE);
?? ?}
?? ?//messageList.AddString("下载成功");
?? ?cout << "下载成功";
?? ?std::cout << "接受文件 : " << file_name << endl << " 接收成功 !" << std::endl;
?? ?fclose(fp);
?? ?closesocket(client_socket);
}

int main()
{
?? ?CFileTranslaterClientDlg clg;
?? ?cout << "欢迎使用文件服务器" << endl
?? ??? ?<< "连接服务器" << endl;
?? ?clg.OnBnClickedButton1();
?? ?cout << "******************" << endl;
?? ?cout<< "请选择服务器的工作模式(输入数字)" << endl
?? ??? ?<< "--> ?0.上传文件 " << endl
?? ??? ?<< "--> ?1.下载文件" << endl
?? ??? ?<< "--> ?2.关闭程序" << endl;
?? ?int choose = 0;
?? ?cin >> choose;
?? ?
??? ?switch (choose)
?? ?{
?? ?case 0:
?? ??? ?clg.OnBnClickedButton3();
?? ??? ?break;
?? ?case 1:
?? ??? ?
?? ??? ?clg.OnBnClickedButton4();
?? ??? ?break;
?? ?case 2:
?? ??? ?return 0;
?? ?default:
?? ??? ?cout << "输入错误" << endl;
?? ?}
?? ?return 0;


}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2021-11-26 08:41:11  更:2021-11-26 08:43:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 9:35:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码