📄 myqqdlg.cpp
字号:
// MyQQDlg.cpp : implementation file
//
#include "stdafx.h"
#include "listen.h"
#include "MyQQDlg.h"
#include "class\\FaderWnd.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#include <dlgs.h>
#define PORT 34567
#define FLAG 2
#define SIZEFILE 1024
const int SOCK_TCP = 0; //TCP模式
const int SOCK_UDP = 1; //UPD模式
CWinThread *pThreadSendFile; //发送文件线程-->_SendFileThread
CWinThread *pThreadSendMsg; //发送消息线程
CWinThread *pThreadLisen; //监听线程-->_ListenTcpThread
CWinThread *pReceiveThread; //接受线程-->_ReceiveThread
/////////////////////////////////////////////////////////////////////////////
// CMyQQDlg dialog
CMyQQDlg::CMyQQDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMyQQDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMyQQDlg)
m_MsgSend = _T("");
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON2);
m_nSockType=0;//TCP
m_WorkType=2;//两者
m_client=0;
m_server=0;
FileWork=false;
FileStop=false;
StopServer=false;
}
void CMyQQDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMyQQDlg)
DDX_Control(pDX, IDC_LIST_BOX_ADDMSG, m_AddMsgLIst);
DDX_Control(pDX, IDC_IPADDRESS, m_You_IP);
DDX_Control(pDX, IDC_PROGRESS_SEND_FILE, m_Progress);
DDX_Text(pDX, IDC_EDIT_SENDMSG, m_MsgSend);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMyQQDlg, CDialog)
//{{AFX_MSG_MAP(CMyQQDlg)
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
ON_BN_CLICKED(IDC_BUTTON_DISCONNECT, OnButtonDisconnect)
ON_BN_CLICKED(IDC_BUTTON_SEND_MSG, OnButtonSendMsg)
ON_BN_CLICKED(IDC_BUTTON_SEND_FILE, OnButtonSendFile)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
ON_BN_CLICKED(IDC_RADIO_TCP, OnRadioTcp)
ON_BN_CLICKED(IDC_RADIO_UDP, OnRadioUdp)
ON_BN_CLICKED(IDC_RADIO_SERVER, OnRadioServer)
ON_BN_CLICKED(IDC_RADIO_CLIENT, OnRadioClient)
ON_BN_CLICKED(IDC_RADIO_BOTH, OnRadioBoth)
ON_BN_CLICKED(IDC_BUTTON_STOP_FILE, OnButtonStopFile)
//ON_MESSAGE(WM_KSEND,OnKSend)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMyQQDlg message handlers
BOOL CMyQQDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
/////////////////////////////////////////////////////////
CString strLocalName;
GetLocalHostName(strLocalName);
CString strLocalIP;
GetIpAddress(strLocalName,strLocalIP);
m_You_IP.SetWindowText(strLocalIP); //设置默认IP为本机
/////////////////////////////////////////////////////////
((CButton*)GetDlgItem(IDC_RADIO_BOTH))->SetCheck(BST_CHECKED);//默认为服务器、客户端一体
GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("启 动");
GetDlgItem(IDC_BUTTON_DISCONNECT)->SetWindowText("关 闭");
((CButton*)GetDlgItem(IDC_RADIO_TCP))->SetCheck(BST_CHECKED);//默认为TCP
GetDlgItem(IDC_BUTTON_SEND_MSG)->EnableWindow(false);//发送消息不可用
GetDlgItem(IDC_BUTTON_SEND_FILE)->EnableWindow(false);//发送文件不可用
GetDlgItem(IDC_BUTTON_CLEAR)->EnableWindow(false);//清除不可用
GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(false);//断开连接不可用
GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_HIDE);
return TRUE; // return TRUE unless you set the focus to a control
}
HCURSOR CMyQQDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//////////////////////////////////线程///////////////////////////////////////////
//////////////////////////////////线程///////////////////////////////////////////
//////////////////////////////////服务器线程开始///////////////////////////////////////////
//TCP监听线程
UINT _ListenTcpThread(LPVOID lparam)
{
CMyQQDlg *pDlg=(CMyQQDlg *)lparam;
if(pDlg->StopServer==true) return -1;
CSocket sockSrvr;
pDlg->m_Potr=PORT+pDlg->m_server;//保存当前使用端口,用于关闭
int createSucceed=sockSrvr.Create(pDlg->m_Potr);
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();
char flag[FLAG]={0};
if(recSo.Receive(flag,FLAG)!=2)
{
return -1;
}
pDlg->m_type=flag[0];
if(pDlg->m_type=='D') return 0;
pThreadLisen=::AfxBeginThread(_ListenTcpThread,pDlg);
pDlg->ReceiveFileMsg(recSo,client);
return 0;
}
UINT _UDPThread(LPVOID lparam) /////UDP接受信息线程开始
{
CMyQQDlg *pDlg=(CMyQQDlg *)lparam;
if(pDlg->StopServer==true) return -1;
CSocket sockSrvrUdp;
sockSrvrUdp.Create(PORT+pDlg->m_client,SOCK_DGRAM);
char buff[100]={0};
int ret=0;
CString ipStr;
CString msg;
UINT port;
for(;;)
{
ret=sockSrvrUdp.ReceiveFrom(buff,100,ipStr,port);//IP和port均为返回值
if(buff[0]=='D') return 0;
if(ret==SOCKET_ERROR)
{
break;
}
msg.Format(buff);
pDlg->AddMsgList(ipStr,msg);
}
sockSrvrUdp.Close();
return 0;
}
////////////////////////////////////服务器线程结束//////////////////////////////////////
////////////////////////////////////客户端线程开始//////////////////////////////////////
//发送文件线程
UINT _SendFileThread(LPVOID lparam)
{
CMyQQDlg *pDlg=(CMyQQDlg *)lparam;
if(pDlg->StopServer==true) return -1;
CSocket sockClient;
sockClient.Create();
CString ip;
pDlg->m_You_IP.GetWindowText(ip);
sockClient.Connect(ip, PORT+pDlg->m_client);
//首先发送标记F为文件,2
int end=0;
end=sockClient.Send("F",FLAG);
///////////////////////////////////////////////////////////////////发送标志是否成功
if(end==SOCKET_ERROR)
{
AfxMessageBox("_SendFileThread Send错误!"+pDlg->GetError(GetLastError()));
return -1;
}
else if(end!=2)
{
AfxMessageBox("文件头错误");
return -1;
}
///////////////////////////////////////////////////////////////////
CFile myFile;
FILEINFO myFileInfo;
if(!myFile.Open(pDlg->m_fileName, CFile::modeRead | CFile::typeBinary))
return -1;
myFileInfo.fileLength=myFile.GetLength(); //得到文件大小
strcpy(myFileInfo.fileName,myFile.GetFileName());//得到文件名称
sockClient.Send(&myFileInfo,sizeof(myFileInfo)); //发送文件信息
pDlg->m_Progress.SetRange32(0,myFileInfo.fileLength);
myFile.Seek(0,CFile::begin);
char m_buf[SIZEFILE]={0};
CString strError;
int num=0;
end=0;
int temp=0;
pDlg->GetDlgItem(IDC_BUTTON_STOP_FILE)->EnableWindow(true);
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;
end=sockClient.Send(m_buf, num);
temp+=end;
pDlg->m_Progress.SetPos(temp);
if(pDlg->FileStop==true)
{
pDlg->FileStop=false;
pDlg->FileWork=false;
break;
}
if(end==SOCKET_ERROR)
{
AfxMessageBox("_SendFileThread Send错误!"+pDlg->GetError(GetLastError()));
break;
}
}
pDlg->m_Progress.SetPos(0);
CString strLocalName;
pDlg->GetLocalHostName(strLocalName);
CString strLocalIP;
pDlg->GetIpAddress(strLocalName,strLocalIP);
if(temp==myFileInfo.fileLength)
pDlg->AddMsgList(strLocalIP+"->"+strLocalName,"文件发送成功");
else
pDlg->AddMsgList(strLocalIP+"->"+strLocalName,"文件发送失败");
myFile.Close();
sockClient.Close();
pDlg->FileWork=false;
pDlg->GetDlgItem(IDC_PROGRESS_SEND_FILE)->ShowWindow(SW_HIDE);
pDlg->GetDlgItem(IDC_BUTTON_STOP_FILE)->EnableWindow(false);
pDlg->GetDlgItem(IDCANCEL)->EnableWindow(true);
pDlg->GetDlgItem(IDC_BUTTON_DISCONNECT)->EnableWindow(true);
return 0;
}
UINT _SendMsgThread(LPVOID lparam) //TCP发送信息线程
{
CMyQQDlg *pDlg=(CMyQQDlg *)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, 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;
}
UINT _ThreadCapture(LPVOID lparam) //抓取对方屏幕线程
{
CMyQQDlg *pDlg=(CMyQQDlg *)lparam;
if(pDlg->StopServer==true) return -1;
CSocket sockClient;
sockClient.Create();
CString ip;
pDlg->m_You_IP.GetWindowText(ip);
sockClient.Connect(ip, PORT+pDlg->m_client);
//首先发送标记C为抓取,2
int end=0;
end=sockClient.Send("C",FLAG);
///////////////////////////////////////////////////////////////////发送标志是否成功
if(end==SOCKET_ERROR)
{
AfxMessageBox("_ThreadCapture Send错误!"+pDlg->GetError(GetLastError()));
return -1;
}
else if(end!=2)
{
AfxMessageBox("发送头错误");
return -1;
}
return 0;
}
///////////////////////////////////////////////////////////////////
UINT _SendMsgUdpThread(LPVOID lparam) //UDP发送信息
{
CMyQQDlg *pDlg=(CMyQQDlg *)lparam;
if(pDlg->StopServer==true) return -1;
CSocket sockClientUdp;
pDlg->m_type=PORT+pDlg->m_client+10;
sockClientUdp.Create(pDlg->m_type,SOCK_DGRAM);
CString strMsg=pDlg->m_MsgSend;
int ret=0;
CString ipStr;
pDlg->m_You_IP.GetWindowText(ipStr);
UINT port=PORT+pDlg->m_server;
ret=sockClientUdp.SendTo(strMsg,strMsg.GetLength(),port,ipStr);
if(ret==SOCKET_ERROR)
{
DWORD error=GetLastError();
}
CString strLocalName;
pDlg->GetLocalHostName(strLocalName);
CString strLocalIP;
pDlg->GetIpAddress(strLocalName,strLocalIP);
pDlg->AddMsgList(strLocalIP+"->"+strLocalName,strMsg);
sockClientUdp.Close();
return 0;
}
////////////////////////////////////客户端线程结束//////////////////////////////////////
///////////////////////////////////函数//////////////////////////////////////////
int CMyQQDlg::ReceiveFileMsg(CSocket &recSo,SOCKADDR_IN &client)//接受函数
{
// if(StopServer==true) return -1;
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -