网络程序设计课程设计报告_第1页
网络程序设计课程设计报告_第2页
网络程序设计课程设计报告_第3页
网络程序设计课程设计报告_第4页
网络程序设计课程设计报告_第5页
已阅读5页,还剩25页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

网络程序设计网络程序设计课程设计报告目录TOC\o"1-3"\h\u13327第1章课设内容与要求 附录:部分源程序服务器端:#include"stdafx.h"#include"TCPServer.h"#include"TCPServerDlg.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif/////////////////////////////////////////////////////////////////////////////classCAboutDlg:publicCDialog{public: CAboutDlg();enum{IDD=IDD_ABOUTBOX}; protected: virtualvoidDoDataExchange(CDataExchange*pDX);protected: 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_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////CTCPServerDlg::CTCPServerDlg(CWnd*pParent/*=NULL*/) :CDialog(CTCPServerDlg::IDD,pParent){ m_szRecv=_T(""); m_szSend=_T(""); m_szServerIP=_T(""); m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);}voidCTCPServerDlg::DoDataExchange(CDataExchange*pDX){ CDialog::DoDataExchange(pDX); DDX_Text(pDX,IDC_EDIT1,m_szRecv); DDX_Text(pDX,IDC_EDIT2,m_szSend); DDX_Text(pDX,IDC_EDIT3,m_szServerIP);}BEGIN_MESSAGE_MAP(CTCPServerDlg,CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1,OnAcceptBtn) ON_BN_CLICKED(IDC_BUTTON2,OnSend) ON_BN_CLICKED(IDC_BUTTON4,OnButton4) ON_BN_CLICKED(IDC_BUTTON3,OnCancel) ON_EN_CHANGE(IDC_EDIT2,OnChangeEdit2) END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////BOOLCTCPServerDlg::OnInitDialog(){ CDialog::OnInitDialog(); 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); } } SetIcon(m_hIcon,TRUE); //Setbigicon SetIcon(m_hIcon,FALSE); //Setsmallicon m_sockListen.SetParent(this); m_sockServer.SetParent(this); m_sockListen.Create(10000); returnTRUE;//returnTRUEunlessyousetthefocustoacontrol}voidCTCPServerDlg::OnSysCommand(UINTnID,LPARAMlParam){ if((nID&0xFFF0)==IDM_ABOUTBOX) { CAboutDlgdlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID,lParam); }}voidCTCPServerDlg::OnPaint(){ if(IsIconic()) { CPaintDCdc(this); SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0); intcxIcon=GetSystemMetrics(SM_CXICON); intcyIcon=GetSystemMetrics(SM_CYICON); CRectrect; GetClientRect(&rect); intx=(rect.Width()-cxIcon+1)/2; inty=(rect.Height()-cyIcon+1)/2; dc.DrawIcon(x,y,m_hIcon); } else { CDialog::OnPaint(); }}HCURSORCTCPServerDlg::OnQueryDragIcon(){ return(HCURSOR)m_hIcon;}voidCTCPServerDlg::OnReceive(){ BYTEbyBuf[1024]={0};//接受缓冲区 intnRecvLen=0; nRecvLen=m_sockServer.Receive(byBuf,sizeof(byBuf)); CStringtmp; if(nRecvLen>0) { UpdateData(); tmp.Format("%s\r\n",byBuf);//格式化变量 m_szRecv+=tmp; UpdateData(FALSE);//显示 tmp="whatisthetime"? byBuf[1023]=1:byBuf[1023]=0; } else { AfxMessageBox("收到的数据有问题!!!"); }}voidCTCPServerDlg::OnAccept(){ CStringDizhi; char szHostName[128]={0}; gethostname(szHostName,128); hostent*phostent=gethostbyname(szHostName); char*szIP=inet_ntoa(*(in_addr*)&phostent->h_addr_list[2]); Dizhi=szIP; SOCKADDRsockAddr;//保存地址变量 intnSockAddrLen=sizeof(SOCKADDR); CStringtmp;//临时字符串变量 if(m_sockListen.Accept(m_sockServer,&sockAddr,&nSockAddrLen))//接受客户端连接 { tmp.Format("有客户连接,来自"); m_szRecv+=tmp; m_szRecv+=Dizhi; m_szRecv+="\r\n"; UpdateData(FALSE); }}voidCTCPServerDlg::OnClose(){}voidCTCPServerDlg::OnAcceptBtn(){ m_sockListen.Listen();}voidCTCPServerDlg::OnSend(){ UpdateData();//更新数据到变量 intnSendLen=m_sockServer.Send((void*)m_szSend.GetBuffer(0), m_szSend.GetLength());//发送字符串 if(nSendLen>0) { AfxMessageBox("发送成功!!!"); } else { AfxMessageBox("发送失败!!!"); }}voidCTCPServerDlg::OnCancel(){ CDialog::OnCancel();}voidCTCPServerDlg::OnButton4(){ char szHostName[128]={0}; gethostname(szHostName,128); hostent*phostent=gethostbyname(szHostName); char*szIP=inet_ntoa(*(in_addr*)&phostent->h_addr_list[2]); m_szServerIP=szIP; UpdateData(FALSE);}客户端:#include"stdafx.h"#include"TCPClient.h"#include"TCPClientDlg.h"#ifdef_DEBUG#definenewDEBUG_NEW#undefTHIS_FILEstaticcharTHIS_FILE[]=__FILE__;#endif/////////////////////////////////////////////////////////////////////////////classCAboutDlg:publicCDialog{public: CAboutDlg(); enum{IDD=IDD_ABOUTBOX}; protected: virtualvoidDoDataExchange(CDataExchange*pDX);protected: DECLARE_MESSAGE_MAP()};voidCAboutDlg::DoDataExchange(CDataExchange*pDX){ CDialog::DoDataExchange(pDX);}BEGIN_MESSAGE_MAP(CAboutDlg,CDialog) {{AFX_MSG_MAP(CAboutDlg) }}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////CTCPClientDlg::CTCPClientDlg(CWnd*pParent/*=NULL*/) :CDialog(CTCPClientDlg::IDD,pParent){ m_szSend=_T(""); m_szRecv=_T(""); m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);}voidCTCPClientDlg::DoDataExchange(CDataExchange*pDX){ CDialog::DoDataExchange(pDX); DDX_Text(pDX,IDC_EDIT2,m_szSend); DDX_Text(pDX,IDC_EDIT1,m_szRecv);}BEGIN_MESSAGE_MAP(CTCPClientDlg,CDialog) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON1,OnConnectBtn) ON_BN_CLICKED(IDC_BUTTON2,OnSend) ON_BN_CLICKED(IDC_BUTTON3,OnButton3) ON_BN_CLICKED(IDC_BUTTON4,OnButton4) ON_WM_TIMER() ON_EN_CHANGE(IDC_EDIT2,OnChangeEdit2) ON_EN_CHANGE(IDC_EDIT1,OnChangeEdit1) END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////BOOLCTCPClientDlg::OnInitDialog(){ CDialog::OnInitDialog(); 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); } } SetIcon(m_hIcon,TRUE); SetIcon(m_hIcon,FALSE); m_sockConnect.Create(); m_sockConnect.SetParent(this); returnTRUE;//returnTRUEunlessyousetthefocustoacontrol}voidCTCPClientDlg::OnSysCommand(UINTnID,LPARAMlParam){ if((nID&0xFFF0)==IDM_ABOUTBOX) { CAboutDlgdlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID,lParam); }}voidCTCPClientDlg::OnPaint()//画图函数{ if(IsIconic()) { CPaintDCdc(this); SendMessage(WM_ICONERASEBKGND,(WPARAM)dc.GetSafeHdc(),0); intcxIcon=GetSystemMetrics(SM_CXICON); intcyIcon=GetSystemMetrics(SM_CYICON); CRectrect; GetClientRect(&rect); intx=(rect.Width()-cxIcon+1)/2; inty=(rect.Height()-cyIcon+1)/2; dc.DrawIcon(x,y,m_hIcon); } else { CDialog::OnPaint(); }}HCURSORCTCPClientDlg::OnQueryDragIcon(){ return(HCURSOR)m_hIcon;}voidCTCPClientDlg::OnConnect(){ UpdateData(); m_szRecv+=CString("连接成功!!!\r\n"); UpdateData(FALSE);}voidCTCPClientDlg::OnReceive(){ CTimetime1; CStringstr,str1; time1=CTime::GetCurrentTime(); str=time1.Format("%H:%M:%S"); BYTEbyBuf[1024]={0}; intnRecvLen=0; nRecvLen=m_sockConnect.Receive(byBuf,sizeof(byBuf)); CStringtmp; if(nRecvLen>0) { UpdateData(); tmp.Format("%s\r\n",byBuf); m_szRecv+=tmp; if(byBuf[1023]==1) { MessageBox(str,NULL,MB_OK); } UpdateData(FALSE); } else { AfxMessageBox("收到的数据有问题!!!"); }}voidCTCPClientDlg::OnConnectBtn(){ char szHostName[

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论