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

📄 wcy_serverdlg.cpp

📁 本代码是语音识别的应用程序
💻 CPP
字号:
// WCY_SERVERDlg.cpp : implementation file
//

#include "stdafx.h"
#include "WCY_SERVER.h"
#include "WCY_SERVERDlg.h"

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

SOCKET m_socket;
SOCKET s_c;
//DWORD dwip;
//char* filename;
CString m_sendfname;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About


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()

/////////////////////////////////////////////////////////////////////////////
// CWCY_SERVERDlg dialog

CWCY_SERVERDlg::CWCY_SERVERDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CWCY_SERVERDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CWCY_SERVERDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CWCY_SERVERDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CWCY_SERVERDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CWCY_SERVERDlg, CDialog)
	//{{AFX_MSG_MAP(CWCY_SERVERDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BNT_SEND, OnBntSend)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWCY_SERVERDlg message handlers

BOOL CWCY_SERVERDlg::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
	InitSocket();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CWCY_SERVERDlg::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 CWCY_SERVERDlg::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 CWCY_SERVERDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CWCY_SERVERDlg::InitSocket()
{


/*	m_socket = socket( AF_INET, SOCK_STREAM, 0 );
	if( m_socket == INVALID_SOCKET )
	{
		::MessageBox(m_hWnd,"创建失败","警告",MB_OK);
		return 0;	 
	}

	SOCKADDR_IN addrSock;
	memset(&addrSock,0,sizeof(addrSock));
	//////////////////////////////////////////////////////////
	//DWORD dwip;
    //((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwip);
	//////////////////////////////////////////////////////////
	addrSock.sin_addr.S_un.S_addr=inet_addr("192.168.1.193");
	addrSock.sin_family=AF_INET;
	addrSock.sin_port=htons(8439);
    int nResult = 0;
	nResult = connect(m_socket,(const sockaddr*)&addrSock,sizeof(addrSock));
	if( nResult == SOCKET_ERROR )
	{
		::MessageBox(m_hWnd,"连接失败","警告",MB_OK);
		return 0;		
	}
	*/
return 1;
}

void CWCY_SERVERDlg::OnBntSend() 
{

	m_socket = socket( AF_INET, SOCK_STREAM, 0 );
	if( m_socket == INVALID_SOCKET )
	{
		::MessageBox(m_hWnd,"创建失败","警告",MB_OK);
		return ;	 
	}

	SOCKADDR_IN addrSock;
	memset(&addrSock,0,sizeof(addrSock));
	//////////////////////////////////////////////////////////
	DWORD dwip;
    ((CIPAddressCtrl*)GetDlgItem(IDC_IPADDRESS1))->GetAddress(dwip);
	//////////////////////////////////////////////////////////
	addrSock.sin_addr.S_un.S_addr=htonl(dwip);
	addrSock.sin_family=AF_INET;
	addrSock.sin_port=htons(8439);
    int nResult = 0;
	nResult = connect(m_socket,(const sockaddr*)&addrSock,sizeof(addrSock));
	if( nResult == SOCKET_ERROR )
	{
		::MessageBox(m_hWnd,"连接失败","警告",MB_OK);
		return ;		
	}
	
	// TODO: Add your control notification handler code here
	//filename="E:\\吴春远\\p_Spain.vox";
    //  int nResult=0;
    char * filename=NULL;
	filename=m_sendfname.GetBuffer(m_sendfname.GetLength()); 
	nResult = FileSend( m_socket, filename );
	if( nResult < 0 )
	{
		return ;	
	}	

}

