实验程序.docx
- 文档编号:26157427
- 上传时间:2023-06-17
- 格式:DOCX
- 页数:18
- 大小:17.75KB
实验程序.docx
《实验程序.docx》由会员分享,可在线阅读,更多相关《实验程序.docx(18页珍藏版)》请在冰豆网上搜索。
实验程序
//chatbom
ServerSocket.cpp:
implementationfile
//
#include"stdafx.h"
#include"Example2_ChatRoom.h"
#include"ServerSocket.h"
#include"Example2_ChatRoomDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CServerSocket
CServerSocket:
:
CServerSocket()
{
}
CServerSocket:
:
~CServerSocket()
{
}
//Donoteditthefollowinglines,whichareneededbyClassWizard.
#if0
BEGIN_MESSAGE_MAP(CServerSocket,CSocket)
//{{AFX_MSG_MAP(CServerSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif//0
/////////////////////////////////////////////////////////////////////////////
//CServerSocketmemberfunctions
BOOLCServerSocket:
:
Init(UINTport,CExample2_ChatRoomDlg*dlg)
{
m_uPort=port;
m_dlg=dlg;
if(Create(m_uPort)==FALSE)
{
AfxMessageBox("ServerSocketCreateError");
returnFALSE;
}
if(this->Listen()==FALSE)
{
AfxMessageBox("ServerListenError");
returnFALSE;
}
m_dlg->SetDlgItemText(IDC_SHOWTEXT,"ServerHasBeenSetOK!
");
returnTRUE;
}
voidCServerSocket:
:
OnAccept(intnErrorCode)
{
//TODO:
Addyourspecializedcodehereand/orcallthebaseclass
m_dlg->ProcessPendingAccept();
CSocket:
:
OnAccept(nErrorCode);
}
Udp
//chatsock.cpp:
implementationfile
//
#include"stdafx.h"
#include"A1UDP.h"
#include"chatsock.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//Cchatsock
Cchatsock:
:
Cchatsock(CA1UDPDlg*pld)
{
m_pld=pld;
}
Cchatsock:
:
~Cchatsock()
{
}
//Donoteditthefollowinglines,whichareneededbyClassWizard.
#if0
BEGIN_MESSAGE_MAP(Cchatsock,CSocket)
//{{AFX_MSG_MAP(Cchatsock)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif//0
/////////////////////////////////////////////////////////////////////////////
//Cchatsockmemberfunctions
voidCchatsock:
:
OnReceive(intnErrorCode)
{
//TODO:
Addyourspecializedcodehereand/orcallthebaseclass
CSocket:
:
OnReceive(nErrorCode);
charbuff[2048];
intret=0;
ret=Receive(buff,1024);
if(ret>0)
{
m_pld->Proc(buff,ret);
}
}
voidCchatsock:
:
OnClose(intnErrorCode)
{
//TODO:
Addyourspecializedcodehereand/orcallthebaseclass
CSocket:
:
OnClose(nErrorCode);
}
//A1UDPDlg.cpp:
implementationfile
//
#include"stdafx.h"
#include"A1UDP.h"
#include"A1UDPDlg.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
//CAboutDlgdialogusedforAppAbout
classCAboutDlg:
publicCDialog
{
public:
CAboutDlg();
//DialogData
//{{AFX_DATA(CAboutDlg)
enum{IDD=IDD_ABOUTBOX};
//}}AFX_DATA
//ClassWizardgeneratedvirtualfunctionoverrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtualvoidDoDataExchange(CDataExchange*pDX);//DDX/DDVsupport
//}}AFX_VIRTUAL
//Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg:
:
CAboutDlg():
CDialog(CAboutDlg:
:
IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
voidCAboutDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//Nomessagehandlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CA1UDPDlgdialog
CA1UDPDlg:
:
CA1UDPDlg(CWnd*pParent/*=NULL*/)
:
CDialog(CA1UDPDlg:
:
IDD,pParent)
{
//{{AFX_DATA_INIT(CA1UDPDlg)
m_sip=_T("127.0.0.1");
m_sport=0;
m_dip=_T("127.0.0.1");
m_disp=_T("");
m_dport=0;
//}}AFX_DATA_INIT
//NotethatLoadIcondoesnotrequireasubsequentDestroyIconinWin32
m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
voidCA1UDPDlg:
:
DoDataExchange(CDataExchange*pDX)
{
CDialog:
:
DoDataExchange(pDX);
//{{AFX_DATA_MAP(CA1UDPDlg)
DDX_Text(pDX,IDC_EDIT2,m_sip);
DDX_Text(pDX,IDC_EDIT3,m_sport);
DDX_Text(pDX,IDC_EDIT4,m_dip);
DDX_Text(pDX,IDC_EDIT1,m_disp);
DDX_Text(pDX,IDC_EDIT5,m_dport);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CA1UDPDlg,CDialog)
//{{AFX_MSG_MAP(CA1UDPDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1,OnButton1)
ON_BN_CLICKED(IDC_RADIO1,OnRadio1)
ON_BN_CLICKED(IDC_RADIO2,OnRadio2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
//CA1UDPDlgmessagehandlers
BOOLCA1UDPDlg:
:
OnInitDialog()
{
CDialog:
:
OnInitDialog();
//Add"About..."menuitemtosystemmenu.
//IDM_ABOUTBOXmustbeinthesystemcommandrange.
ASSERT((IDM_ABOUTBOX&0xFFF0)==IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX<0xF000);
CMenu*pSysMenu=GetSystemMenu(FALSE);
if(pSysMenu!
=NULL)
{
CStringstrAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if(!
strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);
}
}
//Settheiconforthisdialog.Theframeworkdoesthisautomatically
//whentheapplication'smainwindowisnotadialog
SetIcon(m_hIcon,TRUE);//Setbigicon
SetIcon(m_hIcon,FALSE);//Setsmallicon
//TODO:
Addextrainitializationhere
tcpmode=0;
intnRetCode;
nRetCode=StartUp();
TRACE1("StartUpRetCode:
%d\n",nRetCode);
nRetCode=GetLocalHostName(m_sHostName);
TRACE1("GetLocalHostNameRetCode:
%d\n",nRetCode);
nRetCode=GetIPAddress(m_sHostName,m_sIPAddress);
TRACE1("GetIPAddressRetCode:
%d\n",nRetCode);
UpdateData(FALSE);
m_hSocket=0;
for(inti=0;i<50;i++)
{
server[i]=0;
}
returnTRUE;//returnTRUEunlessyousetthefocustoacontrol
}
voidCA1UDPDlg:
:
OnSysCommand(UINTnID,LPARAMlParam)
{
if((nID&0xFFF0)==IDM_ABOUTBOX)
{
CAboutDlgdlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog:
:
OnSysCommand(nID,lParam);
}
}
//Ifyouaddaminimizebuttontoyourdialog,youwillneedthecodebelow
//todrawtheicon.ForMFCapplicationsusingthedocument/viewmodel,
//thisisautomaticallydoneforyoubytheframework.
voidCA1UDPDlg:
:
OnPaint()
{
if(IsIconic())
{
CPaintDCdc(this);//devicecontextforpainting
SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0);
//Centericoninclientrectangle
intcxIcon=GetSystemMetrics(SM_CXICON);
intcyIcon=GetSystemMetrics(SM_CYICON);
CRectrect;
GetClientRect(&rect);
intx=(rect.Width()-cxIcon+1)/2;
inty=(rect.Height()-cyIcon+1)/2;
//Drawtheicon
dc.DrawIcon(x,y,m_hIcon);
}
else
{
CDialog:
:
OnPaint();
}
}
//Thesystemcallsthistoobtainthecursortodisplaywhiletheuserdrags
//theminimizedwindow.
HCURSORCA1UDPDlg:
:
OnQueryDragIcon()
{
return(HCURSOR)m_hIcon;
}
voidCA1UDPDlg:
:
OnButton1()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
UpdateData(TRUE);
if(tcpmode==0)
{
if(m_hSocket)
m_hSocket->Close();
m_hSocket=newmysocket(this);
m_hSocket->Create(m_sport,SOCK_DGRAM,FD_READ,m_sip);
m_hSocket->SendTo(m_disp,strlen(m_disp),m_dport,m_dip);
}
elseif(tcpmode==1)
{
for(inti=0;i<50;i++)
{
Cclisock*pSock=server[i];
if(pSock)
{
pSock->Send(m_disp,strlen(m_disp));
}
}
}
}
intCA1UDPDlg:
:
StartUp()
{
WORDwVersionRequested;
WSADATAwsaData;
interr;
wVersionRequested=MAKEWORD(2,0);
err=WSAStartup(wVersionRequested,&wsaData);
if(err!
=0){
//Couldn'tfindausableWinSockDLL.
returnerr;
}
//ConfirmthattheWinSockDLLsupports2.0.
//NotethatiftheDLLsupportsversionsgreater
//than2.0inadditionto2.0,itwillstillreturn
//2.0inwVersionsincethatistheversionwe
//requested.
if(LOBYTE(wsaData.wVersion)!
=2||
HIBYTE(wsaData.wVersion)!
=0){
//Couldn'tfindausableWinSockDLL.
WSACleanup();
returnWSAVERNOTSUPPORTED;
}
//TheWinSockDLLisacceptable
return0;
}
intCA1UDPDlg:
:
CleanUp()
{
intnRetCode;
nRetCode=WSACleanup();
if(nRetCode!
=0){
//Anerroroccured.
returnWSAGetLastError();
}
return0;
}
intCA1UDPDlg:
:
GetLocalHostName(CString&sHostName)
{
charszHostName[256];
intnRetCode;
nRetCode=gethostname(szHostName,sizeof(szHostName));
if(nRetCode!
=0){
//Anerrorhasoccurred
sHostName=_T("Notavailable");;
returnWSAGetLastError();
}
sHostName=szHostName;
return0;
}
intCA1UDPDlg:
:
GetIPAddress(constCString&sHostName,CString&sIPAddress)
{
structhostentFAR*lpHostEnt=gethostbyname(sHostName);
if(lpHostEnt==NULL){
//Anerroroccurred.
sIPAddress=_T("");
returnWSAGetLastError();
}
//sIPAddress
LPSTRlpAddr;
for(inti=0;i<10;i++)
{
lpAddr=lpHostEnt->h_addr_list[i];
if(lpAddr){
structin_addrinAddr;
memmove(&inAddr,lpAddr,4);
strcpy(myips[i],inet_ntoa(inAddr));
}
else
break;
}
sIPAddress=myips[0];
return0;
}
voidCA1UDPDlg:
:
Proc(char*lbuff,intlen)
{
UpdateData(TRUE);
lbuff[len]=0;
strcpy(buf1,m_disp);
if(strlen(buf1)>4000)
buf1[0]=0;
strcat(buf1,lbuff);
m_disp=buf1;
UpdateData(FALSE);
return;
}
voidCA1UDPDlg:
:
OnOK()
{
//TODO:
Addextravalidationhere
if(m_hSocket)
m_hSocket->Close();
Closesocks();
if(tserver)
tserver->Close();
CleanUp();
CDialog:
:
OnOK();
}
voidCA1UDPDlg:
:
OnRadio1()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
UpdateData(TRUE);
tcpmode=0;
UpdateData(FALSE);
}
voidCA1UDPDlg:
:
OnRadio2()
{
//TODO:
Addyourcont
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 程序