⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mysocket.cpp

📁 自己编的把数控代码用RS232口由计算机传输到数控机床的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
}



UINT _SendMsgThread(LPVOID lparam)	//TCP发送信息线程
{
	
	CMySocket *pDlg=(CMySocket *)lparam;
	if(pDlg->StopServer==true)	return -1;
	
	CSocket sockClient;
	sockClient.Create();
	CString ip,strError;
	pDlg->m_You_IP.GetWindowText(ip);
	int conn=sockClient.Connect(ip, pDlg->m_port+pDlg->m_client);
	if(conn==0)	///////////////////////////////////
	{
		AfxMessageBox("_SendMsgThread Connect错误!"+pDlg->GetError(GetLastError()));
		sockClient.ShutDown(2);
		sockClient.Close();
		AfxEndThread(1L);
		return 0;
		
	}
	//首先发送标记M为信息,2
	int end=0;
	end=sockClient.Send("M",FLAG); 
	if(end==SOCKET_ERROR)
	{
		AfxMessageBox("_SendMsgThread Send错误!"+pDlg->GetError(GetLastError()));
		return -1;
	}
	else if(end!=2)
	{
		AfxMessageBox("消息头错误");
		return -1;
	}
	CString strMsg=pDlg->m_MsgSend;
	end=sockClient.Send(strMsg,strMsg.GetLength()); 
	if(end==SOCKET_ERROR)
	{
		AfxMessageBox("_SendMsgThread Send错误!"+pDlg->GetError(GetLastError()));
		return -1;
	}
	CString strLocalName;
	pDlg->GetLocalHostName(strLocalName);
	CString strLocalIP;
	pDlg->GetIpAddress(strLocalName,strLocalIP);
	pDlg->AddMsgList(strLocalIP+"->"+strLocalName,strMsg);
	
	int i=0;
	sockClient.Close();

	return 0;
}

///////////////////////////////////////////////////////////////////
////////////////////////////////////客户端线程结束//////////////////////////////////////

///////////////////////////////////函数//////////////////////////////////////////

int CMySocket::ReceiveFileMsg(CSocket &recSo,SOCKADDR_IN &client)//接受函数
{
 if(m_type=='F')			//文件
	{
		SaveYouFile(recSo,client);
	}
	
	else if(m_type=='M')	//信息
	{
		char buff[100]={0};
		CString msg;
		int ret=0;
		for(;;)
		{
			ret=recSo.Receive(buff,100);
			if(ret==0)
				break;
			msg+=buff;
		}
		CString strOut,strIn;
		m_You_IP.GetWindowText(strIn);
		GetNamebyAddress(strIn,strOut);
		CString youName;
		youName.Format(inet_ntoa(client.sin_addr));
		CString str=youName+"<-"+strOut;
		AddMsgList(str,msg);
	}
	recSo.Close();	
	return 0;
}

int CMySocket::SaveYouFile(CSocket &recSo, SOCKADDR_IN &client)//接受文件
{
	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);
	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();	
		return 0;	
		
	}
	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),"文件接受成功");
	else
		AddMsgList(inet_ntoa(client.sin_addr),"文件接受失败");
	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;
}
void CMySocket::AddMsgList(CString IP,CString str) //添加信息于LISTBOX 控件中
{
	SYSTEMTIME tm;
	GetLocalTime(&tm);
	CString time;
	time.Format(_T(" %d:%02.2d"), tm.wHour, tm.wMinute);
	m_AddMsgLIst.AddString(IP+"于"+time+"说道: "+str);
	int numList=m_AddMsgLIst.GetCount()-1;
	GetDlgItem(IDC_BUTTON_CLEAR)->EnableWindow(true);
	m_AddMsgLIst.SetTopIndex(numList);
	m_AddMsgLIst.SetCurSel(numList);
	//水平滚动
	int max_width=0;
	CSize sz;
	CClientDC dc(this);
	for(int i=0;i<m_AddMsgLIst.GetCount();i++)
	{
		m_AddMsgLIst.GetText(i,str);
		sz=dc.GetTextExtent(str);
		if(max_width<sz.cx)
			max_width=sz.cx;
	}
	m_AddMsgLIst.SendMessage(LB_SETHORIZONTALEXTENT,max_width,0);

}

