protocol.cpp

来自「gprs」· C++ 代码 · 共 968 行 · 第 1/2 页

CPP
968
字号
	conn = sockClient.Connect(ip, PORT + pDlg->m_client);

	if(conn==0)				///////////////////////////////////
	{
		AfxMessageBox("_SendMsg Connect错误!" + pDlg->GetError(GetLastError()));
		sockClient.ShutDown(2);
		sockClient.Close();
	//	Sleep(2000);
		AfxEndThread(1L);
		return -1;	
	}
	
	g_bLink = 1;     //socket is succeed
	//首先发送标记cmd type|cmd|len|checksum
	int end = 0;
	g_cSendLen = FillSendData(TRANS_INFO, QUERY_SERVER_UPDAGE_DATA, 0, NULL);
	end = sockClient.Send(g_cSendBuf, g_cSendLen); 
	if(end == SOCKET_ERROR)
	{
		AfxMessageBox("_SendMsg Send错误!"+pDlg->GetError(GetLastError()));
		return -1;
	}
	else if(end != sizeof(packData))
	{
		AfxMessageBox("消息头错误");
		return -1;
	}

	CString strLocalName;
	pDlg->GetLocalHostName(strLocalName);
	CString strLocalIP;
	CString str = GetMessageString(g_cSendBuf[1]);
	pDlg->GetIpAddress(strLocalName,strLocalIP);
	pDlg->AddMsgList(strLocalIP+"->"+strLocalName,str);

	char buf[10];
	int s=1;

	while(s!=SOCKET_ERROR && g_bLink)
	{
		s = sockClient.Receive(buf,10);
	
		packData.cmd_type = buf[0];
		packData.cmd = buf[1];
		Client_ReceiveFileMsg(pDlg,sockClient);
		
	}
	sockClient.Close();
	g_bLink = 0;
	
	return 0;
}

#if 0
UINT _SendMsgThread(LPVOID lparam)	//TCP发送信息线程
{
	
	CGprsServerDlg *pDlg=(CGprsServerDlg *)lparam;
	if(pDlg->StopServer == true)	
		return -1;

	CSocket sockClient;
	sockClient.Create();
	CString ip,strError;

	pDlg->m_YourIP.GetWindowText(ip);
	int conn=0;
	conn = sockClient.Connect(ip, PORT + pDlg->m_client);

	if(conn==0)				///////////////////////////////////
	{
		AfxMessageBox("_SendMsgThread Connect错误!" + pDlg->GetError(GetLastError()));
		sockClient.ShutDown(2);
		sockClient.Close();
	//	Sleep(2000);
		AfxEndThread(1L);
		return 0;	
	}
	//首先发送标记cmd type|cmd|len|checksum
	int end = 0;
	end = sockClient.Send(g_cSendBuf, g_cSendLen); 
	if(end == SOCKET_ERROR)
	{
		AfxMessageBox("_SendMsgThread Send错误!"+pDlg->GetError(GetLastError()));
		return -1;
	}
	else if(end != sizeof(packData))
	{
		AfxMessageBox("消息头错误");
		return -1;
	}

	CString strLocalName;
	pDlg->GetLocalHostName(strLocalName);
	CString strLocalIP,str;
	str = GetMessageString(g_cSendBuf[1]);
	pDlg->GetIpAddress(strLocalName,strLocalIP);
	pDlg->AddMsgList(strLocalIP+"->"+strLocalName,str);
	sockClient.Close();
//	Sleep(2000);
	return 0;
}
#endif
////////////////////////////////////客户端线程开始//////////////////////////////////////
#if 1
//发送文件线程
UINT  CStatDir::_SendFileThread(LPVOID lparam)
{
	uchar count;
	uchar buf[20];
	
	CGprsServerDlg *pDlg=(CGprsServerDlg *)lparam;
	if(pDlg->StopServer==true)	return -1;
/*	
	CSocket sockClient;
	sockClient.Create();
	CString ip;
	pDlg->m_YourIP.GetWindowText(ip);
	sockClient.Connect(ip, PORT+pDlg->m_client); 
*/
	//首先发送标记F为文件,
	int end=0;
	g_cSendLen = FillSendData(TRANS_FILE,0,0,NULL);
	count = Bytes2String(g_cSendBuf, buf, g_cSendLen);
	end = pLinkSocket->Send(buf, count);
	
	Sleep(2000);
	///////////////////////////////////////发送标志是否成功
	if(end == SOCKET_ERROR)										
	{
		AfxMessageBox("_SendFileThread Send错误!" + pDlg->GetError(GetLastError()));
		return -1;
	}
	
	else if(end != count) 
	{
		AfxMessageBox("文件头错误");
		return -1;
	}
	
	///////////////////////////////////////////////////////////////////
	CFile myFile;
	FILEINFO myFileInfo;
	pDlg->m_fileName = g_sFileName;
	AfxMessageBox(pDlg->m_fileName);
	
	pDlg->GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_SHOW);
	
	if(!myFile.Open(pDlg->m_fileName, CFile::modeRead | CFile::typeBinary))
		return -1;
	
	memset((uchar *)&myFileInfo,0,sizeof(myFileInfo));
	myFileInfo.fileLength = myFile.GetLength();			//得到文件大小
	strcpy(myFileInfo.fileName,myFile.GetFileName());	//得到文件名称

