Design of the host on the wince Platform

Source: Internet
Author: User
Tags htons

I just got a job at the beginning of school. I want to do embedded work on the wince platform. Because I haven't used it before, let's write a scanner and a client to guide us, one is the server. When the server is required to enable the Service, the client can connect to the server. After the connection is successful, the client can easily communicate and chat. It took me a week to complete the process, for your reference, the following knowledge is involved:

VC ++ language (I didn't know it before, but I have been using C. The difference is too big .)

Multithreading (used to create a thread and receive data .)

Socket, (socket creation, connection, receiving, sending, and disconnection are complicated .)

Next we will provide a program for your reference. Because the system was reinstalled some time ago and vs2008 was not available, you can only upload the program.

// Chat_clientdlg.cpp: implementation file // edit by lineter // QQ: 542375845 # include "stdafx. H "# include" chat_client.h "# include" chat_clientdlg.h "DWORD recivethread (pvoid parg); # ifdef _ debug # define new debug_new # endif // cchat_clientdlg dialog box comment :: partition (cwnd * pparent/* = NULL */): cdialog (cchat_clientdlg: IDD, pparent) {m_hicon = afxgetapp ()-> loadicon (idr_mainframe);} void cchat_clientdlg :: dodataex Change (cdataexchange * PDX) {cdialog: dodataexchange (PDX); ddx_control (PDX, idc_edit1, idcedit1);} begin_message_map (cchat_clientdlg, cdialog) # If defined) &&! Defined (bytes) on_wm_size () # endif //} encode (idc_button1, & found: Success) on_bn_clicked (idc_button2, & found: Success) on_bn_clicked (idc_button3, & found:: onbnclickedbutton3) on_en_change (idc_edit2, & Region: onenchangeedit2) on_bn_clicked (idc_button4, & Region: onbnclickedbutton4) end_message_map () // receiving thread DW Ord recivethread (pvoid parg) {// todo: add the control notification handler code cchat_clientdlg * P = (cchat_clientdlg *) parg; cstring cscontent; // The received data int recv_no = 0; char crecvbuf [100]; while (1) {// clear the string memset (crecvbuf, 0, sizeof (crecvbuf )); // receive data recv_no = Recv (p-> sockclient, crecvbuf,); // process received data cscontent. empty (); cscontent. append (cstring) crecvbuf); cscontent. insert (0, cstring ("server:"); cscontent. append (cstring ("\ r \ n"); // call Function p-> disprecive (cscontent); sleep (1000);} return 0;} // display the received content void cchat_clientdlg: disprecive (cstring cscontent) {cstring csallcontent; // obtain the text box content, and add the data to the top to display getdlgitemtext (idc_edit1, csallcontent); setdlgitemtext (idc_edit1, cscontent + csallcontent );} // cchat_clientdlg bool cchat_clientdlg: oninitdialog () {cdialog: oninitdialog (); // set the icon of this dialog box. When the main window of the application is not a dialog box, the framework will automatically // execute this operation seticon (m_hicon, true); // set the big icon seticon (m_hicon, false ); // set the small icon // display the default server ipcstring cscontent; cscontent = ""; cscontent. append (cstring ("192.168.3.73"); setdlgitemtext (idc_edit3, cscontent); Return true; // return true unless focus is set to the control.} # If defined (_ device_resolution_aware) &&! Defined (win32_platform_wfsp) void cchat_clientdlg: onsize (uint/* ntype */, INT/* CX */, INT/* Cy */) {If (afxisdraenabled ()) {DRA: relayoutdialog (afxgetresourcehandle (), this-> m_hwnd, Dra: getdisplaymode ()! = DRA: portrait? Makeintresource (outputs): makeintresource (idd_chat_client_dialog); }}# endifvoid cchat_clientdlg: onenchangeeditip () {// todo: If the control is a RichEdit control, then it will not // send this notification, unless you rewrite the cdialog: oninitdialog () // function and call cricheditctrl (). seteventmask (), // simultaneously calculate the enm_change flag "or" into the mask. // Todo: add the control notification handler code here} void cchat_clientdlg: onenchangeeditport () {// todo: If the control is a RichEdit control, then it will not // send this notification, unless you rewrite the cdialog: oninitdialog () // function and call cricheditctrl (). seteventmask (), // simultaneously calculate the enm_change flag "or" into the mask. // Todo: add the control notification handler code here} // send the content void cchat_clientdlg: onbnclickedbutton1 () {cstring cscontent; char crecvbuf [100]; cstring csallcontent; char callcontent [500]; cscontent = ""; memset (crecvbuf, 0, sizeof (crecvbuf); memset (callcontent, 0, sizeof (callcontent )); // get the input content and send getdlgitemtext (idc_edit2, cscontent); wchartombyte (cscontent, (lpstr) crecvbuf, sizeof (crecvbuf)/sizeof (crecvbuf [0]); send (sockclient, crecvbuf, strl En (crecvbuf) +); // after processing the input content, cscontent is displayed. insert (0, cstring ("client:"); cscontent. append (cstring ("\ r \ n"); disprecive (cscontent);} // close the connection void cchat_clientdlg: onbnclickedbutton2 () {// todo: add the control notification handler code DWORD exitcode here; // thread exit code // get the thread exit code getexitcodethread (handle1, & exitcode ); // end the thread terminatethread (handle1, exitcode) through the thread exit code; // close the thread handle closehandle (handle1); handle1 = NULL; // close socketclosesocket (sockclient); WSA Cleanup (); // display the disconnection information cstring cscancel; cscancel. append (cstring ("disconnected! "); Setdlgitemtext (idc_edit2, cscancel); // modify the control property getdlgitem (idc_button1)-> enablewindow (false); getdlgitem (idc_button2)-> enablewindow (false ); getdlgitem (idc_button4)-> enablewindow (false); getdlgitem (idc_button3)-> enablewindow (true);} // establish a connection void handle: onbnclickedbutton3 () {// todo: add the control notification handler code cstring cscontent here; // the content in the input box, that is, the IP address (cstring type) Char CIP [100]; // the content in the input box, that is, the IP address (char type) cscontent. empty (); // Clear the original content memset (CIP, 0, sizeof (CIP); // clear the string // get the content in the input box and convert it to Char-type getdlgitemtext (idc_edit3, cscontent); wchartombyte (cscontent, (lpstr) CIP, sizeof (CIP)/sizeof (CIP [0]); // create a socket, client end wversionrequested = makeword (1, 1); err = wsastartup (wversionrequested, & wsadata); If (Err! = 0) {afxmessagebox (cstring ("wsastartup error"), 0, 0); return;} If (lobyte (wsadata. wversion )! = 1 | hibyte (wsadata. wversion )! = 1) {afxmessagebox (cstring ("lobyte error"), 0, 0); wsacleanup (); afxmessagebox (cstring ("lobyte OK error"), 0, 0); return ;} sockclient = socket (af_inet, sock_stream, 0); // client socket // connect sockaddr_in addrsrv to the server through the IP address and port; addrsrv. sin_addr.s_un.s_addr = inet_addr (CIP); // addrsrv. sin_addr.s_un.s_addr = inet_addr ("192.168.3.73"); addrsrv. sin_family = af_inet; addrsrv. sin_port = htons (1000); Connect (sockclient, (sockaddr *) & addrs RV, sizeof (sockaddr); // display the connection success information cstring csok; csok. append (cstring ("connected! "); Setdlgitemtext (idc_edit2, csok); // modify the control property getdlgitem (idc_button1)-> enablewindow (true); getdlgitem (idc_button2)-> enablewindow (true ); getdlgitem (idc_button4)-> enablewindow (true); getdlgitem (idc_button3)-> enablewindow (false); // create the receiving data process handle1 = createthread (null, 0, recivethread, this, 0, null);} void cchat_clientdlg: onenchangeedit2 () {// todo: If the control is a RichEdit control, it will not // send this notification unless cdidit is rewritten :: oninitdialog () // Function and call cricheditctrl (). seteventmask (), // calculate the enm_change flag "or" into the mask at the same time. // Todo: add control notification handler code here} // cstring to Char function bool cchat_clientdlg: wchartombyte (lpcwstr lpcwszstr, lpstr lpszstr, DWORD dwsize) {DWORD dwminsize; dwminsize = widechartomultibyte (cp_oemcp, null, lpcwszstr,-1, null, 0, null, false); If (dwsize + 1 <dwminsize) {return false;} widechartomultibyte (cp_oemcp, null, lpcwszstr,-1, lpszstr, dwsize, null, false); Return true;} // The function for receiving keys. Warning: after pressing it, it will become stuck to receive the data void cchat_clientdlg: onbnclickedbutton4 () {// todo: add the control notification handler code cstring cscontent; cstring csallcontent; int recv_no = 0; char crecvbuf [100]; // receives data through socket, blocking recv_no = Recv (sockclient, crecvbuf, 0); // puts the received data into the cscontent at the top of the input box after processing the data. empty (); cscontent. append (cstring) crecvbuf); cscontent. insert (0, cstring ("server:"); cscontent. append (cstring ("\ r \ n"); disprecive (cscontent );}

There are two text editing boxes and four buttons. The corresponding header file is also changed as follows:

// Chat_clientdlg.h: header file // # pragma once # include <winsock2.h> # pragma comment (Lib, "ws2.lib") # include <stdio. h> # include "afxwin. H "// cchat_clientdlg dialog box class cchat_clientdlg: Public cdialog {// construct public: cchat_clientdlg (cwnd * pparent = NULL ); // standard constructor // data in the dialog box Enum {IDD = idd_chat_client_dialog}; Word wversionrequested; wsadata; int err; socket sockclient, socksrv; bool wchartombyte R, lpstr lpszstr, DWORD dwsize); DWORD winapi revthread (lpvoid lpparameter); void disprecive (cstring cscontent); // display the received data handle handle1; // Process Handle protected: Virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV support // protected: hicon m_hicon; // The generated message ing function virtual bool oninitdialog (); # If defined (_ device_resolution_aware )&&! Defined (win32_platform_wfsp) afx_msg void onsize (uint/* ntype */, INT/* CX */, INT/* Cy */); # endifdeclare_message_map () public: afx_msg void onenchangeedit1 (); afx_msg void onenchangeeditip (); afx_msg void onenchangeeditport (); afx_msg void forward (); afx_msg void onenchangeedit2 (); afx_msg void onbnclickedbutton4 (); cedit idcedit1 ;};

The above is the client, and there is a server, which is similar to the client, but there are also some differences.

// Chat_serverdlg.cpp: implementation file // # include "stdafx. H "# include" chat_server.h "# include" chat_serverdlg.h "# ifdef _ debug # define new debug_new # endifdword recivethread (pvoid parg); // convert cstring to Char function bool cchat_serverdlg :: wchartombyte (maid, DWORD dwsize) {DWORD dwminsize; dwminsize = bytes (cp_oemcp, null, lpcwszstr,-1, null, 0, null, false ); if (dwsize + 1 <dwminsize) {return Fal Se;} widechartomultibyte (cp_oemcp, null, lpcwszstr,-1, lpszstr, dwsize, null, false); Return true;} // The receiving thread DWORD recivethread (pvoid parg) {// todo: add the control notification handler code cchat_serverdlg * P = (cchat_serverdlg *) parg; int recv_no = 0; char crecvbuf [100]; cstring cscontent; sockaddr_in addrclient; int Len = sizeof (sockaddr); memset (crecvbuf, 0, sizeof (crecvbuf); P-> sockconn = accept (p-> socksrv, (sockaddr *) & addrclient, & Len); P-> dispinfo (Cstring ("one connected. "); P-> enablesend (true); While (1) {Recv (p-> sockconn, crecvbuf, 100,0); cscontent. empty (); cscontent. append (cstring) crecvbuf); cscontent. insert (0, cstring ("server:"); cscontent. append (cstring ("\ r \ n"); If (crecvbuf [0]! = 0) {P-> disprecive (cscontent);} else {P-> enablesend (false); closesocket (p-> sockconn ); p-> dispinfo (cstring ("One disconnected. "); P-> sockconn = accept (p-> socksrv, (sockaddr *) & addrclient, & Len); P-> dispinfo (cstring (" one connected. "); P-> enablesend (true) ;}} return 0 ;}// whether to enable the sending box void cchat_serverdlg: enablesend (bool enable_send) {If (enable_send) {getdlgitem (idc_button1)-> enablewindow (true);} else {getdlgitem (idc_butto N1)-> enablewindow (false) ;}// display the data above the text box void cchat_serverdlg: disprecive (cstring cscontent) {cstring csallcontent; getdlgitemtext (idc_edit1, csallcontent ); setdlgitemtext (idc_edit1, cscontent + csallcontent);} // display the message void cchat_serverdlg: dispinfo (cstring cscontent) {setdlgitemtext (idc_edit2, cscontent);} cchat_serverdlg :: cchat_serverdlg (cwnd * pparent/* = NULL */): cdialog (cchat_serverdlg: IDD, pparent) {M _ Hicon = afxgetapp ()-> loadicon (idr_mainframe);} void cchat_serverdlg: dodataexchange (cdataexchange * PDX) {cdialog: dodataexchange (PDX);} convert (convert, cdialog) # If defined (_ device_resolution_aware )&&! Defined (identifier) on_wm_size () # endif //} encode (idc_button3, & cchat_serverdlg: identifier) iterate (idc_button2, & cchat_serverdlg: identifier) iterate (idc_button1, & Signature:: onbnclickedbutton1) end_message_map () // cchat_serverdlg message processing program bool cchat_serverdlg: oninitdialog () {cdialog: oninitdialog (); // set the icon of this dialog box. When the main window of the application is not a dialog box, the framework will automatically // execute this operation seticon (m_hicon, true); // set the big icon seticon (m_hicon, false ); // set the small icon // todo: add the extra initialization code return true here; // return true unless the focus is set to the control.} # If defined (_ device_resolution_aware )&&! Defined (win32_platform_wfsp) void cchat_serverdlg: onsize (uint/* ntype */, INT/* CX */, INT/* Cy */) {If (afxisdraenabled ()) {DRA: relayoutdialog (afxgetresourcehandle (), this-> m_hwnd, Dra: getdisplaymode ()! = DRA: portrait? Makeintresource (outputs): makeintresource (outputs) ;}# endifvoid cchat_serverdlg: onbnclickedbutton3 () {// todo: add the control notification handler code word wversionrequested; wsadata; int err; wversionrequested = makeword (1, 1); err = wsastartup (wversionrequested, & wsadata); If (Err! = 0) {return;} If (lobyte (wsadata. wversion )! = 1 | hibyte (wsadata. wversion )! = 1) {wsacleanup (); return;} socksrv = socket (af_inet, sock_stream, 0); sockaddr_in addrsrv; addrsrv. sin_addr.s_un.s_addr = htonl (inaddr_any); addrsrv. sin_family = af_inet; addrsrv. sin_port = htons (1000); BIND (socksrv, (sockaddr *) & addrsrv, sizeof (sockaddr); Listen (socksrv, 5); handle1 = createthread (null, 0, recivethread, this, 0, null); getdlgitem (idc_button1)-> enablewindow (false); getdlgitem (idc_button2)-> enablewindow (true ); Getdlgitem (idc_button3)-> enablewindow (false); dispinfo (cstring ("Server start! ");} Void cchat_serverdlg: onbnclickedbutton2 () {// todo: add the control notification handler code DWORD exitcode here; // thread exit code // get the thread exit code getexitcodethread (handle1, & exitcode); // use the thread exit code to end the thread terminatethread (handle1, exitcode ); // close the thread handle closehandle (handle1); handle1 = NULL; // close socketclosesocket (socksrv); wsacleanup (); // change the control property getdlgitem (idc_button1) -> enablewindow (false); getdlgitem (idc_button2)-> enablewindow (false); getdlgitem (idc_button3 )-> Enablewindow (true); // display dispinfo (cstring ("server closed! ");} Void cchat_serverdlg: onbnclickedbutton1 () {cstring cscontent; char crecvbuf [100]; cstring csallcontent; char callcontent [500];
// Chat_serverdlg.h: header file // # pragma once # include <winsock2.h> # pragma comment (Lib, "ws2.lib") # include <stdio. h> # include "afxwin. H "// cchat_serverdlg dialog box class cchat_serverdlg: Public cdialog {// construct public: cchat_serverdlg (cwnd * pparent = NULL ); // standard constructor // data in the dialog box Enum {IDD = idd_chat_server_dialog }; /* ++ ++ */socket socksrv; socket sockconn; voi D disprecive (cstring cscontent); // display the received data handle handle1; // Process Handle bool wchartombyte (lpcwstr lpcwszstr, lpstr lpszstr, DWORD dwsize); void cchat_serverdlg :: dispinfo (cstring cscontent); // display information void enablesend (bool enable_send ); // enable the sending key/* ++ ++ */ protected: virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV support // protected: hicon m_hic On; // The generated message ing function virtual bool oninitdialog (); # If defined (_ device_resolution_aware )&&! Defined (win32_platform_wfsp) afx_msg void onsize (uint/* ntype */, INT/* CX */, INT/* Cy */); # endifdeclare_message_map () public: afx_msg void onbnclickedbutton3 (); afx_msg void onbnclickedbutton2 (); afx_msg void onbnclickedbutton1 ();};

Int Len = sizeof (sockaddr); cscontent = ""; memset (crecvbuf, 0, sizeof (crecvbuf); memset (callcontent, 0, sizeof (callcontent )); // get the input content and send getdlgitemtext (idc_edit2, cscontent); wchartombyte (cscontent, (lpstr) crecvbuf, sizeof (crecvbuf)/sizeof (crecvbuf [0]); send (sockconn, crecvbuf, strlen (crecvbuf) + 1, 0 );//
After processing the input content, cscontent is displayed. insert (0, cstring ("client:"); cscontent. append (cstring ("\ r \ n"); disprecive (cscontent );}

The above is all the code. If you need the source file, you can leave a message and tell me your mailbox. I will send it to you as soon as possible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.