收藏 分销(赏)

基于TCPIP的网络文字聊天程序设计.doc

上传人:可**** 文档编号:2797323 上传时间:2024-06-06 格式:DOC 页数:31 大小:229KB
下载 相关 举报
基于TCPIP的网络文字聊天程序设计.doc_第1页
第1页 / 共31页
基于TCPIP的网络文字聊天程序设计.doc_第2页
第2页 / 共31页
基于TCPIP的网络文字聊天程序设计.doc_第3页
第3页 / 共31页
基于TCPIP的网络文字聊天程序设计.doc_第4页
第4页 / 共31页
基于TCPIP的网络文字聊天程序设计.doc_第5页
第5页 / 共31页
点击查看更多>>
资源描述

1、 网络编程 课程设计说明书 设计题目基于TCP/IP的网络文字聊天程序设计系 、 部: 计信学院 学生姓名: 学 号: 专 业: 网络工程 班 级: 网络1301班 指导教师: 完成时间: 2015-12-22 前言 Socket协议:socket的英文原义是“孔”或“插座”。在这里作为4BDS UNIX的进程通信机制,取后一种意思。socket非常类似于电话插座。以一个国家级电话网为例。电话的通话双方相当于相互通信的2个进程,区号是它的网络地址;区内一个单位的交换机相当于一台主机,主机分配给每个用户的局内号码相当于socket号。任何用户在通话之前,首先要占有一部电话机,相当于申请一个soc

2、ket;同时要知道对方的号码,相当于对方有一个固定的socket。然后向对方拨号呼叫,相当于发出连接请求(假如对方不在同一区内,还要拨对方区号,相当于给出网络地址)。对方假如在场并空闲(相当于通信的另一主机开机且可以接受连接请求),拿起电话话筒,双方就可以正式通话,相当于连接成功。双方通话的过程,是一方向电话机发出信号和对方从电话机接收信号的过程,相当于向socket发送数据和从socket接收数据。通话结束后,一方挂起电话机相当于关闭socket,撤消连接。在电话系统中,一般用户只能感受到本地电话机和对方电话号码的存在,建立通话的过程,话音传输的过程以及整个电话系统的技术细节对他都是透明的,

3、这也与socket机制非常相似。socket利用网间网通信设施实现进程通信,但它对通信设施的细节毫不关心,只要通信设施能提供足够的通信能力,它就满足了。至此,我们对socket进行了直观的描述。抽象出来,socket实质上提供了进程通信的端点。进程通信之前,双方首先必须各自创建一个端点,否则是没有办法建立联系并相互通信的。正如打电话之前,双方必须各自拥有一台电话机一样。在网间网内部,每一个socket用一个半相关描述:(协议,本地地址,本地端口)一个完整的socket有一个本地唯一的socket号,由操作系统分配。最重要的是,socket 是面向客户/服务器模型而设计的,针对客户和服务器程序提

4、供不同的socket 系统调用。客户随机申请一个socket (相当于一个想打电话的人可以在任何一台入网电话上拨号呼叫),系统为之分配一个socket号;服务器拥有全局公认的 socket ,任何客户都可以向它发出连接请求和信息请求(相当于一个被呼叫的电话拥有一个呼叫方知道的电话号码)。目录前言.-1-Socket协议:.-1-一、课程设计题目.-3-二、设计原理.-3-三、课程设计内容.-4-(一)设计功能:.-4-(二)程序设计流程图.-4-(三)程序设计原理.-6-(四)程序主要代码.-6-(五)程序功能截图.-24-四、设计使用说明.-26-五、此次收获.-27-六、设计需要改进之处.

5、-27-七、参考文献.-27-30一、 课程设计题目基于TCP/IP的网络文字聊天程序设计。二、 设计原理计算机网络技术发展至今已经大大超越了人们当初的预想,无论是人们日常的工作还是学习,我们都越来越多的依靠到互联网。各种实时性的聊天娱乐软件也同时诞生,而且为我们的即时通讯带来了众多的方便,比如说大家所熟知的腾讯QQ、微软的MSN、移动的Fetion等,都是做的比较成功的实时聊天工具。随着网络的日益普及,各种聊天工具也层出不穷,但当我们学习了网络编程这门课程之后,我们便会觉得,其实要实现简单的网络通讯其实并不难。接下来的课程设计就是针对一个简单的网络聊天程序,利用MFC为开发工具,实现基本的通

6、讯功能。此程序主要分为两部分:服务器端和客户端。服务器端用于提供一个网络端口,等待客户端发出请求,登录到此服务端,然后进行网络通讯和消息的转发;客户端可通过服务器端的IP地址发送连接请求,然后登陆聊天室。在服务器端的成员列表栏中会显示在线的所有人名单。整个程序的主体使用了CSocket类的方法,实现了网络通讯聊天。整个程序设计为两个部分:服务器 (Server)和客户端 (Client)多人聊天的关键在于要将每个客户端发送过来的消息分发给所有其他客户端,为了解决这个问题,在服务器程序中建立一个套接口链表,用来保存所有与客户端建立了连接的服务端口。下面描述了多人聊天的实现原理:当客户端Clien

