With the in-depth of computer network, computer network programming has become increasingly important during programming. Many articles have been introduced to use VC for Socket programming methods. However, since it is directly using dynamic connection wsock32.dll, it is more cumbersome. In fact, the VC MFC class library provides a socket class such as CasyncSocket, which is very convenient to implement Socket programming.
Client
Create a Dialog Based project: CSockClient. Design a dialog box, add id_connect (connection), ID_send button, add the listbox control IDC_LISTMSG and Edit control IDC_EDitmsg, and add variables to the CCSockClientDLG class in ClassWizard in ClassWizard.
Control ID TYPE MEMBER IDC_EDITMSG CEDIT M_MSG IDC_LISTMSG CLISTBOX M_MSGS
Add the following code in MySock.ccp:
#include "csockclient.h" #include "csockclientdlg.h"
Add the following code to mysock.h:
Public: BOOL M_BCONNECTED; UINT M_NLENGTH; Char M_SzBuffer [4096];
Heavy load function in mysock.ccp
Mysock :: mysock () {m_nlength = 0; MEMSET (m_szbuffer, 0, sizeof (m_szbuffer)); m_bconnected = false;} mysock :: ~ mysock () {// Close socket IF (m_hsocket! = Invalid_socket) Close ();} void MySock :: OnReceive (int nErrorCode) {m_nLength = Receive (m_szBuffer, sizeof (m_szBuffer), 0); // The following two lines of code to obtain the dialog pointer CCSockClientApp * pApp = (CCSockClientApp *) AfxGetApp ( ); CCSockClientDlg * pDlg = (CCSockClientDlg *) pApp-> m_pMainWnd; pDlg-> m_MSGS.InsertString (0, m_szBuffer); memset (m_szBuffer, 0, sizeof (m_szBuffer)); CAsyncSocket :: OnReceive (nErrorCode);} void MySock :: Onsend (Int NerrorCode) {send (m_szbuffer, m_nlength, 0); m_nlength = 0; MEMSET (m_szbuffer, 0, sizeof (m_szbuffer)); // Continue to discuss a "read" network event, receive Server message asyncselect ( FD_READ); CASYNCSOCKET :: Onsend (NerrorCode);
void MySock :: OnConnect (int nErrorCode) {if (nErrorCode == 0) {m_bConnected = TRUE; CCSockClientApp * pApp = (CCSockClientApp *) AfxGetApp (); CCSockClientDlg * pDlg = (CCSockClientDlg *) pApp-> m_pMainWnd; memcpy (m_szBuffer , "Connected to", 13); strncat (m_szbuffer, pdlg-> m_szserveradr, sizeof (pdlg-> m_szserveradr); PDLG-> m_msgs.insertstring (0, m_szbuffer); asyncselect (fd_read);} // Deliver one " Reading "Network Event, ready to receive CasyncSocket :: OnConnect (NERRORCODE);} Built a dialogue IDD_ADDR to enter IP addresses and port; add two Edit controls: IDC_ADDR, IDC_Port Press the table in ClassWizard to add Caddrdlg class in ClassWizard variable. Control ID Type Member IDC_Addr CString m_Addr IDC_Port Int m_Port CSockClientDlg.ccp the added code: #include "AddrDlg.h" protected: int TryCount; MySock m_clientSocket; UINT m_szPort; public: char m_szServerAdr [256];
Double-click the "Connection" button in the IDD_CSockClient_Dialog dialog box, add the following code:
m_clientSocket.ShutDown (2); m_clientSocket.m_hSocket = INVALID_SOCKET; m_clientSocket.m_bConnected = FALSE; CAddrDlg m_Dlg; m_Dlg.m_Port = 1088; // default port 1088 if (m_Dlg.DoModal () == IDOK && m_Dlg.m_Addr.IsEmpty! ()) {Memcpy (m_szserveradr, m_dlg.m_addr); m_szport = m_dlg.m_port; settimer (1, 1000, null); // establish a timer, try to connect once every 1 second = 0;}
Add Windows Message WM_TIMER Response Function Ontimer
void CCSockClientDlg :: OnTimer (UINT nIDEvent) {if (m_clientSocket.m_hSocket == INVALID_SOCKET) {BOOL bFlag = m_clientSocket.Create (0, SOCK_STREAM, FD_CONNECT); if {AfxMessageBox ( "Socket Error!") (bFlag!); m_clientSocket .Close (); PostQuitMessage (0); return;}} m_clientSocket.Connect (m_szServerAdr, m_szPort); TryCount ; if (TryCount> = 10 || m_clientSocket.m_bConnected) {KillTimer (1); if (TryCount> = 10) AFXMessageBox ("Connect Failed!"); Return;} cdialog :: ONTIMER (Nidevent);} Double-click the "Send" button in the IDD_CSockClient_Dialog dialog box, add the following code:
void CCSockClientDlg :: OnSend () {if (m_clientSocket.m_bConnected) {m_clientSocket.m_nLength = m_MSG.GetWindowText (m_clientSocket.m_szBuffer, sizeof (m_clientSocket.m_szBuffer)); m_clientSocket.AsyncSelect (FD_WRITE); m_MSG.SetWindowText ( ""); }} Double-click the "Close" button in the IDD_CSockClient_Dialog dialog box, add the following code: void ccsockclientdlg :: Onexit () {m_clientsocket.shutdown (2); // Turn Socket enddialog (0);}
Run this project, enter the host name or IP when the connection is connected, and the CasyncSocket class will automatically process.