This example for you to share the simple C + + QQ program server-side implementation code for your reference, the specific content as follows
ServerDlg.cpp:implementation File//#include "StdAfx.h" #include "Server.h" #include "ServerDlg.h" #ifdef _DEBUG #d
Efine new debug_new #undef this_file static char this_file[] = __file__; #endif #define WM_SHOWTASK wm_user+1 #define WM_SERVER_ACCEPT wm_user+2///////////////////////////////////////////// CAboutDlg dialog used for App about class Caboutdlg:public CDialog {Public:cabou
Tdlg ();
Dialog Data//{{afx_data (CAboutDlg) enum {IDD = Idd_aboutbox}; }}afx_data//ClassWizard generated virtual function overrides//{{afx_virtual (CABOUTDLG) protected:virtual Voi D DoDataExchange (cdataexchange* PDX); DDX/DDV Support//}}afx_virtual//Implementation protected://{{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::D odataexchange (PDX);
{{Afx_data_map (CAboutDlg)//}}afx_data_map} begin_message_map (CAboutDlg, CDialog)//{{afx_msg_map (CABOUTDLG) No message handlers//}}afx_msg_map end_message_map ()/////////////////////////////////////////////////////////// Cserverdlg dialog Cserverdlg::cserverdlg (cwnd* pparent/*=null*/): CDialog (Cserverdlg::idd, PPa
Rent) {//{{afx_data_init (cserverdlg) m_strshow = _t (""); }}afx_data_init//Note This LoadIcon does not require a subsequent destroyicon in Win32 M_hicon = AfxGetApp ()->l
Oadicon (IDR_MAINFRAME);
m_people = 0;
} void Cserverdlg::D odataexchange (cdataexchange* pDX) {CDialog::D odataexchange (PDX);
{{Afx_data_map (Cserverdlg) DDX_Control (PDX, Idc_static_people, m_strpeople);
DDX_Control (PDX, Idc_static_host, m_strhost);
DDX_Text (PDX, Idc_edit_show, m_strshow); }}afx_data_map} begin_message_map (Cserverdlg, CDialog)//{{afx_msg_map (CSERVERDLG) On_wm_syscommand () ON_WM_PAI NT () On_wm_quErydragicon () on_message (wm_showtask,onshowtask) on_message (wm_server_accept,onserveraccept) ON_WM_DESTROY () ON_CO Mmand (id_exit,ondlgexit) on_command (id_show,ondlgshow) on_command id_hide,ondlghide//}}AFX_MSG_MAP (ON_STN_CLICKE) D (Idc_static_host, &cserverdlg::onstnclickedstatichost) end_message_map ()/////////////////////////////////// CSERVERDLG message handlers BOOL Cserverdlg::oninitdialog () {Cdialog::o
Ninitdialog ();
Add "About ..." menu item to System menu.
Idm_aboutbox must is in the System command range.
ASSERT ((Idm_aboutbox & 0xfff0) = = Idm_aboutbox);
ASSERT (Idm_aboutbox < 0xf000);
cmenu* Psysmenu = GetSystemMenu (FALSE);
if (Psysmenu!= NULL) {CString straboutmenu;
Straboutmenu.loadstring (Ids_aboutbox);
if (!straboutmenu.isempty ()) {Psysmenu->appendmenu (mf_separator);
Psysmenu->appendmenu (mf_string, Idm_aboutbox, Straboutmenu); }}//SetThe icon for this dialog. The framework does this automatically//when the application ' s main window isn't not a dialog SetIcon (M_hicon, TRUE); Set big icon SetIcon (M_hicon, FALSE);
Set Small icon initnotify (); SetWindowText ("server");//Display caption above dialog//1 load Socket library initsocket (); 2 initialization of the socket library return TRUE; Return TRUE unless your set the focus to a control} VOID cserverdlg::gethostinfor () {char szhostname[128]; Get SHINE-PC//Storage host name if (GetHostName (szhostname,128) ==0) The right situation {hostent* phost; Host Content phost = gethostbyname (szhostname); Nic Information//phost returns a list pointing to the host lpcstr Psz = Inet_ntoa (* (struct in_addr *) phost->h_addr_list[0])
//Get the PSZ variable CString strtemp pointing to IP;
strtemp + = "host name:";
strtemp + = Szhostname;
strtemp + = "";
strtemp+= "IP address:";
Strtemp+=psz;
M_strhost.setwindowtext (strtemp);
CString strpeople; StrPeople.format ("%d", m_people);
M_strpeople.setwindowtext (strpeople);
} VOID Cserverdlg::initsocket () {wsadata wsadata;
WSAStartup (Makeword (2,2), &wsadata);
Gethostinfor ();
Create a listening socket M_slisten = socket (AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (m_slisten==invalid_socket) {MessageBox ("Create SOCKET Failed");
WSACleanup ();
Return
}//Initialize NIC//sockaddr_in (2.2) sockaddr (1.0) m_serveraddr.sin_family = af_inet;
M_SERVERADDR.SIN_ADDR.S_ADDR = Inaddr_any;
M_serveraddr.sin_port = htons (9527); 1.0//Bind if (Bind (M_slisten, (sockaddr*) &m_serveraddr,sizeof (m_serveraddr)) ==socket_error) {MessageBox
("Bind Failed");
WSACleanup ();
Closesocket (M_slisten);
Return
}//Monitor if (Listen (m_slisten,20) ==socket_error) {MessageBox ("Listen Failed");
WSACleanup ();
Closesocket (M_slisten);
Return } wsaasyncselect (M_slisten,m_hwnd,wm_server_accept, fd_accept| fd_read|
Fd_close); for (int i=0;i<max;i++) {M_sclient[i] = Invalid_socket;
} m_strshow= "Now server are working!\r\nwaiting for client's connecting ...";
UpdateData (FALSE);
Return } void Cserverdlg::onsyscommand (UINT NID, LPARAM LPARAM) {if (NID & 0xfff0) = = Idm_aboutbox) {CAboutDlg D
Lgabout;
Dlgabout.domodal ();
else {Cdialog::onsyscommand (NID, LParam); }//If You add a Minimize button to your dialog, you'll need the code below/to draw the icon.
For MFC applications using the Document/view model,//The the ' is automatically do for you by the framework. void Cserverdlg::onpaint () {if (Isiconic ()) {CPAINTDC DC (this);//device context for painting SENDMESSAG E (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 Cserverdlg::onquerydragicon () {return (hcursor) M_hicon;}
VOID cserverdlg::initnotify () {m_nid.cbsize = sizeof (NOTIFYICONDATA);
M_nid.hwnd = m_hwnd;
M_nid.uid = idi_icon_notify; M_nid.uflags = nif_icon| nif_message|
Nif_tip; M_nid.ucallbackmessage = Wm_showtask;
Custom messages handle mouse actions on pallets M_nid.hicon = LoadIcon (AfxGetInstanceHandle (), Makeintresource (idi_icon_notify)); strcpy (M_nid.sztip, "point Me")/////When the mouse is placed above, the displayed content Shell_NotifyIcon (Nim_add,&m_nid);//Add icon in tray area lresult cserverdlg::o Nshowtask (WPARAM wparam,lparam LPARAM) {//wparam receives the ID of the icon, and LPARAM receives the mouse behavior if (wparam!=idi_icon_notify) {retur
n 1;
Switch (lParam) {case wm_rbuttonup: {CMenu Menu;
Menu.createpopupmenu (); Menu.appendmenu (MF_string, Id_show, "show");
Menu.appendmenu (mf_string, Id_hide, "hidden");
Menu.appendmenu (mf_string, Id_exit, "exit");
CPoint Pt; GetCursorPos (&PT);
Get mouse position Menu.trackpopupmenu (Tpm_rightbutton, Pt.x, Pt.y, this); Menu.destroymenu ();
Destruction of resource break;
} return 0;
} void Cserverdlg::ondestroy () {Cdialog::ondestroy ();
Shell_NotifyIcon (Nim_delete,&m_nid);
} void Cserverdlg::ondlgexit () {SendMessage (wm_close,null,null);}
void Cserverdlg::ondlghide () {ShowWindow (sw_hide);}
void Cserverdlg::ondlgshow () {:: ShowWindow (This->m_hwnd,sw_normal);} Fd_accept fd_close fd_read lresult cserverdlg::onserveraccept (WPARAM wparam,lparam LPARAM) {//wsagetselectevent =
= LoWord (lParam) int iEvent = Wsagetselectevent (LParam); Call the Winsock API function to get the network event type switch (iEvent) {Case FD_ACCEPT://Client connection request event {onaccept ();
The user's online request Connect break; Case fd_close://Client Disconnect event: {OnCLose (WParam);
WParam who was shut off the break; Case fd_read://Network Packet arrival event {onrecive (WParam);
Send break;
} Default:break;
return 0;
} VOID cserverdlg::onrecive (WPARAM WPARAM) {sendinfor Data = {0};
int i = 0;
for (i=0;i<max;i++) {if (M_sclient[i]==wparam) {break;
} if (I==max) {return;
int iRet = recv (M_sclient[i], (char*) &data,sizeof (Data), 0); 0 Guan Yu//message type user name information content//Analysis packet switch (data.itype) {case 20:case 6: {//6 11 The other party has accepted the request to establish the connection ...
.. int j = 0; for (j=0;j<max;j++) {if (m_sclient[j]!=invalid_socket&& strcmp M_peopleinfor[j].strnam
E.lockbuffer (), data.szname) ==0) {Send (M_sclient[j), (char*) &data,sizeof (Data), 0);
}} break;
Case 7: {int j = 0; for (j=0;j<max;j++) {if (m_sclient[j]!=invalid_socket&& strcmp(M_peopleinfor[j].strname.lockbuffer (), data.szname) ==0) {Send (M_sclient[j), (char*) &data,sizeof (Data
), 0);
}} break;
Case 5://11-12 {//11--->12//5 11|1.txt|5| 5 192.168.0.100|11|1.txt|5|
--->12 CString strtemp;
Strtemp+=m_peopleinfor[i].strip;
strtemp+= "|";
strtemp+=data.szmsg;
strcpy (Data.szmsg,strtemp.lockbuffer ());
int j = 0; for (j=0;j<max;j++) {if (m_sclient[j]!=invalid_socket&& strcmp M_peopleinfor[j].strnam
E.lockbuffer (), data.szname) ==0) {Send (M_sclient[j), (char*) &data,sizeof (Data), 0);
}} break;
Case: {int j = 0; for (j=0;j<max;j++) {if (m_sclient[j]!=invalid_socket&& strcmp M_peopleinfor[j].strnam E.lockbuffer (), data.szname) ==0) {Send (M_sclient[j), (char*) &datA,sizeof (Data), 0);
}} break;
Case: {int j = 0; for (j=0;j<max;j++) {if (m_sclient[j]!=invalid_socket&&j!=i) {Send (m_sclient[
J], (char*) &data,sizeof (Data), 0);
}} break;
Case 2: {int j = 0; for (j=0;j<max;j++) {if (strcmp (m_peopleinfor[j].strname,data.szname) ==0 &&m_sclient[j
]!=invalid_socket) {Send (M_sclient[j], (char*) &data,sizeof (Data), 0);
Break
}} break;
Case 1: {int j = 0; for (j=0;j<max;j++) {if (m_sclient[j]!=invalid_socket&&j!=i) {Send (m_sclient[
J], (char*) &data,sizeof (Data), 0);
}} break;
Case 0://new people {m_peopleinfor[i].strname = Data.szname;
Add data to the control Onlineoroutline (i, "on-line"); Refresh user list//1 to the old notice to the new user (himself get himself)
int j = 0;
for (j=0;j<max;j++) {if (m_sclient[j]!=invalid_socket) {CString Temp;
Data.itype = 0;
strcpy (Data.szname,m_peopleinfor[i].strname);
Temp + = "\ r \ n";
GetCurrentTime ();
Temp+=m_timer.lockbuffer ();
temp+= "System message: \ r \ n";
Temp+=m_peopleinfor[i].strname;
temp+= "into the chat room";
strcpy (Data.szmsg,temp.lockbuffer ());
Send (M_sclient[j], (char*) &data,sizeof (Data), 0);
}//To the new notice the old user J = 0; for (j=0;j<max;j++) {if (M_sclient[j]!=invalid_socket && j!=i) {Data.itype =
0;
memset (data.szmsg,0,400);
strcpy (Data.szname,m_peopleinfor[j].strname);
Send (M_sclient[i], (char*) &data,sizeof (Data), 0);
}} break;
}} VOID cserverdlg::onaccept () {int i = 0; for (i=0;i<max;i++) {if (M_sclient[i]==invalid_sockET) {break;
} if (I==max) {return;
} sockaddr_in clientaddr;
int nlen = sizeof (sockaddr_in);
M_sclient[i] = Accept (M_slisten, (sockaddr*) &clientaddr,&nlen);
LPCSTR StrIP = Inet_ntoa (* (struct in_addr*) &clientaddr.sin_addr);
M_peopleinfor[i].strip = strIp;
m_people++;
CString strpeople;
Strpeople.format ("%d", m_people);
M_strpeople.setwindowtext (strpeople);
UpdateData (FALSE);
} VOID Cserverdlg::getcurrenttime () {SYSTEMTIME St = {0};
Getlocaltime (&ST);
int j = 0;
j = sprintf (M_timer.lockbuffer (), "%d Years", st.wyear);
J + + sprintf (M_timer.lockbuffer () +j, "%d months", st.wmonth);
J + + sprintf (M_timer.lockbuffer () +j, "%d Days", st.wday);
J + + sprintf (M_timer.lockbuffer () +j, "%d", st.whour);
if (st.wminute<10) {int i = 0;
J + + sprintf (M_timer.lockbuffer () +j, "%d", I);
J + + sprintf (M_timer.lockbuffer () +j, "%d minutes", st.wminute);
if (st.wsecond<10) {int i = 0; J + + sprintf (M_timer.lockbuffer () +j, "%d", i);
J + + sprintf (M_timer.lockbuffer () +j, "%d seconds", st.wsecond);
} VOID cserverdlg::onlineoroutline (int iindex,cstring yesorno) {CString strtemp;
GetCurrentTime ();
Strtemp+=m_timer.lockbuffer ();
strtemp+= ';
strtemp+= "User:";
strtemp+= M_peopleinfor[iindex].strname;
strtemp+= "";
Strtemp+=m_peopleinfor[iindex].strip;
strtemp+= Yesorno;
M_strshow + = "\ r \ n";
M_strshow + = strtemp;
UpdateData (FALSE);
} VOID Cserverdlg::onclose (WPARAM WPARAM) {int i = 0;
for (i=0;i<max;i++) {if (M_sclient[i]==wparam) {break;
} if (I==max) {return;
} onlineoroutline (I, "off-line");
m_people--;
CString strpeople;
Strpeople.format ("%d", m_people);
M_strpeople.setwindowtext (strpeople);
Sendinfor Data;
int j = 0;
for (j=0;j<max;j++) {if (m_sclient[j]!=invalid_socket&&j!=i) {data.itype =-1;
CString STRMSG;
GetCurrentTime ();
Strmsg+=m_timer.lockbuffer (); Strmsg+=m_peopleinfor[i].sTrname;
strmsg+= "quit the chat room";
strcpy (Data.szname,m_peopleinfor[i].strname.lockbuffer ());
strcpy (Data.szmsg,strmsg.lockbuffer ());
Send (M_sclient[j], (char*) &data,sizeof (Data), 0);
}//Empty out the user's resources closesocket (M_sclient[i]);
M_sclient[i] = Invalid_socket;
M_peopleinfor[i].strip = "";
M_peopleinfor[i].strname = "";} void Cserverdlg::onstnclickedstatichost () {//TODO: Add control notification handler code here}
The above is the entire content of this article, I hope to help you learn.