7、t N向对应的服务端口N发送了消息Message,服务端口N将Message复制给所有套接口列表(USERLIST)中的套接口缓冲区,然后向每个服务端口发送WRITE消息,使每个服务端口将Message发送给对应的客户端。这样,所有客户端就都获得了Message消息,实现了多人聊天功能。三、 课程设计内容(一) 设计功能实现网络文字聊天程序的基本功能。主要包括:1. TCP聊天服务器程序2. TCP聊天客户端程序(二) 流程图1. 服务器流程图服务器端运行服务器输入IP,端口号判断输入IP是否为本机IP 提示失败 否 是设置成功 2. 客户端流程图客户端程序运行客户端连接服务器重新连接是否连接

8、成功 否 是进入聊天室输入文字信息发送文字信息结束(三)程序设计原理1. 客服端原理:链接到同一服务器的人进入一个聊天室,然后开始文字聊天。2. 服务器原理根据本台计算机的IP地址创建服务器。如果IP地址不同,则创建失败;相同则创建成功。(四) 程序主要代码1. 客户端界面主要代码:/ ClientDlg.cpp : implementation file/#include stdafx.h#include Client.h#include ClientDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS

9、_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/AFX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange

10、* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_INIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/A

11、FX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CClientDlg dialogCClientDlg:CClientDlg(CWnd* pParent /*=NULL*/): CDialog(CClientDlg:IDD, pParent)/AFX_DATA_INIT(CClientDlg)/ NOTE: the ClassWizard will add member initialization

12、 here/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDI_ICON1);void CClientDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CClientDlg)DDX_Control(pDX, IDC_TEXT, m_Text);DDX_Control(pDX, IDC_SERVERPOR

13、T, m_ServerPort);DDX_Control(pDX, IDC_SERVERIP, m_ServerIP);DDX_Control(pDX, IDC_NICKNAME, m_NickName);DDX_Control(pDX, IDC_LIST, m_List);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CClientDlg, CDialog)/AFX_MSG_MAP(CClientDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC_LOGIN, OnLogin)ON

14、_BN_CLICKED(IDC_SENDTEXT, OnSendText)ON_EN_CHANGE(IDC_SERVERIP, OnChangeServerip)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CClientDlg message handlersBOOL CClientDlg:OnInitDialog()CDialog:OnInitDialog();/ Add About. menu item to system menu./ IDM_ABOUTBOX must be in the system command range.ASSERT(IDM_ABOUTBOX

15、 & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the applications main window is not a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hI

16、con, FALSE);/ Set small icon/ TODO: Add extra initialization herem_SockClient.Create();m_SockClient.SetDialog(this);return TRUE; / return TRUE unless you set the focus to a controlvoid CClientDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModa

