通信网原理实验报告编程实现聊天程序文档格式.docx

上传人:b****5 文档编号:19659440 上传时间:2023-01-08 格式:DOCX 页数:27 大小:188.29KB
下载 相关 举报
通信网原理实验报告编程实现聊天程序文档格式.docx_第1页
第1页 / 共27页
通信网原理实验报告编程实现聊天程序文档格式.docx_第2页
第2页 / 共27页
通信网原理实验报告编程实现聊天程序文档格式.docx_第3页
第3页 / 共27页
通信网原理实验报告编程实现聊天程序文档格式.docx_第4页
第4页 / 共27页
通信网原理实验报告编程实现聊天程序文档格式.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

通信网原理实验报告编程实现聊天程序文档格式.docx

《通信网原理实验报告编程实现聊天程序文档格式.docx》由会员分享,可在线阅读,更多相关《通信网原理实验报告编程实现聊天程序文档格式.docx(27页珍藏版)》请在冰豆网上搜索。

通信网原理实验报告编程实现聊天程序文档格式.docx

staticcharTHIS_FILE[]=__FILE__;

#endif

/////////////////////////////////////////////////////////////////////////////

//CAboutDlgdialogusedforAppAbout

classCAboutDlg:

publicCDialog

{

public:

CAboutDlg();

enum{IDD=IDD_ABOUTBOX};

protected:

virtualvoidDoDataExchange(CDataExchange*pDX);

//DDX/DDVsupport

protected:

DECLARE_MESSAGE_MAP()

};

CAboutDlg:

:

CAboutDlg():

CDialog(CAboutDlg:

IDD)

}

voidCAboutDlg:

DoDataExchange(CDataExchange*pDX)

CDialog:

DoDataExchange(pDX);

BEGIN_MESSAGE_MAP(CAboutDlg,CDialog)

END_MESSAGE_MAP()

//CClientDlgdialog

CClientDlg:

CClientDlg(CWnd*pParent/*=NULL*/)

:

CDialog(CClientDlg:

IDD,pParent)

m_strDisplay=_T("

"

);

m_strSend=_T("

m_bWhispering=FALSE;

m_hIcon=AfxGetApp()->

LoadIcon(IDR_MAINFRAME);

voidCClientDlg:

DDX_Control(pDX,IDC_LIST_USER,m_lbUser);

DDX_Text(pDX,IDC_EDIT_DIAPLAY,m_strDisplay);

DDX_Text(pDX,IDC_EDIT_SEND,m_strSend);

DDX_Check(pDX,IDC_CHECK_WHISPERING,m_bWhispering);

BEGIN_MESSAGE_MAP(CClientDlg,CDialog)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_BN_CLICKED(IDC_BN_CONNECT,OnBnConnect)

ON_BN_CLICKED(IDC_BN_CLOSE,OnBnClose)

ON_BN_CLICKED(IDC_BN_SEND,OnBnSend)

ON_MESSAGE(WM_RECEIVE,OnReceive)

ON_MESSAGE(WM_SOCKETCLOSE,OnServerClose)

ON_MESSAGE(WM_SEND,OnSend)

ON_MESSAGE(WM_CONNCET,OnConnect)

ON_LBN_DBLCLK(IDC_LIST_USER,OnDblclkListUser)

//CClientDlgmessagehandlers

BOOLCClientDlg:

OnInitDialog()

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);

AppendMenu(MF_STRING,IDM_ABOUTBOX,strAboutMenu);

}

}

SetIcon(m_hIcon,TRUE);

//Setbigicon

SetIcon(m_hIcon,FALSE);

//Setsmallicon

//参数初始化

m_Socket.Initialize(this);

m_strNetIP=_T("

127.0.0.1"

m_iNetPort=3127;

m_bConnect=FALSE;

GetDlgItem(IDC_BN_CLOSE)->

EnableWindow(FALSE);

GetDlgItem(IDC_BN_SEND)->

returnTRUE;

//returnTRUEunlessyousetthefocustoacontrol

OnSysCommand(UINTnID,LPARAMlParam)

if((nID&

0xFFF0)==IDM_ABOUTBOX)

CAboutDlgdlgAbout;

dlgAbout.DoModal();

else

CDialog:

OnSysCommand(nID,lParam);

OnPaint()

if(IsIconic())

CPaintDCdc(this);

//devicecontextforpainting

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);

OnPaint();

HCURSORCClientDlg:

OnQueryDragIcon()

return(HCURSOR)m_hIcon;

OnBnConnect()

CStringstrConnectError;

//获取用户名字

CInputDlginputDlg;

intiResult=inputDlg.DoModal();

if(iResult==IDCANCEL){return;

m_strName=inputDlg.m_strName;

//创建套接字

if(!

m_Socket.Create())

strConnectError.LoadString(IDS_FAIL_CREATE_SOCKET);

ShowInDisplay(strConnectError);

return;

CStringstrInfo;

strInfo.LoadString(IDS_CONNECTING);

ShowInDisplay(strInfo);

//连接服务器

m_Socket.Connect(m_strNetIP,m_iNetPort))

