Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1955327
  • 博文数量: 356
  • 博客积分: 8284
  • 博客等级: 中将
  • 技术积分: 4580
  • 用 户 组: 普通用户
  • 注册时间: 2009-05-15 20:25
个人简介

天行健,君子以自强不息

文章分类

全部博文(356)

文章存档

2018年(1)

2016年(4)

2015年(13)

2014年(14)

2013年(2)

2012年(25)

2011年(43)

2010年(65)

2009年(189)

分类: C/C++

2015-09-19 10:45:48


点击(此处)折叠或打开

  1. // vc_tcpclientDlg.cpp : implementation file
  2. //

  3. #include "stdafx.h"
  4. #include "vc_tcpclient.h"
  5. #include "vc_tcpclientDlg.h"

  6. #pragma comment(lib,"ws2_32.lib")
  7. //#include "winsock2 .h"

  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif



  13. //0,非阻塞,1,阻塞
  14. #define            ZS                        1
  15. ///printf控制台输出使能
  16. #define            LZ_PRINTF_ENABLE        0
  17. ///TCP DEBUG 为1,目标IP为局域网IP
  18. #define            LZ_LOCAL_TCP_DEBUG        0
  19. ////定时根据域名解析出对应的实时IP地址
  20. #define            LZ_DNS_TIMER_ENABLE        1

  21. #if LZ_PRINTF_ENABLE>0
  22. #pragma comment(linker, "/subsystem:\"CONSOLE\" /entry:\"WinMainCRTStartup\"")
  23. #endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CAboutDlg dialog used for App About

  26. class CAboutDlg : public CDialog
  27. {
  28. public:
  29.     CAboutDlg();

  30. // Dialog Data
  31.     //{{AFX_DATA(CAboutDlg)
  32.     enum { IDD = IDD_ABOUTBOX };
  33.     //}}AFX_DATA

  34.     // ClassWizard generated virtual function overrides
  35.     //{{AFX_VIRTUAL(CAboutDlg)
  36.     protected:
  37.     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  38.     //}}AFX_VIRTUAL

  39. // Implementation
  40. protected:
  41.     //{{AFX_MSG(CAboutDlg)
  42.     //}}AFX_MSG
  43.     DECLARE_MESSAGE_MAP()
  44. };

  45. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  46. {
  47.     //{{AFX_DATA_INIT(CAboutDlg)
  48.     //}}AFX_DATA_INIT
  49. }

  50. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  51. {
  52.     CDialog::DoDataExchange(pDX);
  53.     //{{AFX_DATA_MAP(CAboutDlg)
  54.     //}}AFX_DATA_MAP
  55. }

  56. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  57.     //{{AFX_MSG_MAP(CAboutDlg)
  58.         // No message handlers
  59.     //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()

  61. /////////////////////////////////////////////////////////////////////////////
  62. // CVc_tcpclientDlg dialog

  63. CVc_tcpclientDlg::CVc_tcpclientDlg(CWnd* pParent /*=NULL*/)
  64.     : CDialog(CVc_tcpclientDlg::IDD, pParent)
  65. {
  66.     //{{AFX_DATA_INIT(CVc_tcpclientDlg)
  67.     m_strTcpTx = _T("");
  68.     //}}AFX_DATA_INIT
  69.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  70.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  71. }

  72. void CVc_tcpclientDlg::DoDataExchange(CDataExchange* pDX)
  73. {
  74.     CDialog::DoDataExchange(pDX);
  75.     //{{AFX_DATA_MAP(CVc_tcpclientDlg)
  76.     DDX_Text(pDX, IDC_EDIT_TX, m_strTcpTx);
  77.     //}}AFX_DATA_MAP
  78. }

  79. BEGIN_MESSAGE_MAP(CVc_tcpclientDlg, CDialog)
  80.     //{{AFX_MSG_MAP(CVc_tcpclientDlg)
  81.     ON_WM_SYSCOMMAND()
  82.     ON_WM_PAINT()
  83.     ON_WM_QUERYDRAGICON()
  84.     ON_WM_TIMER()
  85.     ON_BN_CLICKED(IDC_BUTTON_CONNECT, OnButtonConnect)
  86.     ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
  87.     ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
  88.     ON_BN_CLICKED(IDC_BUTTON_LED1, OnButtonLed1)
  89.     ON_BN_CLICKED(IDC_BUTTON_LED2, OnButtonLed2)
  90.     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  91.     ON_MESSAGE(WM_RECEIVE,OnMsgReceive)
  92.     ON_MESSAGE(WM_TRANS,OnTransmit)
  93.     ON_BN_CLICKED(IDC_BUTTON_AUTO_CONNECT, OnButtonAutoConnect)
  94.     //}}AFX_MSG_MAP
  95. END_MESSAGE_MAP()

  96. /////////////////////////////////////////////////////////////////////////////
  97. // CVc_tcpclientDlg message handlers

  98. BOOL CVc_tcpclientDlg::OnInitDialog()
  99. {
  100.     CDialog::OnInitDialog();

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

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

  105.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  106.     if (pSysMenu != NULL)
  107.     {
  108.         CString strAboutMenu;
  109.         strAboutMenu.LoadString(IDS_ABOUTBOX);
  110.         if (!strAboutMenu.IsEmpty())
  111.         {
  112.             pSysMenu->AppendMenu(MF_SEPARATOR);
  113.             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  114.         }
  115.     }

  116.     // Set the icon for this dialog. The framework does this automatically
  117.     // when the application's main window is not a dialog
  118.     SetIcon(m_hIcon, TRUE);            // Set big icon
  119.     SetIcon(m_hIcon, FALSE);        // Set small icon

  120.     m_bConnected=FALSE;
  121.     char szHostName[]="number007cool.gicp.net";
  122.     InitSocket();
  123.     GetRemoteIP(szHostName);
  124.     InitTimer();
  125.     // TODO: Add extra initialization here
  126.     
  127.     return TRUE; // return TRUE unless you set the focus to a control
  128. }

  129. void CVc_tcpclientDlg::OnSysCommand(UINT nID, LPARAM lParam)
  130. {
  131.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  132.     {
  133.         CAboutDlg dlgAbout;
  134.         dlgAbout.DoModal();
  135.     }
  136.     else
  137.     {
  138.         CDialog::OnSysCommand(nID, lParam);
  139.     }
  140. }

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

  144. void CVc_tcpclientDlg::OnPaint()
  145. {
  146.     if (IsIconic())
  147.     {
  148.         CPaintDC dc(this); // device context for painting

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

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

  157.         // Draw the icon
  158.         dc.DrawIcon(x, y, m_hIcon);
  159.     }
  160.     else
  161.     {
  162.         CDialog::OnPaint();
  163.     }
  164. }

  165. // The system calls this to obtain the cursor to display while the user drags
  166. // the minimized window.
  167. HCURSOR CVc_tcpclientDlg::OnQueryDragIcon()
  168. {
  169.     return (HCURSOR) m_hIcon;
  170. }

  171. int CVc_tcpclientDlg::InitSocket()
  172. {
  173.     WSAData wsadata;
  174.     int wsaret;
  175.     wsaret=WSAStartup(0x0202,&wsadata);
  176.     if (wsaret!=0)
  177.     {
  178.         AfxMessageBox("wsa start failed");
  179.         return 1;
  180.     }

  181.     unsigned long iMode=1;
  182.     ioctlsocket(m_socket,FIONBIO,&iMode);//非阻塞模式
  183.     
  184. //    sockaddr_in localaddr;
  185. //    localaddr.sin_port=htons(51000);
  186. //    bind(m_socket,(sockaddr *)&localaddr,sizeof(localaddr));    
  187.     return 0;
  188.     
  189. }

  190. void CVc_tcpclientDlg::OnButtonConnect()
  191. {
  192.     // TODO: Add your control notification handler code here
  193.     
  194.     GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(FALSE);
  195.     GetDlgItem(IDC_STATIC_CONNECT)->SetWindowText("");
  196.     Sleep(500);
  197.     GetDlgItem(IDC_BUTTON_CONNECT)->EnableWindow(TRUE);
  198.     

  199.     if (m_bConnected==FALSE)
  200.     {
  201. #if ZS==0
  202.         unsigned long iMode=1;
  203. #else
  204.         unsigned long iMode=0;
  205. #endif
  206.         m_socket=::socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  207.         if(m_socket==INVALID_SOCKET)
  208.         {
  209.             AfxMessageBox("failed socket");
  210.         }
  211. #if LZ_LOCAL_TCP_DEBUG>0
  212.         m_strRemoteIP="192.168.7.111";//test use
  213. #endif    
  214.         m_serveraddr.sin_family=AF_INET;
  215.         m_serveraddr.sin_port=htons(51000);
  216.         m_serveraddr.sin_addr.S_un.S_addr=inet_addr(m_strRemoteIP);

  217.         ioctlsocket(m_socket,FIONBIO,(u_long FAR *)&iMode);
  218.         
  219.         struct fd_set mask;
  220.         FD_ZERO(&mask);
  221.         FD_SET(m_socket,&mask);
  222.         struct timeval timeout;
  223.         timeout.tv_sec=2;
  224.         timeout.tv_usec=0;

  225.     
  226. #if ZS==0
  227.      if (select(0,&mask,NULL,NULL,&timeout)==1)
  228.      {
  229. #endif    
  230.             int bconnect=connect(m_socket,(sockaddr *)&m_serveraddr,sizeof(m_serveraddr));
  231.                 //    printf("connect : %d\r\n",bconnect);
  232.             if ( bconnect==-1 )
  233.             {
  234.                 GetDlgItem(IDC_STATIC_CONNECT)->SetWindowText("连接失败");
  235.                 m_bConnected=FALSE;
  236.             }
  237.             else
  238.             {
  239.                 GetDlgItem(IDC_STATIC_CONNECT)->SetWindowText("连接成功");
  240.                 m_bConnected=TRUE;
  241.                 GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("断开");
  242.                 DWORD dwid;
  243.             
  244.             m_hThread=CreateThread(NULL,1024,(LPTHREAD_START_ROUTINE)ThreadRecvProc,this,0,&dwid);
  245.             }    
  246.         
  247. #if ZS==0            
  248.      }
  249. #endif    
  250.     
  251.     }
  252.     else if (m_bConnected==TRUE)
  253.     {
  254.         GetDlgItem(IDC_BUTTON_CONNECT)->SetWindowText("连接");
  255.         closesocket(m_socket);
  256.         m_bConnected=FALSE;
  257.         return;
  258.     }
  259.     
  260. }
  261. int CVc_tcpclientDlg::GetRemoteIP(char *szName)
  262. {
  263.     HOSTENT *host_entry;
  264.     u_char byhostip[4];
  265.     try
  266.     {
  267.         host_entry=gethostbyname(szName);
  268.     }
  269.     catch (...)
  270.     {
  271.         return 1;
  272.     }
  273.     
  274.     if (host_entry!=0)
  275.     {
  276.         CString strTemp;
  277.         m_strRemoteIP="";
  278.     
  279.         for (int i=0;i<4;i++)
  280.         {
  281.             strTemp.Format("%u",(u_char)host_entry->h_addr_list[0][i]);
  282.             m_strRemoteIP+=strTemp;
  283.             if (i!=3)
  284.             {
  285.                 m_strRemoteIP+=".";
  286.             }
  287.         }
  288.     ///    m_strRemoteIP="112.10.198.139";
  289.         GetDlgItem(IDC_STATIC_REMOTEIP)->SetWindowText(m_strRemoteIP);
  290.     }

  291.     return 0;
  292. }
  293. ////定时器处理函数 /////////////////////////////
  294. void CVc_tcpclientDlg::OnTimer(UINT nIDEvent)
  295. {
  296.     // TODO: Add your message handler code here and/or call default
  297.     LONG ret;
  298.     char szname[]="number007cool.gicp.net";
  299.     switch( nIDEvent)
  300.     {
  301.         case 1:
  302.             try
  303.             {
  304. #if LZ_DNS_TIMER_ENABLE>0
  305.                 GetRemoteIP(szname);
  306. #endif
  307.             }
  308.             catch (CException* e)
  309.             {
  310.                 
  311.             }
  312.             break;
  313.         case 2://定时发送读取温度请求

  314.             ret=send(m_socket,"C",1,0);
  315.             printf("send ret is %d\r\n",ret);
  316.             if (ret<0)//根据发送函数返回值判断连接断开
  317.             {
  318.                 OnButtonConnect();
  319.             }
  320.             SendMessage(WM_TRANS,0,ret);
  321.         
  322.             break;
  323.         case 6://定时判断连接是否断开
  324.             if (m_bConnected==FALSE)
  325.             {
  326.                 OnButtonConnect();
  327.             }
  328.             else
  329.             {
  330.                 OnButton1();//get temp
  331.             }
  332.             break;
  333.         default:
  334.             break;
  335.     }
  336.     CDialog::OnTimer(nIDEvent);
  337. }

  338. int CVc_tcpclientDlg::InitTimer()
  339. {
  340.     SetTimer(1,1000,NULL);


  341.     return 0;
  342. }



  343. void CVc_tcpclientDlg::OnButtonSend()
  344. {
  345.     // TODO: Add your control notification handler code here
  346.     UpdateData(TRUE);
  347.     int ret=send(m_socket,m_strTcpTx,m_strTcpTx.GetLength(),0);
  348.     printf("sned: %d\r\n",ret);

  349.     ::SendMessage(AfxGetMainWnd()->m_hWnd,WM_TRANS,ret,0);
  350.     if (ret<0)
  351.     {
  352.         m_bConnected=FALSE;
  353.     }

  354. }
  355. ////////////////接收线程
  356. int CVc_tcpclientDlg::ThreadRecvProc(LPVOID lparam)
  357. {
  358.     BYTE byrecv[2024];
  359.     int ret;
  360.     static BYTE byflag=0;    
  361.     CVc_tcpclientDlg *dlg=(CVc_tcpclientDlg *)lparam;
  362.     CString strtemp;
  363.     CString strrx;
  364.     while (TRUE)
  365.     {
  366.     //    if(!dlg->m_bConnected)
  367.             //break;

  368.         ret=recv(dlg->m_socket,(char *)byrecv,1024,0);
  369.         if (ret>0)
  370.         {    
  371.             
  372.             //for (int i=0;i<ret;i++)
  373.             {
  374.                 strtemp.Format("%s",byrecv);
  375.                 strrx+=strtemp+"\r\n";
  376.             }
  377.             dlg->GetDlgItem(IDC_EDIT_RX)->SetWindowText(strrx);
  378.             strrx="";
  379.             ::SendMessage(AfxGetMainWnd()->m_hWnd,WM_RECEIVE,0,0);
  380.         }
  381.         Sleep(1);
  382.     }
  383.     
  384.     return 0;
  385. }
  386. //// 接收灯 显示 消息响应函数
  387. void CVc_tcpclientDlg::OnMsgReceive(WPARAM wParam, LPARAM lParam)
  388. {
  389.     HWND hwnd=::GetDlgItem(m_hWnd,IDC_GREEN);
  390.     if (::IsWindowVisible(hwnd))
  391.     {
  392.         ::ShowWindow(hwnd,SW_HIDE);
  393.     }
  394.     else
  395.     {
  396.         ::ShowWindow(hwnd,SW_SHOW);
  397.     }
  398. }
  399. //// 发送灯 显示 消息响应函数
  400. void CVc_tcpclientDlg::OnTransmit(WPARAM wparam, LPARAM lparam)
  401. {
  402.     if (!m_bConnected)
  403.     {
  404.         return;
  405.     }
  406.     HWND hwnd=::GetDlgItem(m_hWnd,IDC_RED);
  407.     if (lparam>0)//如果发送成功就闪灯
  408.     {
  409.         if (::IsWindowVisible(hwnd))
  410.         {
  411.             ::ShowWindow(hwnd,SW_HIDE);
  412.         }
  413.         else
  414.         {
  415.             ::ShowWindow(hwnd,SW_SHOW);
  416.         }
  417.     }

  418. }

  419. void CVc_tcpclientDlg::OnButton2()
  420. {
  421.     // TODO: Add your control notification handler code here
  422.     static int flag=0;
  423.     if (flag==0)
  424.     {
  425.         flag=1;
  426.         int ret=send(m_socket,"1",1,0);
  427.         SendMessage(WM_TRANS,ret,0);
  428.         GetDlgItem(IDC_BUTTON_LED1)->SetWindowText("灭灯1");
  429.     }
  430.     else
  431.     {
  432.         flag=0;
  433.         int ret=send(m_socket,"2",1,0);
  434.         SendMessage(WM_TRANS,ret,0);
  435.         GetDlgItem(IDC_BUTTON_LED1)->SetWindowText("亮灯1");
  436.     }
  437.     

  438. }

  439. void CVc_tcpclientDlg::OnButton3()
  440. {
  441.     // TODO: Add your control notification handler code here
  442.     int ret=send(m_socket,"2",1,0);
  443.     SendMessage(WM_TRANS,ret,0);
  444. }

  445. void CVc_tcpclientDlg::OnButton4()
  446. {
  447.     // TODO: Add your control notification handler code here
  448.     int ret=send(m_socket,"0",1,0);
  449.     SendMessage(WM_TRANS,ret,0);
  450.     
  451. }

  452. void CVc_tcpclientDlg::OnButtonLed1()
  453. {
  454.     // TODO: Add your control notification handler code here
  455.         static int flag=0;
  456.     if (flag==0)
  457.     {
  458.         flag=1;
  459.         int ret=send(m_socket,"1",1,0);
  460.         SendMessage(WM_TRANS,ret,0);
  461.         GetDlgItem(IDC_BUTTON_LED1)->SetWindowText("灭灯1");
  462.     }
  463.     else
  464.     {
  465.         flag=0;
  466.         int ret=send(m_socket,"2",1,0);
  467.         SendMessage(WM_TRANS,ret,0);
  468.         GetDlgItem(IDC_BUTTON_LED1)->SetWindowText("亮灯1");
  469.     }
  470. }

  471. void CVc_tcpclientDlg::OnButtonLed2()
  472. {
  473.     // TODO: Add your control notification handler code here
  474.     static int flag=0;
  475.     if (flag==0)
  476.     {
  477.         flag=1;
  478.         int ret=send(m_socket,"3",1,0);
  479.         SendMessage(WM_TRANS,ret,0);
  480.         GetDlgItem(IDC_BUTTON_LED2)->SetWindowText("灭灯2");
  481.     }
  482.     else
  483.     {
  484.         flag=0;
  485.         int ret=send(m_socket,"4",1,0);
  486.         SendMessage(WM_TRANS,ret,0);
  487.         GetDlgItem(IDC_BUTTON_LED2)->SetWindowText("亮灯2");
  488.     }
  489. }

  490. void CVc_tcpclientDlg::OnButton1()
  491. {
  492.     // TODO: Add your control notification handler code here
  493.     static bool btimer2open=FALSE;
  494.     if (m_bConnected)
  495.     {
  496.         if (btimer2open==FALSE)
  497.         {
  498.             SetTimer(2,11,NULL);
  499.             btimer2open=TRUE;
  500.         }
  501.         
  502.     }
  503.     else
  504.     {
  505.         KillTimer(2);
  506.         btimer2open=FALSE;
  507.     }
  508. }

  509. void CVc_tcpclientDlg::OnButtonAutoConnect()
  510. {
  511.     // TODO: Add your control notification handler code here
  512.     SetTimer(6,10000,NULL);
  513. }




阅读(1692) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~