TCP文件传输MFC客户端

本文介绍如何使用Visual Studio 2015实现TCP协议下的文件传输功能,包括连接服务器、发送文件及接收文件的操作流程,并提供关键代码片段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在VS2015下实现的TCP文件传输,界面如下:


连接服务器button功能:

1:加载socket库

2.创建套接字

3:连接服务器

button消息响应如下:

void CFileClientDlg::OnConnect()
{
	// TODO: Add your control notification handler code here
	WORD ver = MAKEWORD(2, 0);
	WSADATA wsadata;
	if (WSAStartup(ver, &wsadata))
	{
		MessageBox(_T("load winsock dll failing"), _T("load"));
		return;
	}
	//create socket
	socket_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (INVALID_SOCKET == socket_client)
	{
		MessageBox(_T("create socket failing"), _T("create"));
		return;
	}
	//connet server
	sockaddr_in addr;
	memset(&addr, 0, sizeof(sockaddr_in));
	addr.sin_family = AF_INET;
	addr.sin_port = htons(10000);
	addr.sin_addr.S_un.S_addr = inet_addr("10.0.210.99");
	if (connect(socket_client, (sockaddr*)&addr, sizeof(sockaddr)))
	{
		MessageBox(_T("connect server failed"), _T("connect"));
		return;
	}

	/*m_sock.Create();
	if (!m_sock.Connect(_T("10.0.210.99"), 10000))
	{
		MessageBox(_T("connect failed"), _T("connect"));
		EndDialog(IDCANCEL);
		return;
	}*/
//	socket_client = m_sock.m_hSocket;

//	Sleep(1);
	GetDlgItem(IDC_CONNECT)->EnableWindow(false);
//	MessageBox(_T("successful open"));
}
连接服务器后,此按钮不可用;
class CFileClientDlg : public CDialogEx
{
// Construction
public:
	CFileClientDlg(CWnd* pParent = NULL);	// standard constructor

// Dialog Data
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_FILECLIENT_DIALOG };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support


// Implementation
protected:
	HICON m_hIcon;
	SOCKET socket_client;
	CSocket m_sock;
dlg头文件加两个成员变量socket socket_client以及CSocket m_sock;

发送文件button消息响应实习如下:

void CFileClientDlg::OnSendfile()
{
	// TODO: Add your control notification handler code here
	//open file
	CFileDialog send_dlg(TRUE);
	if (IDOK == send_dlg.DoModal())
	{
		//get route
		CString path_name = send_dlg.GetPathName();
		//create file
		CFile file_context(path_name, CFile::modeRead);
		char context[256]{ 0 };
		//send file
		while (file_context.Read(context, 256))
		{
			//send context
			if (SOCKET_ERROR == send(socket_client, context, 256, NULL))
			{
				MessageBox(_T("send failed"), _T("send"));
				return;
			}
			memset(context, 0, 256);
		}

		//close file
		file_context.Close();
		MessageBox(_T("send successful"), _T("send"));
	}
}
接收文件button消息响应如下:

void CFileClientDlg::OnRecvfile()
{
	// TODO: Add your control notification handler code here
	CFileDialog recv_dlg(false);
	if (IDOK == recv_dlg.DoModal())
	{
		CString file_path = recv_dlg.GetPathName();
		//
		CFile file_recv(file_path, CFile::modeCreate | CFile::modeWrite);
		//receive content
		if (socket_client)
		{
			char recv_context[256] = { 0 };
			while (recv(socket_client, recv_context, 255, NULL))
			{
				//fill content
				file_recv.Write(recv_context, strlen(recv_context));
				if (strlen(recv_context) < 255)
				{
					break;
				}
			}
		}
		//close file
		file_recv.Close();
		MessageBox(_T("receive successful"), _T("receive"));
	}
}
退出button消息响应如下:

void CFileClientDlg::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	closesocket(socket_client);
	WSACleanup();
	CDialogEx::OnCancel();
}
在此,这些还涉及到了文件操作。

文件创建的过程中包含了winsock

编译过程中可能遇到一个问题,响应解决办法:http://blog.youkuaiyun.com/thecentry/article/details/78955431

源代码的地址为:http://download.youkuaiyun.com/download/thecentry/10188769




评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值