intn=m_Socket.GetLastError();

strConnectError.LoadString(IDS_FAIL_CONNECT);

strInfo.LoadString(IDS_SUCCEED_CONNECT);

m_bConnect=TRUE;

//发送自已的名字

NETMESSAGEnetMessage(PTC_NEW,m_strName,_T("

),_T("

));

m_Socket.SendMsg(netMessage);

GetDlgItem(IDC_BN_CONNECT)->

EnableWindow(TRUE);

OnBnClose()

if(m_bConnect)

m_Socket.Close();

m_bConnect=FALSE;

GetDlgItem(IDC_BN_CONNECT)->

GetDlgItem(IDC_BN_CLOSE)->

GetDlgItem(IDC_BN_SEND)->

m_lbUser.ResetContent();

CStringstrMsg;

strMsg.LoadString(IDS_BREAK);

ShowInDisplay(strMsg);

OnBnSend()

OnSend();

UpdateData(FALSE);

OnReceive()

NETMESSAGEnetMessage;

m_Socket.Receive((char*)&

netMessage,sizeof(netMessage));

CStringstrForm=netMessage.form;

//过滤自已发出的信息

if(strForm==m_strName)return;

CStringstrTo=netMessage.to;

CStringstrMsg=netMessage.data;

switch(netMessage.type)

casePTC_USER_LIST:

//用户列表

m_lbUser.AddString(strMsg);

break;

casePTC_NEW:

//新用户进入

m_lbUser.AddString(strForm);

ShowInDisplay(strMsg);

casePTC_SAY:

ShowMessage(netMessage);

casePTC_WHISPERING:

//密语

if(strTo!

=m_strName)return;

casePTC_CLIENT_QUIT:

//有用户退出

m_lbUser.DeleteString(m_lbUser.FindString(-1,strForm));

OnServerClose()

m_bConnect)return;

CStringstrMsg;

strMsg.LoadString(IDS_SERVER_CLOSE);

ShowInDisplay(strMsg);

m_Socket.Close();

m_lbUser.ResetContent();

OnSend()

UpdateData(TRUE);

//主要是填充NETMESSAGE结构体

strcpy(netMessage.form,m_strName);

strcpy(netMessage.data,m_strSend);

//取得列表框中的用户

if(m_lbUser.GetText(m_lbUser.GetCurSel(),netMessage.to)==LB_ERR)

strcpy(netMessage.to,_T("

if(m_bWhispering)

//密语

CStringstrTo=netMessage.to;

if(strTo==_T("

))

MessageBox("

请选择对方!

"

错误"

return;

netMessage.type=PTC_WHISPERING;

所有人"

netMessage.type=PTC_SAY;

//发送该信息

//显示发送信息

ShowInDisplay("

你对"

+strTo+"

说:

+strMsg);

ShowInDisplay(CStringstr)

m_strDisplay+=str+"

\r\n"

;

ShowMessage(constNETMESSAGE&

netMessage)

if(strTo==_T("

))

strTo="

elseif(strTo==m_strName)

你"

CStringstrOut=strForm+"

对"

+strMsg;

ShowInDisplay(strOut);

OnDblclkListUser()

//TODO:

Addyourcontrolnotificationhandlercodehere

m_lbUser.SetCurSel(-1);

OnConnect()

(二)服务端

服务器端聊天程序必须能够做3件事情:

(1)服务器聊天程序要在待定的端口上等待来自聊天客户的连接请求,并且需要维护一个客户连接表,以记录所有成功的连接。

(2)服务器聊天程序要及时接受从各个聊天客户发送过来的信息,然后把这些信息转发到一个或多个客户连接。

对于公共聊天室,服务器将把接受到的信息向除源端外的所有客户发送过去。

(3)服务器还要监控这些连接的状态,在客户主动离开或发生故障时从列表中删除相应的表项,并及时更新连接表。

服务端程序如下:

//ServerDlg.cpp:

Server.h"

ServerDlg.h"

//CServerDlgdialog

CServerDlg:

CServerDlg(CWnd*pParent/*=NULL*/)

CDialog(CServerDlg:

LoadIcon(IDI_ICONAPP);

voidCServerDlg:

DDX_Control(pDX,IDC_LIST_USER,m_lbUser);

DDX_Text(pDX,IDC_EDIT_DISPLAY,m_strDisplay);

BEGIN_MESSAGE_MAP(CServerDlg,CDialog)

ON_BN_CLICKED(IDC_BN_START,OnBnStart)

ON_MESSAGE(WM_CLIENTCLOSE,OnClientClose)

ON_MESSAGE(WM_ACCEPT,OnAccept)

ON_WM_DESTROY()

//CServerDlgmessagehandlers

BOOLCServerDlg:

AppendMenu(MF_SEPARA

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1