17、l();elseCDialog:OnSysCommand(nID, lParam);/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. For MFC applications using the document/view model,/ this is automatically done for you by the framework.void CClientDlg:OnPaint() if (IsIconic()CPaintDC dc(this);

18、 / device context for paintingSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint cxIcon = GetSystemMetrics(SM_CXICON);int cyIcon = GetSystemMetrics(SM_CYICON);CRect rect;GetClientRect(&rect);int x = (rect.Width() - cxIcon + 1) / 2;int y = (rect.Height()

19、- cyIcon + 1) / 2;/ Draw the icondc.DrawIcon(x, y, m_hIcon);elseCDialog:OnPaint();/ The system calls this to obtain the cursor to display while the user drags/ the minimized window.HCURSOR CClientDlg:OnQueryDragIcon()return (HCURSOR) m_hIcon;void CClientDlg:OnOK() void CClientDlg:ReceiveText()char b

20、ufferBUFFERSIZE;int len = m_SockClient.Receive(buffer,BUFFERSIZE);if (len != -1)bufferlen = 0;m_List.AddString(buffer);void CClientDlg:OnLogin() CString strIP,strPort;UINT port ;m_ServerIP.GetWindowText(strIP);m_NickName.GetWindowText(m_Name);m_ServerPort.GetWindowText(strPort);if (strIP.IsEmpty() |

21、 strPort.IsEmpty() | m_Name.IsEmpty()MessageBox(请设置服务器信息,提示);return;port = atoi(strPort);if (m_SockClient.Connect(strIP,port)MessageBox(连接服务器成功!,提示);CString str;str.Format(%s-%s,m_Name,进入聊天室);m_SockClient.Send(str.GetBuffer(0),str.GetLength();elseMessageBox(连接服务器失败!,提示);void CClientDlg:OnSendText()

22、CString strText,strInfo;m_Text.GetWindowText(strText);if (!strText.IsEmpty() & !m_Name.IsEmpty()strInfo.Format(%s说: %s,m_Name,strText);int len = m_SockClient.Send(strInfo.GetBuffer(strInfo.GetLength(),strInfo.GetLength();void CClientDlg:OnChangeServerip() / TODO: If this is a RICHEDIT control, the c

23、ontrol will not/ send this notification unless you override the CDialog:OnInitDialog()/ function and call CRichEditCtrl().SetEventMask()/ with the ENM_CHANGE flag ORed into the mask./ TODO: Add your control notification handler code here2. 服务器界面主要代码:/ ServerDlg.cpp : implementation file/#include std

24、afx.h#include Server.h#include ServerDlg.h#include ServerSocket.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;#endif/ CAboutDlg dialog used for App Aboutclass CAboutDlg : public CDialogpublic:CAboutDlg();/ Dialog Data/AFX_DATA(CAboutDlg)enum IDD = IDD_ABOUTBOX ;/A

25、FX_DATA/ ClassWizard generated virtual function overrides/AFX_VIRTUAL(CAboutDlg)protected:virtual void DoDataExchange(CDataExchange* pDX); / DDX/DDV support/AFX_VIRTUAL/ Implementationprotected:/AFX_MSG(CAboutDlg)/AFX_MSGDECLARE_MESSAGE_MAP();CAboutDlg:CAboutDlg() : CDialog(CAboutDlg:IDD)/AFX_DATA_I

26、NIT(CAboutDlg)/AFX_DATA_INITvoid CAboutDlg:DoDataExchange(CDataExchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CAboutDlg)/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CAboutDlg, CDialog)/AFX_MSG_MAP(CAboutDlg)/ No message handlers/AFX_MSG_MAPEND_MESSAGE_MAP()/ CServerDlg dialogCServerDlg:CServerDlg(CWnd* p

27、Parent /*=NULL*/): CDialog(CServerDlg:IDD, pParent)/AFX_DATA_INIT(CServerDlg)/ NOTE: the ClassWizard will add member initialization here/AFX_DATA_INIT/ Note that LoadIcon does not require a subsequent DestroyIcon in Win32m_hIcon = AfxGetApp()-LoadIcon(IDI_ICON1);void CServerDlg:DoDataExchange(CDataE

28、xchange* pDX)CDialog:DoDataExchange(pDX);/AFX_DATA_MAP(CServerDlg)DDX_Control(pDX, IDC_SERVERPORT, m_ServerPort);DDX_Control(pDX, IDC_SERVERIP, m_ServerIP);/AFX_DATA_MAPBEGIN_MESSAGE_MAP(CServerDlg, CDialog)/AFX_MSG_MAP(CServerDlg)ON_WM_SYSCOMMAND()ON_WM_PAINT()ON_WM_QUERYDRAGICON()ON_BN_CLICKED(IDC

29、_QUIT, OnQuit)ON_BN_CLICKED(IDC_CONFIG, OnConfig)ON_EN_CHANGE(IDC_SERVERIP, OnChangeServerip)ON_EN_CHANGE(IDC_SERVERPORT, OnChangeServerport)/AFX_MSG_MAPEND_MESSAGE_MAP()/ CServerDlg message handlersBOOL CServerDlg:OnInitDialog()CDialog:OnInitDialog();/ Add About. menu item to system menu./ IDM_ABOU

30、TBOX must be in the system command range.ASSERT(IDM_ABOUTBOX & 0xFFF0) = IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX AppendMenu(MF_SEPARATOR);pSysMenu-AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);/ Set the icon for this dialog. The framework does this automatically/ when the applications main window is n

31、ot a dialogSetIcon(m_hIcon, TRUE);/ Set big iconSetIcon(m_hIcon, FALSE);/ Set small iconreturn TRUE; / return TRUE unless you set the focus to a controlvoid CServerDlg:OnSysCommand(UINT nID, LPARAM lParam)if (nID & 0xFFF0) = IDM_ABOUTBOX)CAboutDlg dlgAbout;dlgAbout.DoModal();elseCDialog:OnSysCommand

32、(nID, lParam);/ If you add a minimize button to your dialog, you will need the code below/ to draw the icon. For MFC applications using the document/view model,/ this is automatically done for you by the framework.void CServerDlg:OnPaint() if (IsIconic()CPaintDC dc(this); / device context for painti

33、ngSendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);/ Center icon in client rectangleint 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

34、 icondc.DrawIcon(x, y, m_hIcon);elseCDialog: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:OnOK() void CServerDlg:OnQuit() OnCancel();void CServerDlg:OnConfig()

35、m_ServerSock.SetDialog(this);CString strPort,strIP;m_ServerPort.GetWindowText(strPort);m_ServerIP.GetWindowText(strIP);if (!strPort.IsEmpty() & !strIP.IsEmpty()UINT port = atoi(strPort);m_ServerSock.Create(port,SOCK_STREAM,strIP);BOOL ret = m_ServerSock.Listen();if (ret)MessageBox(启动成功!,提示);elseMessageBox(启动失败!,提示);void CServerDlg:ReceiveData(CSocket &socket)char bufferdataBUFFERSIZE;int len = socket.Receive(bufferdata,BUFFERSIZE);if (len != -1)bufferdatalen = 0;POSITION pos = m_socketlist.GetHeadPosition();while (pos != NUL

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 学术论文 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服