C++实现socket通信,客户端向服务端发送文件(视频,图片)

第一次发博客,写项目时需要客户端向服务端发送图片,视频,网上找了很多,都不是自己想要的,于是自己写

一个,可能有很多问题,求大佬轻喷,主要参考了《windows网络编程基础教程》(杨传栋,张焕远编)这本书。

服务端:

/**
    @anthor :Aaron
	2018年6月27日

*/
#include <iostream>
#include <fstream>
#include <process.h>       //使用C++运行库中函数创建多线程
#include <WinSock2.h>
#define PORT 8000       //定义服务监听端口
#pragma comment(lib,"ws2_32.lib")
#define _WINSOCK_DEPRECATED_NO_WARNINGS 
#define BUF_SIZE 1024
using namespace std;
char Command[20];     //存放从键盘接收到的结束命令
ifstream inFile;     //定义文件输出流
unsigned int _stdcall GetCommand(void *par);      //声明输入close命令的线程函数
unsigned int _stdcall RecPict(void *par);      //声明接收照片的线程函数
int main()
{
	/*定义相关变量*/
	int sock_server;
	struct sockaddr_in addr, client_addr;
	unsigned hThread1, hThread2;
	unsigned int ThreadID1, ThreadID2;
	int addr_len = sizeof(struct sockaddr_in);

	/*初始化winsock DLL*/
	WSADATA wsaData;
	WORD wVersionRequested = MAKEWORD(2, 2);   //生成版本号
	if (WSAStartup(wVersionRequested, &wsaData) != 0)
	{
		cout << "加载winsock.dll失败!\n";
		return 0;
	}
	/*创建套接字*/
	if ((sock_server = socket(AF_INET, SOCK_STREAM, 0)) < 0)          //创建一个socket
	{
		cout << "创建套接字失败!\n";
		WSACleanup();
		return 0;
	}
	/*设置套接字为非阻塞模式*/
	unsigned long ul = 1;  //设置套接字选项参数
	int nRet = ioctlsocket(sock_server, FIONBIO, (unsigned long*)&ul);
	if (nRet == SOCKET_ERROR)
	{
		cout << "设置套接字选项失败!\n";
		closesocket(sock_server);
		WSACleanup();
		return 0;
	}
	/*绑定IP端口*/
	memset((void *)&addr, 0, addr_len);
	addr.sin_family = AF_INET;
	addr.sin_port = htons(PORT);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);    //使用本机的所有IP地址
	if (bind(sock_server, (LPSOCKADDR)&addr, sizeof(addr)) != 0)
	{
		cout << "绑定地址失败!\n";
		closesocket(sock_server);
		WSACleanup();
		return 0;
	}
	/*开始监听*/
	if (listen(sock_server, 5) != 0)
	{
		cout << "listen函数调用失败!\n";
		closesocket(sock_server);
		WSACleanup();
		return 0;
	}
	else
		cout << "listening........\n";
	/*启动命令接受线程*/
	hThread1 = _beginthreadex(NULL, 0, GetCommand, (LPVOID)Command, 0, &ThreadID1);
	/*接受并处理客户链接*/
	SOCKET newsock;
	while (1)
	{
		if (strcpy(Command, "close") == 0)break; //如果输入close命令则退出
		newsock = accept(sock_server, (LPSOCKADDR)&client_addr, &addr_len);
		if (newsock != INVALID_SOCKET)
		{
			cout << "cnnet from" << inet_ntoa(client_addr.sin_addr) << endl;
			hThread2 = _beginthreadex(NULL, 0, RecPict, (LPVOID)newsock, 0, &ThreadID2);
		}
	}
	inFile.close();
	closesocket(sock_server);
	return 0;
}
/*接收照片函数*/
unsigned int _stdcall RecPict(void *par)
{

	FILE *fp = fopen("E://90.mp4", "wb");  //以二进制方式打开(创建)文件
	char buffer[BUF_SIZE] = { 0 };  //文件缓冲区
	int nCount=0;
	SOCKET sock = (SOCKET)par;
	//send(sock, "ok", 3, 0);
	while ((nCount = recv(sock, buffer, BUF_SIZE, 0)) > 0) {
		fwrite(buffer, nCount, 1, fp);
	}
	puts("File transfer success!");
	fclose(fp);
	closesocket(sock);
	WSACleanup();
	system("pause");
	return 0;

}
/*从键盘接收命令的线程函数*/
unsigned int _stdcall GetCommand(void *par)
{
	char * p = (char *)par;
	while (strcmp(p, "close") != 0)
		cin.get(p, 20);
	return 0;
}

欢迎大家为我指出不足!



相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页