VC++ Windows Socket 传输文件

服务端

// 服务端
#include <cstdio>
#include <cstdlib>
#include <WinSock2.h>
#include <Ws2tcpip.h>   // inet_pton 函数所在头文件
#pragma comment(lib, "ws2_32.lib") // 加载 ws2_32.dll

#define BUF_SIZE 1024

int main()
{
	// 检查文件是否存在
	const char *filename = "D:\\script.sql";
	FILE *fp = nullptr;
	fopen_s(&fp, filename, "rb"); // 二进制方式打开
	if (!fp)
	{
		printf("%s 文件打开出错!", filename);
		system("pause");
		exit(0);
	}

	long curPos = ftell(fp);
	if (fseek(fp, 0, SEEK_END) != 0) // 成功返回 0
	{
		exit(0);
	}
	long fileSize = ftell(fp);
	if (fseek(fp, curPos, SEEK_SET) != 0)
	{
		exit(0);
	}
	printf("文件大小: %d 字节。", fileSize);

	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);
	SOCKET servSock = socket(AF_INET, SOCK_STREAM, 0);

	SOCKADDR_IN sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));
	sockAddr.sin_family = PF_INET;
	//sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1"); 
	// 有警告,用下面新函数替换 https://blog.csdn.net/qq_22642239/article/details/102456232
	inet_pton(AF_INET, "127.0.0.1", &sockAddr.sin_addr.s_addr);
	sockAddr.sin_port = htons(1234);

	bind(servSock, (SOCKADDR*)(&sockAddr), sizeof(SOCKADDR));
	listen(servSock, 1);

	SOCKADDR clntAddr;
	int nSize = sizeof(SOCKADDR);
	SOCKET clntSock = accept(servSock, (SOCKADDR *)(&clntAddr), &nSize);

	// 循环发送数据
	char buffer[BUF_SIZE] = { 0 }; // 缓冲区

	size_t nCount = 0;
	while ((nCount = fread(buffer, 1, BUF_SIZE, fp)) > 0)
	{
		send(clntSock, buffer, static_cast<int>(nCount), 0);
	}

	shutdown(clntSock, SD_SEND); // 文件读取完毕,断开输出流,向客户端发送 FIN 包
	recv(clntSock, buffer, BUF_SIZE, 0); // 阻塞, 等待客户端接受完毕

	fclose(fp);
	closesocket(clntSock);
	closesocket(servSock);
	WSACleanup();

	system("pause");
	return 0;
}

客户端

//客户端

#include<stdio.h>
#include<stdlib.h>
#include<WinSock2.h>
#include <Ws2tcpip.h>   // inet_pton 函数所在头文件

#pragma comment(lib,"ws2_32.lib")

#define BUF_SIZE 1024

int main()
{
	//先输入文件名,看文件是否能创建成功
	char filename[128] = "D:\\sock_test_file2.txt";//文件名
	printf("Input filename to save:");
	//gets(filename);
	//以二进制方式打开(创建)文件
	FILE *fp = nullptr;
	fopen_s(&fp, filename, "wb");
	if (!fp)
	{
		printf("Cannot open file, press any key to exit!\n");
		system("pause");
		exit(0);
	}

	WSADATA wsaData;
	WSAStartup(MAKEWORD(2, 2), &wsaData);
	SOCKET sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

	struct sockaddr_in sockAddr;
	memset(&sockAddr, 0, sizeof(sockAddr));
	sockAddr.sin_family = PF_INET;
	//sockAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	inet_pton(PF_INET, "127.0.0.1", &sockAddr.sin_addr.s_addr);
	sockAddr.sin_port = htons(1234);
	connect(sock, (SOCKADDR*)&sockAddr, sizeof(SOCKADDR));

	//循环接收数据,直到文件传输完毕
	char buffer[BUF_SIZE] = { 0 };
	int nCount = 0;
	while ((nCount = recv(sock, buffer, BUF_SIZE, 0)) > 0)
	{
		fwrite(buffer, nCount, 1, fp);
	}
	puts("File transfer success!");

	//文件接收完毕后直接关闭套接字,无需调用shutdown()
	fclose(fp);
	closesocket(sock);
	WSACleanup();

	system("pause");
	return 0;
}

经测试下来,可以传送文件。




代码源自:https://www.cnblogs.com/cq51/p/15533371.html 博文

posted @ 2024-12-21 21:43  double64  阅读(2)  评论(0编辑  收藏  举报