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

📄 udpsrvview.cpp

📁 Nucleus Plus源代码
💻 CPP
字号:
// UdpsrvView.cpp : implementation file
//contains UDP client thread for WinSock 

#include "stdafx.h"
#include "WinDemo.h"
#include "UdpsrvView.h"
#include "UdpsrvDoc.h"
#include "LoopByte.h"

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

static HWND m_hWindow3;
static int nUSBytes, nUSPck, stop_client;
//#define buflength 1000
#define defaultloop 5
#define defaultport 7

HANDLE hStartEvent3 = NULL;
CLoopByte dlg;

UINT UdpcliThread(LPVOID pParam)

{
	
	WSADATA stWSAData;               /* return data for Winsock startup */
	SOCKET s;                        /* socket handle                   */
	struct sockaddr_in stRmtAddr2;    /* structure of remote socket      */
	char OutBuf [1000];         /* array for output                */
	char InBuf [1000];          /* array for input                 */
	
	int cRet;                        /* connection   return error value */
	int sRet;                        /* startup      return error value */
	int snRet;                       /* send         return error value */
	int rRet;                        /* read         return error value */
	int clRet;                       /* close        return error value */
	int n;                           /* return for Select */
	long loop;                        /* counter for loop                */
	long loopmax = 0;                   /* maximum times to do loop        */
	int buflength = 0;
	int sendval;                     /* number of characters sent       */
	int port=7;                     /* number of port to use           */
    FD_SET readfd;
    struct timeval timeout1;
    timeout1.tv_sec = 3;
    timeout1.tv_usec = 0;
	nUSBytes = 0;
	nUSPck = 0;
    stop_client = 0;
	buflength = dlg.m_eBytes;
	loopmax = dlg.m_eLoops;
   
    sRet = WSAStartup(0x0101, &stWSAData);     /*stWSAData not looked at */
    
    if (sRet != 0)                          /* if fail, tell user and exit */
		TRACE("Winsock startup error - Winsock may not be loaded!\n");
	
	
    TRACE("Winsock started\n");              /*information for user */
    TRACE("winsock sockets provided>>%d\n", stWSAData.iMaxSockets);
    TRACE("winsock udp max>>%d\n", stWSAData.iMaxUdpDg);
	
    
    s = socket(PF_INET, SOCK_DGRAM, 0);       /*request dgram A.K.A. udp */
    
    
    if (s == INVALID_SOCKET)                       /* give error message */
		TRACE("Unable to get socket error - %d\n", WSAGetLastError());
	
	
    stRmtAddr2.sin_family               =PF_INET;       /*internet family */
  	stRmtAddr2.sin_port = htons(port);
	stRmtAddr2.sin_addr.s_addr = stRmtAddr.sin_addr.s_addr;

	cRet = connect(s,                                 
		(struct sockaddr FAR *)&stRmtAddr2,
		sizeof(struct sockaddr));
	
	
    if (cRet == SOCKET_ERROR)            
        TRACE("Unable to connect socket- error %d\n", WSAGetLastError());
	
	
	
    TRACE("Socket connected\n");        
    TRACE("Sending to %s\n", inet_ntoa(stRmtAddr2.sin_addr));
    TRACE("on Port # %ld\n", ntohs(stRmtAddr2.sin_port));
    TRACE("for %d loops\n", loopmax);
	
	
	
	
	memset(OutBuf, 'A', buflength);
	sendval = strlen(OutBuf);
	loop = 0;
	while (((loop < loopmax) || (loopmax == 0))
            && (!stop_client) ) /*beginning of loop */		
	{ 
        snRet = send(s,
			(LPSTR)&OutBuf,          /*send the string */
			buflength,           
			0);                      /* flags, 0 = no flags */
            
        if (snRet == SOCKET_ERROR)       
        {
			TRACE("Unable to send Error - %d\n", WSAGetLastError());
            stop_client = 1;
        }
	
		/* return value is number of bytes sent*/
		
        
        memset(InBuf, 0, buflength);
        
        FD_ZERO(&readfd);
        FD_SET(s, &readfd);
        n = select(s, &readfd, 0, 0, &timeout1); 
        if (n != SOCKET_ERROR && n != 0)
        {
	        rRet = recv(s,                    /* time to get the string back */
			(LPSTR)&InBuf,
			buflength,             
			0);

/*            if (rRet == SOCKET_ERROR)
            {
                TRACE("Unable to receive Error - %d\n", WSAGetLastError());
            }
*/
  }
        else
        {
            TRACE("UDP Socket Error");
            stop_client = 1;
        }

		nUSBytes += rRet;

		nUSPck++;
        DWORD dwResult;
		::SendMessageTimeout(m_hWindow3, WM_UPDATE_PROGUSRV, 0, 0, SMTO_BLOCK, 250, &dwResult);
		if (loopmax != 0)
			loop++;
		
	}           /*end of loop */
	
	
	clRet = closesocket(s);       /*only use closesocket, no shutdown() */
	
	if (clRet != 0)            
		TRACE("Problem closing socket! Error - %d\n", WSAGetLastError());
	
	WSACleanup();                        /*De-register WinSock */
    SetEvent(hStartEvent3);
	
    return(0);	
 }                                      /* End of Program */
 
 
 
 /////////////////////////////////////////////////////////////////////////////
 // CUdpsrvView
 
 IMPLEMENT_DYNCREATE(CUdpsrvView, CFormView)
	 
	 CUdpsrvView::CUdpsrvView()
	 : CFormView(CUdpsrvView::IDD)
 {
	 //{{AFX_DATA_INIT(CUdpsrvView)
	 m_udpsrvByte = 0;
	 m_udpsrvPck = 0;
	 //}}AFX_DATA_INIT
 }
 
 CUdpsrvView::~CUdpsrvView()
 {
 }
 
 void CUdpsrvView::DoDataExchange(CDataExchange* pDX)
 {
	 CFormView::DoDataExchange(pDX);
	 //{{AFX_DATA_MAP(CUdpsrvView)
	 DDX_Text(pDX, IDC_UDP_SRV_BYTE, m_udpsrvByte);
	 DDV_MinMaxInt(pDX, m_udpsrvByte, 0, 2147483647);
	 DDX_Text(pDX, IDC_UDP_SRV_PCK, m_udpsrvPck);
	 DDV_MinMaxInt(pDX, m_udpsrvPck, 0, 2147483647);
	 //}}AFX_DATA_MAP
 }
 
 
 BEGIN_MESSAGE_MAP(CUdpsrvView, CFormView)
	 //{{AFX_MSG_MAP(CUdpsrvView)
	 ON_WM_CLOSE()
	 ON_WM_CREATE()
	 ON_WM_DESTROY()
	 //}}AFX_MSG_MAP
	 ON_MESSAGE(WM_UPDATE_PROGUSRV, OnUpdateProgress3)
	 END_MESSAGE_MAP()
	 
	 /////////////////////////////////////////////////////////////////////////////
	 // CUdpsrvView diagnostics
	 
