概述
// file_client.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include "pch.h"
#include <iostream>
#include<WinSock2.h>
#include<thread>
#include<fstream>
#pragma comment(lib,"ws2_32.lib")
using namespace std;
//=====================全局常量================
#define port 8901
#define client_ipadd "127.0.0.1"
const int BUFFER_SIZE = 1024;
//=====================全局变量================
SOCKET client;
sockaddr_in saddr;
sockaddr_in caddr;
int slen = sizeof(saddr);
int clen = sizeof(caddr);
char buf[200];
char discon[] = "byebye";
char goon[] = "next";
ifstream fp;
struct File {
char buffer[BUFFER_SIZE];
int length;
int flag;
}file;
//=====================函数声明================
void initSocket();
void getFileName(char path[], char name[]);
void openFile(char path[]);
int getFileLength(char buf[]);
bool choose();
//DWORD WINAPI sendFile_Thread(LPVOID lp);
void sendFile_Thread();
int main()
{
//初始化listenSer套接字
initSocket();
memset(buf, 0, sizeof(buf));
int irecv=recv(client, buf, sizeof(buf), 0);
if (irecv == SOCKET_ERROR) {
::cout << "接收错误!" << endl;
closesocket(client);
WSACleanup();
exit(1);
}
::cout <<buf<< endl;
sendFile_Thread();
//开始发送文件
/*HANDLE hsend_Thread = CreateThread(NULL, 0, sendFile_Thread, (LPVOID)&file, 0, NULL);
if (hsend_Thread == NULL) {
cout << "线程创建失败" << endl;
}*/
//CloseHandle(hsend_Thread);
closesocket(client);
WSACleanup();
system("pause");
return 0;
}
void initSocket() {
WSADATA wsadata;
WORD version = MAKEWORD(2, 2);
if (::WSAStartup(version, &wsadata)) {
::cout << "加载Socket失败!" << endl;
system("pause");
exit(1);
}
//创建socket
client = socket(AF_INET, SOCK_STREAM, 0);
if (client == INVALID_SOCKET) {
::WSACleanup();
system("pause");
exit(1);
}
//初始化地址包
saddr.sin_family = AF_INET;
saddr.sin_port = htons(port);
saddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
//初始化变量
slen = sizeof(saddr);
clen = sizeof(caddr);
if (connect(client, (SOCKADDR*)&saddr, slen)==SOCKET_ERROR) {
cout << "连接服务器失败!" << endl;
closesocket(client);
WSACleanup();
system("pause");
exit(1);
}
}
// 判断文件夹是否存在
BOOL IsDirExist(char cdir[])
{
string dir(cdir);
size_t origsize = dir.length() + 1;
const size_t newsize = 100;
size_t convertedChars = 0;
wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(dir.length() - 1));
mbstowcs_s(&convertedChars, wcstring, origsize, dir.c_str(), _TRUNCATE);
DWORD dwAttrib = GetFileAttributes(wcstring);
return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 != (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
//打开文件函数声明,特别注意是传递引用,不然后续操作会出错
void openFile(char data[]) {
while (1) {
cout << "请输入要发送文件的路径: " << endl;
cin >> data; //输入图片的绝对路径
// 以读 / 写方式打开一个二进制文件,只允许读 / 写数据。若图片无法打开,则路径有问题,关闭连接
fp.open(data, ios::in | ios::binary);
if (!(fp.is_open())) {
memset(data, 0, sizeof(data));
cout << "图片路径出错,请重新尝试!" << endl;
}
else {
break;
}
}
}
void getFileName(char path[], char name[]) {
int len = strlen(path);
int count = 0;
for (int i = len; i > 0; i--) {
if (path[i] != '\') {
count++;
}
else {
break;
}
}
int j = 0;
int pos = len - count + 1;
for (int i = pos; i < len; i++) {
name[j++] = path[i];
}
name[j] = '