windows C++使用socket发送和接收文件

 
 
如有看不懂的地方建议从这里开始
C++下TCP发送hello world的过程:https://blog.csdn.net/qq_39511059/article/details/79658841
大体思路就是将文件分块,然后转化为char流传输。
//server.cpp用来接受文件
#define _CRT_SECURE_NO_WARNINGS 1
#include <winsock2.h> 
#include <stdio.h> 
#include <windows.h>
#include <string.h>
#pragma comment(lib,"ws2_32.lib")
#include<iostream>
using namespace std;
struct FileName {//用来存储和传输文件名扩展名的结构体
	char Fname[64];
	int len;
};
class FileReciever {
private:
	int server;
	int client;
	sockaddr_in sa;
	sockaddr_in ca;
	char buff[1024];
	FILE *fp;
	FileName fn;
public:
	FileReciever()
	{
		server = socket(AF_INET, SOCK_STREAM, 0);
		client = socket(AF_INET, SOCK_STREAM, 0);

	}
	int Listen(int port)
	{
		sa.sin_family = AF_INET;
		sa.sin_port = htons(port);
		sa.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

		bind(server, (sockaddr*)&sa, sizeof(sa));
		listen(server, 5);
		int len = sizeof(ca);
		printf("waiting for connect\n");
		client = accept(server, (sockaddr*)&ca, &len);
		if (client == INVALID_SOCKET)
			cout << "failed" << endl;
		return 1;
	}
	int RecieveFile(const char *path)
	{
		char p[64];
		strcpy_s(p,64, path);
		int len = strlen(p);
		if (p[len - 1] != '\\')
		{
			p[len] = '\\';
			len++;
		}
		recv(client, (char*)&fn, sizeof(fn),0);
		strcat(p, fn.Fname);
		long long siz;
		recv(client, (char*)&siz, sizeof(siz), 0);
		siz = siz / 1024;
		fp = fopen(p, "wb+");
		long long int index = 0;
		int num;
		while (index<=siz)
		{
			num = recv(client, buff, 1024, 0);
			if (num <= 0)
				break;
			fwrite(buff, (int)num, 1, fp);
			index++;

			cout << (int)index * 100 / siz << num<<endl;
		}
		printf("seccess\n");
		return 0;
	}
};
int main()
{
	WSADATA WSAData;
	WSAStartup(MAKEWORD(2, 2), &WSAData);
	FileReciever fr;
	fr.Listen(3000);
	char p[] = "F:\\";
	fr.RecieveFile(p);
	system("pause");
}


//client.cpp用来发送文件
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1
#define _CRT_SECURE_NO_WARNINGS 1
#include<winsock2.h>
#include<stdlib.h>
#include<iostream>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
struct FileName {//存储形式:"文件名.扩展名"
	char Fname[64];
	int len;
};
class FileSender
{
private:
	FILE * fp;
	SOCKET sock;
	sockaddr_in addr;
	FileName fn;
	char temp[1024];
public:
	FileSender()
	{
		sock = socket(AF_INET, SOCK_STREAM, 0);
	}
	int Connect(const char *ip, int port)
	{
		addr.sin_family = AF_INET;
		addr.sin_port = htons(port);
		addr.sin_addr.S_un.S_addr = inet_addr(ip);

		if (connect(sock, (sockaddr*)&addr, sizeof(addr)) != 0)
		{
			cout << "failed.." << endl;
		}
		return 1;
	}
	int openFile(const char *path)
	{
		char name[32], ext[16];
		_splitpath(path, NULL, NULL, name, ext);
		strcat_s(name, 32, ext);
		strcpy_s(fn.Fname, 32, name);
		fn.len = strlen(fn.Fname);
		fp = fopen(path, "rb");
		return 0;
	}
	int SendFile(const char *path)
	{
		openFile(path);
		send(sock, (char*)&fn, sizeof(FileName),0);
		fseek(fp, 0, SEEK_END);
		long long siz = ftell(fp);
		fseek(fp, 0, SEEK_SET);
		send(sock, (CHAR*)&siz, sizeof(siz),0);
		siz = siz / 1024;
		long long index=0;
		int num;
		while (1)
		{
			num=fread(temp, 1, 1024, fp);
			if (num == 0)
				break;
			send(sock, temp, num, 0);
			index++;
			cout << (int)index*100/siz<<"%" << num << endl;
		}
		cout << "Successfully send" << endl;
		return 0;
	}

};
int main()
{ 
	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);
	char path[] = "F:\\Program Files\\newText.txt";//发送文件的路径
	FileSender fs;
	if(fs.Connect("127.0.0.1", 3000))
	fs.SendFile(path); 
	system("pause");
}


评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值