/*	myFileInfo.fileLength=556677;

	strcpy(myFileInfo.fileName,"davidli.jpg");*/
	pLinkSocket->Send((unsigned char *)&myFileInfo,sizeof(myFileInfo));	//发送文件信息

//	BYTE buf1[62];
//	memcpy(buf1,(uchar *)&myFileInfo,sizeof(myFileInfo)); 
	
	CString str;
	//str = buf;
//	str = myFileInfo.fileName;
//	AfxMessageBox(str); 
//	while(1);  
	
	Sleep(2000);
	pDlg->m_Progress.SetRange32(0,myFileInfo.fileLength);
	myFile.Seek(0,CFile::begin);

	uchar m_buf[SIZEFILE]={0};
//	uchar m_buf1[SIZEFILE*2];
	CString strError;
	int num=0;
	end=0;
	DWORD temp=0; 
	pDlg->GetDlgItem(IDC_BUTTON_STOP_FILE)->EnableWindow(true);
	
//	pLinkSocket->Send("David Ready",11);	//发送文件信息
//	Sleep(3000);
	for(;;)
	{

		if(pDlg->FileWork==false)
		{
			pDlg->FileWork=true;
		//	pDlg->GetDlgItem(IDCANCEL)->EnableWindow(false);
			pDlg->GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(false);
		}
		num = myFile.Read(m_buf, SIZEFILE);
	
		if(num==0) break;
	//	num = Bytes2String(m_buf,m_buf1,SIZEFILE);
		end = pLinkSocket->Send(m_buf, SIZEFILE); 
		temp += end;
		pDlg->m_Progress.SetPos(temp);
		if(pDlg->FileStop==true) 
		{
			pDlg->FileStop=false;
			pDlg->FileWork=false;
			break;
		}
		if(end == SOCKET_ERROR)
		{
		//	AfxMessageBox("LTH");
			AfxMessageBox("_SendFileThread Send错误!"+pDlg->GetError(GetLastError()));
			break;
		}
		Sleep(1000);
	}
	pDlg->m_Progress.SetPos(0);
	CString strLocalName;
	pDlg->GetLocalHostName(strLocalName);
	CString strLocalIP;
	pDlg->GetIpAddress(strLocalName,strLocalIP);
//	CString str;
	str = myFileInfo.fileName;
	
	if(temp == myFileInfo.fileLength)
		str += "文件发送成功";
	else
		str += "文件发送失败";
	
		pDlg->AddMsgList(strLocalIP+"->"+strLocalName, str);
	
	myFile.Close();
	

	pDlg->FileWork = false;
	pDlg->GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_HIDE);

	m_nFileCount++;
	pDlg->m_ProgressFileCnt.SetPos(m_nFileCount);
 