void CMySocket::OnButtonConnect() 
{
    UpdateData(true);
	CString str;
	m_You_IP.GetWindowText(str);
	CString strOut,strIn;
	m_You_IP.GetWindowText(strIn);
	if(GetNamebyAddress(strIn,strOut)==-1)
	{
		GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(true);//连接可用
		return;
	}
	m_MsgSend="你好!和"+strIn+"连接成功!";


	//
	pThreadLisen=::AfxBeginThread(_ListenTcpThread,this);	//开始TCP线程
	GetDlgItem(IDC_BUTTON_SEND_MSG)->EnableWindow(true);//发送消息可用
	GetDlgItem(IDC_BUTTON_SEND_FILE)->EnableWindow(true);//文件可用
		
	GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(true);//断开可用
	GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(false);//连接不可用
	GetDlgItem(IDC_IPADDRESS)->EnableWindow(false);

		UpdateData(false);
	
}

void CMySocket::OnButtonDisconnect() 
{
    GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(true);
	GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON_SEND_MSG)->EnableWindow(false);
	GetDlgItem(IDC_BUTTON_SEND_FILE)->EnableWindow(false);
	m_AddMsgLIst.ResetContent();
	GetDlgItem(IDC_BUTTON_CLEAR)->EnableWindow(false);
	GetDlgItem(IDC_IPADDRESS)->EnableWindow(true);
	m_AddMsgLIst.SendMessage(LB_SETHORIZONTALEXTENT,0,0);

/////////////////////////////////////////发送结束
    DWORD   dwStatus;
		if (pThreadLisen != NULL)
		{
			if(::GetExitCodeThread(pThreadLisen->m_hThread, &dwStatus)==0)
			{
				int errror=GetLastError();
				return;
			}
			if (dwStatus == STILL_ACTIVE)
			{
				CSocket sockClient;
				sockClient.Create();
				CString ip,strError;
				ip="127.0.0.1";
				int conn=sockClient.Connect(ip, m_Potr);
				if(conn==0)	///////////////////////////////////
				{
					AfxMessageBox("关闭错误!"+GetError(GetLastError()));
					sockClient.ShutDown(2);
					sockClient.Close();
					return;
					
				}
				sockClient.Send("D",FLAG); //结束
				
			}
			else
			{
				delete pThreadLisen;
				pThreadLisen = NULL;
			}
		}
	
}

void CMySocket::OnButtonSendMsg() 
{
    UpdateData(true);
	if(m_MsgSend.GetLength()==0) return;
	::AfxBeginThread(_SendMsgThread,this);
	
}

void CMySocket::OnButtonSendFile() 
{
    CFileDialog dlg(true);
	CString ip;
	m_You_IP.GetWindowText(ip);
	CString title="文件发往"+ip+"请选择";
	dlg.m_ofn.lpstrTitle=title;//标题条
	if(dlg.DoModal()==IDOK)
	{
		m_fileName=dlg.GetPathName();
		GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_SHOW);
		pThreadSendFile=::AfxBeginThread(_SendFileThread,this);	//开始传送文件线程
	}
	
}

void CMySocket::OnButtonStopFile() 
{
    FileStop=true;
	FileWork=false;
	GetDlgItem(IDCANCEL)->EnableWindow(true);
	GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(true);	
}

void CMySocket::OnButtonClear() 
{
    m_AddMsgLIst.ResetContent();
	GetDlgItem(IDC_BUTTON_CLEAR)->EnableWindow(false);
	m_AddMsgLIst.SendMessage(LB_SETHORIZONTALEXTENT,0,0);
	
}
LRESULT CMySocket::OnKSend(WPARAM wParam,LPARAM lParam)
{
	OnButtonSendMsg();
	return 0;
}

BOOL CMySocket::PreTranslateMessage(MSG* pMsg) 
{
if (pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
		
		if(GetFocus()->GetDlgCtrlID()==IDC_EDIT_SENDMSG ||GetFocus()->GetDlgCtrlID()==IDC_BUTTON_SEND_MSG)
		{
			AfxGetMainWnd()->SendMessage(WM_KSEND);
			return TRUE;
		}
	
	return CDialog::PreTranslateMessage(pMsg);
}

⌨️ 快捷键说明

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