Windows socket entry routine
The content of Test.cpp is passed here, and the key is to explain the acquisition of the IP address.
// TestDlg.cpp: importation file //
#include "stdafx.h" #include "test.h" #include "testdlg.h" #include
#ifdef _debug # define new debug_new # undef this_filestatic char this_file [] = __file __; # ENDIF
/// Caboutdlg Dialog Used for App About
Class Caboutdlg: public cdialog {public: capoutdlg ();
// 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
// ImplementationProtace: // {{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_MAPEND_MESSAGE_MAP ()
/// ctestdlg dialog
Ctestdlg :: ctestdlg (cwnd * pparent / * = null * /): cdialog (ctestdlg :: IDd, pparent) {/ {{AFX_DATA_INIT (CTESTDLG) m_INFO = _t (""); m_output = _t (""); m_input = _t (""); m_connect = _t ("); m_ipaddress = _t ("); m_port = 0; m_status = -1; //}} AFX_DATA_INIT // Note That Loadicon Does Not Require A Subsequent Destroyicon in Win32 m_hicon = AFXGetApp () -> loadicon (idR_mainframe);
void CTestDlg :: DoDataExchange (CDataExchange * pDX) {CDialog :: DoDataExchange (pDX); // {{AFX_DATA_MAP (CTestDlg) DDX_Text (pDX, IDC_OUTPUTEDIT, m_Output); DDX_Text (pDX, IDC_INPUTEDIT, m_Input); DDX_Text (pDX, IDC_CONNECTEDIT , m_Connect); DDX_Text (pDX, IDC_IPADDRESS, m_IPAddress); DDV_MaxChars (pDX, m_IPAddress, 15); DDX_Text (pDX, IDC_PORT, m_Port); DDX_Radio (pDX, IDC_SERVERRADIO, m_Status); //}} AFX_DATA_MAP} BEGIN_MESSAGE_MAP (CTestDlg, CDialog) // {{AFX_MSG_MAP (CTestDlg) ON_WM_SYSCOMMAND () ON_WM_PAINT () ON_WM_QUERYDRAGICON () ON_BN_CLICKED (IDC_CONNECTBUTTON, OnConnect) ON_BN_CLICKED (IDC_DISCONNECTBUTTON, OnDisconnect) ON_BN_CLICKED (IDC_SENDBUTTON, OnSend) ON_BN_CLICKED (IDC_SERVERRADIO, OnServerradio) ON_BN_CLICKED (IDC_CLIENTRADIO, OnClientradio) ON_BN_CLICKED (Idc_sendclearbutton, onsendclear) ON_BN_CLICKED (IDC_ReceiveClearButton, onreceiveClear) //}} AFX_MSG_MAPEND_MESSAGE_MAP ()
/// ctestdlg Message Handlers
Bool ctestdlg :: oninitdialog () {cdialog :: oninitdialog ();
// Add "About ..." Menu Item to System Menu.
// IDM_ABOUTBOX MUST BE IN THE SYSTEM Command Range. Assert ((idm_aboutbox & 0xff0) == idm_aboutbox; assert (idm_aboutbox <0xf000);
CMenu * pSysMenu = GetSystemMenu (FALSE); if (pSysMenu = NULL!) {CString strAboutMenu; strAboutMenu.LoadString (IDS_ABOUTBOX); if {pSysMenu-> AppendMenu (MF_SEPARATOR) (strAboutMenu.IsEmpty ()!); PSysMenu-> AppendMenu ( MF_String, IDM_AboutBox, Straboutmenu);}}
// set the icon for this dialog. The framework does this automatic or //hen the application's main window is not a dialog seticon (m_hicon, true); // set big icon seticon (m_hicon, false); // set small icon m_status = -1; m_ServerSocket = NULL; m_ClientSocket = NULL; m_arIn = NULL; m_arOut = NULL; m_file = NULL; m_Connect = ""; m_IPAddress = "127.0.0.1"; m_Port = 5000; GetDlgItem (IDC_IPADDRESS) -> EnableWindow (FALSE ); GetDlgItem (IDC_PORT) -> EnableWindow (FALSE); UpdateData (FALSE); return TRUE; // return TRUE unless you set the focus to a control} void CTestDlg :: OnSysCommand (UINT nID, LPARAM lParam) {if (( NID & 0xFFF0) == idm_aboutbox) {Caboutdlg Dlgabout; dlgabout.domodal ();} else {cdialog :: oversysCommand (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 automaticly done for you by the framework.
Void ctestdlg :: 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 ctestdlg :: ONQUERYDRAGICON ()} (hcursor) m_hicon;}
Void ctestdlg :: onConnect () {cstring msg; updatedata (true); if (m_status == 0) // server {if (m_serversocket! = null) {m_connect = "please disconnect!"; Updatedata (false);} else {m_Connect = "Waiting for Client ..."; UpdateData (FALSE); (! AfxSocketInit ()) if {MessageBox ( "! WindowsSocket initial failed", "Send", MB_ICONSTOP); return;} m_ServerSocket = new CNewSocket; m_ServerSocket -> m_status = m_status; m_serversocket-> getdlg (this); if (! m_serversocket-> create (M_Port)) MessageBox ("SendSocket Create Failed!", "Send", MB_ICONSTOP); Else {m_serversocket-> listen (); }}} Else {if (m_status == 1) {IF (m_clientsocket! = Null) {m_connect = "please disconnect!"; Updatedata (false);} else {m_connect = "connect to the server ..."; Updatedata (False); if (! AFXSocketinit ()) {MessageBox ("WindowsSocket Initial Failed!" , "Receive", MB_ICONSTOP); return;} m_ClientSocket = new CNewSocket; m_ClientSocket-> GetDlg (this); m_ClientSocket-> m_Status = m_Status; if (m_ClientSocket-> Create (!)) {MessageBox ( "ReceiveSocket create failed!" , "Receive", MB_ICONSTOP); return;} else {if) {CString str = m_Connect (m_ClientSocket-> Connect (m_IPAddress, m_Port!); SocketReset (); m_Connect = str; m_Connect = "Error!"; UpdateData (FALSE );
} Else {m_Connect = "OK!"; M_file = new CSocketFile (m_ClientSocket); m_arIn = new CArchive (m_file, CArchive :: load); m_arOut = new CArchive (m_file, CArchive :: store);} UpdateData (FALSE); }}}}} {Msg = "please choose the status!"; AfxMessageBox (MSG);}} void ctestdlg :: Onsend () {if (m_arout) {if (m_status == 0) {Updatedata (TRUE); * m_arout << m_output; m_arout-> flush ();} else {updata (true); * m_arout << m_output; m_arout-> flush ();}} else afxMessageBox ("not connect!" }
Void ctestdlg :: onaccept () {structure sockaddr_in addr; cstring ss; int address; m_connect = "ok!"; updatedata (false); m_clientsocket = new cnewsocket; m_clientsocket-> getdlg (this);
m_serversocket-> accept (* m_clientsocket, (struct socmeddr *) & addr, & addrlen;
/ / Must join "& address" to get the IP address correctly
SS.Format ("% s", :: inet_ntoa (addr.sin_addr)); // inet_ntoa (addr.sin_addr); getdlgitem (idc_edit3) -> setWindowText (SS);
m_ClientSocket-> m_Status = m_ServerSocket-> m_Status; m_file = new CSocketFile (m_ClientSocket); m_arIn = new CArchive (m_file, CArchive :: load); m_arOut = new CArchive (m_file, CArchive :: store);}
Void ctestdlg :: OnRecEive () {* m_arin >> m_input; updatedata (false);
Void ctestdlg :: onDisconnect () {if (m_arout! = null) {socketReset (); m_connect = "disconnected!"; Updatedata (false);}}
void CTestDlg :: OnClose () {if (m_ClientSocket-> m_Status == 0) m_Connect = "Client"; else m_Connect = "Server"; m_Connect = "has disconnected!"; UpdateData (FALSE);} void CTestDlg :: SocketReset () {If (m_arin! = Null) {delete m_arin; m_arin = null;} if (m_arout! = Null) {delete m_arout; m_arout = null;} if (m_file! = Null) {delete m_file; m_file = NULL; } if (m_ClientSocket = NULL!) {delete m_ClientSocket; m_ClientSocket = NULL;} if (! m_ServerSocket = NULL) {delete m_ServerSocket; m_ServerSocket = NULL;} m_Connect = ""; UpdateData (FALSE);}
Void ctestdlg :: ONSERVERRADIO () {Updatedata (TRUE); getdlgitem (idc_ipaddress) -> enableWindow (false); getdlgitem (idc_port) -> enableWindow (true); Updatedata
Void ctestdlg :: OnClientradio () {Updatedata (TRUE); getdlgitem (idc_ipaddress) -> enableWindow (TRUE); getdlgitem (IDC_Port) -> enableWindow (true); Updatedata (false);}
Void ctestdlg :: OnsendClear () {m_output = ""; Updatedata (false);
Void ctestdlg :: onreceiveclear () {m_input = ""; Updatedata (false);