int CWCY_SERVERDlg::FileSend(SOCKET S, char *filename)
{
	SOCKET s=S;
	FILE*				stream;
	struct Fileinfo 	fileinfo;
	int					nResult; 
	if( !( stream = fopen(filename, "rb") ) )
	{
		return 0;
	} 
	memset( &fileinfo, 0x00, sizeof(fileinfo) );

    memcpy( &fileinfo.fileName, filename, sizeof( fileinfo.fileName ) );

	fileinfo.fileBlock = SEND_BUFFER_SIZE;

	long temp = filesize( stream );

	if(temp < SEND_BUFFER_SIZE)
		fileinfo.BlockNum = 1;
	else
		fileinfo.BlockNum = temp / SEND_BUFFER_SIZE + 1;
	fileinfo.fileSize = filesize( stream );

	unsigned int infolen = sizeof( fileinfo );
	do
	{	
		unsigned int len = sizeof( fileinfo );
		nResult = send( s, (char *)&fileinfo , len , 0 );
		//printf("Send Fileinfo OK!!!\n");
		if ( nResult == SOCKET_ERROR )
		{
			MessageBox("文件发送失败");
			return SOCKET_ERROR;	
		}
		infolen -= nResult;
	}
	while( infolen > 0);		//发送文件基本信息
	BYTE  sendData[SEND_BUFFER_SIZE] ;
	memset( sendData ,0x00 , SEND_BUFFER_SIZE );

	int allSendSIZE = filesize( stream );
	int NUM = fileinfo.BlockNum - 1;
	int endNum = temp % SEND_BUFFER_SIZE;
	int ii = 1;
	int jj=1;

	do
	{
		unsigned long  sendThisTime, doneSoFar, buffOffset;
		buffOffset = 0;
		while( !feof( stream ) )
		{	
			if( ii <= NUM)
			{
				memset( sendData ,0x00 , SEND_BUFFER_SIZE );
				fread( sendData, SEND_BUFFER_SIZE, 1, stream);
			}
			else
			{
				memset( sendData ,0x00 , SEND_BUFFER_SIZE );
				fread( sendData, endNum, 1, stream);
			}
			if( allSendSIZE < SEND_BUFFER_SIZE )
				sendThisTime = endNum;
			else
				sendThisTime = SEND_BUFFER_SIZE;
			do
			{
				Sleep(150);
				doneSoFar = send( s, (char *)sendData, sendThisTime , 0 ); 
				// test for errors and get out if they occurred
				if ( doneSoFar == SOCKET_ERROR )
				{
					return SOCKET_ERROR;
				}
				buffOffset += doneSoFar;
				sendThisTime -= doneSoFar;
				allSendSIZE -= doneSoFar;

			}
			while ( sendThisTime > 0 );

		}	
///////////////////////////////////////////////////////////////////////////////////////////////
	//	recv( *s, ( char *)&ii, sizeof(ii), 0 );
	SOCKADDR_IN addrFrom;
	int len=sizeof(SOCKADDR);

	char recvBuf[2000];
	int retval;
	retval=recvfrom(s,recvBuf,2000,0,(SOCKADDR*)&addrFrom,&len);


///////////////////////////////////////////////////////////////////////////////////////////
	CString str_a=recvBuf;
	
    SetDlgItemText(IDC_EDIT_RESULT,str_a);	
	}
	while ( allSendSIZE > 0 );
//	delete[]  sendData;
	fclose( stream );
	//////////////////////////////////
	//shutdown(s,   0);
	//closesocket(s);
	return 1;
}

long CWCY_SERVERDlg::filesize(FILE *stream)
{
   long curpos, length; 
   curpos = ftell(stream); 
   fseek(stream, 0L, SEEK_END); 
   length = ftell(stream); 
   fseek(stream, curpos, SEEK_SET); 
   return length;
}

void CWCY_SERVERDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	/*
  SOCKADDR_IN   sockAddr;   
  int   iLen=sizeof(sockAddr);
  getpeername(m_socket,(struct   sockaddr   *)&sockAddr,&iLen);//
  //getsockname(m_socket,(struct   sockaddr   *)&sockAddr,&iLen);//得到本地的IP地址和端口号   
  char * strAddr=NULL;
  int IPPort;
  strAddr   =   inet_ntoa(sockAddr.sin_addr);//IP    
  IPPort=sockAddr.sin_port;   //端口号
	*/
	///////////////////////////////////////
	int i=22;
	send( m_socket, (char *)&i, sizeof(i) , 0 );
	closesocket(m_socket);
	CDialog::OnCancel();
}

void CWCY_SERVERDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	///	filename=NULL;
	CFileDialog fileDlg(TRUE);                //创建打开文件对话框
	fileDlg.m_ofn.lpstrTitle="打开文件";
	fileDlg.m_ofn.lpstrFilter="All Files(*.*)\0*.*\0\0"; //设定筛选

	if(IDOK==fileDlg.DoModal())
		m_sendfname=fileDlg.GetPathName();
//	filename=m_sendfname.GetBuffer(m_sendfname.GetLength());  
	
}

⌨️ 快捷键说明

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