//	pDlg->GetDlgItem(IDC_BUTTON_STOP_FILE)->EnableWindow(false);

//	pDlg->GetDlgItem(IDCANCEL)->EnableWindow(true);
//	pDlg->GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(true);
	if(WaitClientAck_FileReceived())
	{
		//CString str= youName+"<-"; //+strOut;
		//	AddMsgList(str, GetMessageString(packData.cmd));
		//AfxMessageBox("Client received 1 file");
		return 0;
	}
	else
	{
		return -1;	
	}
	
}
#endif

uchar WaitClientAck_FileReceived(void)
{
	uchar ch=1;
	char buf[20];
	uchar buf1[10];

	memset(buf,0,20);
	while(ch != SOCKET_ERROR)
	{
		ch = pLinkSocket->Receive(buf,20);
		
		String2bytes(&buf[1],buf1,8);
		if((buf1[0] == 'M') && (buf1[1] == CLIENT_ONEFILE_RECEIVED))
			return 1;
		else
			return 0;
	}
	return 0;
}




//TCP监听线程
UINT _ListenTcpThread(LPVOID lparam)
{
	CGprsServerDlg *pDlg=(CGprsServerDlg *)lparam;

	if(pDlg->StopServer==true)	return -1;
	
	CSocket sockSrvr;
	pDlg->m_Port = PORT + pDlg->m_server;	//保存当前使用端口,用于关闭
	
	int createSucceed=sockSrvr.Create(pDlg->m_Port);
	if(createSucceed==0)
	{
		AfxMessageBox("_ListenTcpThread Create错误!"+pDlg->GetError(GetLastError()));
		return -1;
	}
	
	int listenSucceed = sockSrvr.Listen();	//开始监听

	if(listenSucceed == 0)
	{
		AfxMessageBox("_ListenTcpThread Listen错误!"+pDlg->GetError(GetLastError()));
		return -1;
	}
	
	CSocket recSo;
	SOCKADDR_IN client;
	int iAddrSize = sizeof(client);
	int acceptSucceed = sockSrvr.Accept(recSo, (SOCKADDR *)&client, &iAddrSize);	//接受连接并取得对方IP
	if(acceptSucceed == 0)
	{
		AfxMessageBox("_ListenTcpThread Accept错误!" + pDlg->GetError(GetLastError()));
		return -1;
	}
	sockSrvr.Close();

	pThreadLisen=::AfxBeginThread(_ListenTcpThread,pDlg);
	
//	Sleep(2000);
	char buf1[20];
	int s=1;
	BYTE buf[10];
	
    while(s != SOCKET_ERROR)
    {
		memset(buf1,0,20);
		s = recSo.Receive(buf1,20);

	/*	if(s != 8) 
		{
		  CString str;
		   str.Format("%d",s);
		   CString str1 ="接收长度不对:" + str;
			AfxMessageBox(str1);
			return -1;	
		}*/
		
		BYTE count2 = String2bytes((char *)&buf1[1],buf,s);

	/*	CString str;
		str.Format("%c",buf1[0]);
		AfxMessageBox(str);
		str.Format("%d",buf[0]);
		AfxMessageBox(str);
	*/
		memcpy((uchar *)&packData,buf,count2);
		pDlg->m_type=buf[0];
		if(pDlg->m_type=='D') return 0;
		pDlg->ReceiveFileMsg(recSo,client);
	}
	 return 0;
}

