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

📄 rectestdlg.cpp

📁 通过网络发送audio的代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// RecTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RecTest.h"
#include "RecTestDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
CString g_ServerIp;


class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRecTestDlg dialog

CRecTestDlg::CRecTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CRecTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CRecTestDlg)
	m_ServerIp = _T("127.0.0.1");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CRecTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CRecTestDlg)
	DDX_Text(pDX, IDC_EDIT1, m_ServerIp);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CRecTestDlg, CDialog)
	//{{AFX_MSG_MAP(CRecTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BEGIN, OnBegin)
	ON_MESSAGE(MM_WIM_DATA,OnMM_WIM_DATA)
	ON_MESSAGE(MM_WIM_CLOSE,OnMM_WIM_CLOSE)
	ON_MESSAGE(MM_WOM_OPEN,OnMM_WOM_OPEN)
	ON_MESSAGE(MM_WOM_DONE,OnMM_WOM_DONE)
	ON_MESSAGE(MM_WOM_CLOSE,OnMM_WOM_CLOSE)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRecTestDlg message handlers

BOOL CRecTestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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 
	//allocate memory for wave header
	pWaveHdr1=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	pWaveHdr2=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	pWaveHdrOut=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	//***reinterpret_cast ****  operation is used to convert any type of
	//pointer to other type
	//allocate memory for save buffer
	//	CAduioData struct
	for(int i=0;i<InBlocks;i++)
	{
		m_AudioDataIn[i].dwLength = 0;
		m_AudioDataIn[i].lpdata = reinterpret_cast<PBYTE>(malloc(1));

	}

	for(i =0;i<OutBlocks;i++)
	{
		m_AudioDataOut[i].dwLength = 0;
		m_AudioDataOut[i].lpdata = reinterpret_cast<PBYTE>(malloc(1));
	}
	nAudioIn = 0;
	nAudioOut = 0;
	nSend = 0;
	nReceive = 0;
   
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CRecTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CRecTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CRecTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

UINT Audio_Listen_Thread(LPVOID lParam)
{
	CRecTestDlg *pdlg = (CRecTestDlg*)lParam;
	CSocket m_Server;
	DWORD 	length;
	if(!m_Server.Create(3000))
		AfxMessageBox("Listen Socket create error"+pdlg->GetError(GetLastError()));//不可单个运行,重复产生同一端口显然是错误的
	if(!m_Server.Listen()) 
		AfxMessageBox("m_server.Listen ERROR"+pdlg->GetError(GetLastError()));
	CSocket recSo;
	if(! m_Server.Accept(recSo))
	   AfxMessageBox("m_server.Accept() error"+pdlg->GetError(GetLastError()));
	m_Server.Close();	
	int sendcount=0;//just for test
	int ret ;
	while(1)
	{	//	开始循环接收声音文件,首先接收文件长度
		ret = recSo.Receive(&length,sizeof(DWORD));		
		if(ret== SOCKET_ERROR )
			AfxMessageBox("服务器端接收声音文件长度出错,原因: "+pdlg->GetError(GetLastError()));
		if(ret!=sizeof(DWORD))
		{
			AfxMessageBox("接收文件头错误,将关闭该线程");
        		recSo.Close();
			return -1;
		}//接下来开辟length长的内存空间
		pdlg->m_AudioDataOut[pdlg->nReceive].lpdata =(PBYTE)realloc (0,length);
		if (pdlg->m_AudioDataOut[pdlg->nReceive].lpdata == NULL)
		{
			MessageBeep (MB_ICONEXCLAMATION) ;
			AfxMessageBox("erro memory_ReceiveAudio");
			pdlg->OnOK();
			recSo.Close();
			return -1;
		}
		else//内存申请成功,可以进行循环检测接受
		{
			DWORD dwReceived = 0,dwret;
			while(length>dwReceived)
			{
				dwret = recSo.Receive((pdlg->m_AudioDataOut[pdlg->nReceive].lpdata+dwReceived),(length-dwReceived));
				dwReceived +=dwret;
				if(dwReceived ==length)
				{
					pdlg->m_AudioDataOut[pdlg->nReceive].dwLength = length;
					break;
				}
				
			}
		}//本轮声音文件接收完毕
		//	nReceive=3,下面是清0
		pdlg->nReceive=(pdlg->nReceive+1)%OutBlocks;
	}
	recSo.Close();
	pdlg->OnOK();
	return 0;
}
UINT Audio_Send_Thread(LPVOID lParam)//专职的声音文件发送线程
{                                    // 端口也是专用的
	CRecTestDlg *pdlg = (CRecTestDlg*)lParam;
	CSocket m_Client;
	m_Client.Create();
	if(g_ServerIp.IsEmpty())
	{
		::MessageBox(NULL,"Ip is invalidable","Error",MB_OK);
		return 0;
	}
	if( m_Client.Connect(g_ServerIp,3000))
	{		
		DWORD ret, length;
		int count=0;
		while(1)//循环使用指针nSend
		{
			//	如果采集声音满了(一块)以后会调用回调函数,填写dwLength
			length =pdlg->m_AudioDataIn[pdlg->nSend].dwLength;			
        		if(length !=0)
			{	//首先发送块的长度
				if(((ret = m_Client.Send(&length,sizeof(DWORD)))!=sizeof(DWORD))||(ret==SOCKET_ERROR))
				{   
					AfxMessageBox("声音文件头传输错误!"+pdlg->GetError(GetLastError()));
					pdlg->OnOK();
					break;	
				}
				//其次发送块的内容	,循环检测发送
				DWORD dwSent = 0;//已经发送掉的字节数
				while(1)//==============================发送声音文件开始

⌨️ 快捷键说明

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