#ifdef _DEBUG
	 void CUdpsrvView::AssertValid() const
 {
	 CFormView::AssertValid();
 }
 
 void CUdpsrvView::Dump(CDumpContext& dc) const
 {
	 CFormView::Dump(dc);
 }
#endif //_DEBUG
 
 /////////////////////////////////////////////////////////////////////////////
 // CUdpsrvView message handlers
 
 void CUdpsrvView::OnClose() 
 {
	 CFormView::OnClose();
 }
 
 int CUdpsrvView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
 {
	 if (CFormView::OnCreate(lpCreateStruct) == -1)
		 return -1;
	 
	dlg.DoModal();
	m_hWindow3 = GetSafeHwnd();
	CWinThread* pThread3 = AfxBeginThread(UdpcliThread, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
	hStartEvent3 = ::CreateEvent(NULL, FALSE, FALSE, NULL);
	 
	 return 0;
 }
 
 void CUdpsrvView::OnDestroy() 
 {
     stop_client = 1;
	 CFormView::OnDestroy();
	 
	WaitForSingleObject(hStartEvent3, INFINITE);
	CloseHandle(hStartEvent3);	 
	 
 }

LRESULT CUdpsrvView::OnUpdateProgress3(WPARAM wParam, LPARAM lParam)
{
    m_udpsrvByte = nUSBytes;
	m_udpsrvPck = nUSPck;
	UpdateData(FALSE); //calls DDX
	return 0;
}

⌨️ 快捷键说明

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