UINT _SearchFileThread(LPVOID lparam)	//TCP发送信息线程
{
	
	CGprsServerDlg *pDlg=(CGprsServerDlg *)lparam;
	
	if(pDlg->StopServer==true)	return -1;
	
	//构造类对象
	CStatDir statdir(lparam);

	AfxMessageBox(sSrcPath);
	//设置要遍历的目录
	if (!statdir.SetInitDir(sSrcPath))
	{
		AfxMessageBox("目录不存在。");
		return -1;
	}
	//开始遍历
	statdir.SetWorkMode(0);
	statdir.BeginBrowse("*.jpg");

	//CString str;
	//str.Format("length=%d",statdir.GetFileCount());
	
	pDlg->GetDlgItem(IDC_PROGRESS_SEND_FILECOUNT)->ShowWindow(SW_SHOW);
	pDlg->m_ProgressFileCnt.SetRange32(0,statdir.GetFileCount());

	statdir.SetWorkMode(1);
	statdir.ResetFileCount();
	statdir.BeginBrowse("*.jpg");
	pDlg->GetDlgItem(IDC_PROGRESS_SEND_FILECOUNT)->ShowWindow(SW_HIDE);
	
	AfxMessageBox("ok");

	//统计结果中,子目录个数不含 . 及 ..
	//printf("文件总数: %d\n子目录总数: %d\n",statdir.GetFileCount(),
	//statdir.GetSubdirCount();
	return 0;
}

UINT _SendMsgUdpThread(LPVOID lparam)		//UDP发送信息线程
{
	return 0;
}

int CGprsServerDlg::SaveYouFile(CSocket &recSo)	//接受文件
{
	CString fname;
//	CFileDialog dlg(false);				//另存文件
	FILEINFO myFileInfo;
	
	recSo.Receive(&myFileInfo,sizeof(FILEINFO));
	int fileLength=myFileInfo.fileLength;

	CString strfileIp,strfileName,strfileLength;
//	strfileIp.Format(inet_ntoa(client.sin_addr));
	strfileName.Format(myFileInfo.fileName);
	strfileLength.Format("%f",myFileInfo.fileLength/1024.0);

#if 0
	CString title="文件"+strfileName+" 大小"+strfileLength+"KB "+"来在"+strfileIp+" 是否接受";
	dlg.m_ofn.lpstrTitle=title;			//标题条
	char fileme[500]={0};				//必须足够大小
	strcpy(fileme,strfileIp+strfileName);
	dlg.m_ofn.lpstrFile=fileme;			//文件名称
	if(dlg.DoModal()==IDOK)
	{
		fname=dlg.GetPathName();	//得到文件名名称、路径
		GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_SHOW);
	}
	else
	{
		GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_HIDE);
		GetDlgItem(IDC_BUTTON_STOP_FILE)->EnableWindow(false);
		
		GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(true);
		GetDlgItem(IDCANCEL)->EnableWindow(true);
		
		recSo.Close();	
//		Sleep(2000);
		return 0;	
	}
#endif
	fname = sDstPath + strfileName;

	char buf[SIZEFILE]={0};
	CFile f(fname,CFile::modeCreate|CFile::modeWrite);	//存文件
	
	m_Progress.SetRange32(0,fileLength);
	
	int n = 0;					//接受的字节数 0表示结束
	int temp = 0;
	
	GetDlgItem(IDC_BUTTON_STOP_FILE)->EnableWindow(true);
	GetDlgItem(IDCANCEL)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(false);
	
	
	for(;;)
	{
		n=recSo.Receive(buf,SIZEFILE);		//接受
		if(n==0)							//0表示结束
			break;							//接受完毕
		f.Write(buf,n);
		temp += n;
		m_Progress.SetPos(temp);
		if(FileWork==false) FileWork=true;
		if(FileStop==true) 
		{
			FileStop=false;
			FileWork=false;
			break ;
		}
		
	}
	f.Close();
	m_Progress.SetPos(0);

/*	if(temp==fileLength)
		AddMsgList(inet_ntoa(client.sin_addr),strfileName+"文件接受成功");
	else
		AddMsgList(inet_ntoa(client.sin_addr),strfileName+"文件接受失败");
*/
	FileWork=false;
	GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_HIDE);
	GetDlgItem(IDC_BUTTON_STOP_FILE)->EnableWindow(false);
	
	GetDlgItem(IDCANCEL)->EnableWindow(true);
	GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(true);
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?