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

📄 serversocket.cpp

📁 电子监控的网络编程实例
💻 CPP
字号:
// ServerSocket.cpp : implementation file
//

#include "stdafx.h"
#include "videocapture.h"
#include "ServerSocket.h"
#include "VideoCaptureView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CServerSocket

CServerSocket::CServerSocket()
{
	m_bConnected=FALSE;
}

CServerSocket::~CServerSocket()
{
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CServerSocket, CAsyncSocket)
	//{{AFX_MSG_MAP(CServerSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CServerSocket member functions

void CServerSocket::OnAccept(int nErrorCode) 
{
	CVideoCaptureView *pView = CVideoCaptureView::GetView();
	while(!pView)
	{
		pView = CVideoCaptureView::GetView();
	}
	SOCKADDR_IN client;
	int iAddrSize=sizeof(client);
	if(pView->m_skt==INVALID_SOCKET && !m_bConnected)
	{
		Accept(pView->m_skt,(SOCKADDR*)&client,&iAddrSize);
		RemoteAdd=inet_ntoa(client.sin_addr);
	}
	
//	ShowMessage("远程客户["+RemoteAdd+"]连接成功");
//	::KillTimer(pView->GetSafeHwnd(),IDC_TIME_CAPTURE);
	pView->SetRemoteMode("远程客户["+RemoteAdd+"]连接成功");
//	::SetTimer(pView->GetSafeHwnd(),IDC_TIME_CAPTURE,100,0);
	m_bConnected=TRUE;
	pView->m_dwManualTransRecordID=0;
	
	///远程传输线程	

	::WaitForSingleObject(pView->hThreadCopy,INFINITE); //等待旧的传输线程结束
//	::CloseHandle(pView->hThreadCopy);
	DATASEND* ptp = new DATASEND;
	ptp->pView = pView;
	pView->ExitSendDataThread=FALSE;
	pView->pThreadDataSend=AfxBeginThread(ThreadDataSend, ptp,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
	::DuplicateHandle(GetCurrentProcess(),pView->pThreadDataSend->m_hThread,GetCurrentProcess(),&pView->hThreadCopy,0,FALSE,DUPLICATE_SAME_ACCESS);
	pView->pThreadDataSend->ResumeThread();
	pView->pcsDlg->GetDlgItem(IDC_STATIC_FTP)->SetWindowText("传输状态:启动");
	CAsyncSocket::OnAccept(nErrorCode);
}

void CServerSocket::OnClose(int nErrorCode) 
{
	CVideoCaptureView *pView = CVideoCaptureView::GetView();	

	ShowMessage("远程客户断开连接");	
	pView->SetRemoteMode("远程客户断开连接");
//	m_bConnected=FALSE;
	
	CAsyncSocket::OnClose(nErrorCode);
}

void CServerSocket::OnConnect(int nErrorCode) 
{
	if (nErrorCode)
	{
	}
	else
	{
		//显示连接成功
		CVideoCaptureView *pView = CVideoCaptureView::GetView();
		pView->SetRemoteMode("连接主程序成功");
		pView->Listen();
	}	
	
	CAsyncSocket::OnConnect(nErrorCode);
}

⌨️ 快捷键说明

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