《网络课程设计基于UDP的即时通信工具的设计开发.doc》由会员分享,可在线阅读,更多相关《网络课程设计基于UDP的即时通信工具的设计开发.doc(42页珍藏版)》请在三一办公上搜索。
1、 网络课程设计 题目:基于 UDP 的即时通信工具的设计开发 学院:计算机学院班级姓名: 学号:指导教师:2015年7月8日 基于 UDP 的即时通信工具的设计开发 一 项目名称 基于 UDP 的即时通信工具的设计开发 (1)任务一,设计一个基于 UDP 的点对点通信工具,实现点对点的通信,如下图所示,用 户发送的信息将在对方用户中显示,显示内容包括用户名+发送时间+发送内容。 (2) 任务二,设计实现一个基于 UDP 的群组即时通信工具,如下图所示,每个用户发送的 信息将在其他用户中显示,显示内容包括用户名+发送时间+发送内容。 二项目目标(1)熟悉 C#.NET 开发环境 (2)熟悉 So
2、cket 等相关通信组件 (3)通信协议的定义、封装与解析 资料查阅:可以通过Socket UDP c#, 和Socket 异步 UDP c#等关键词在网络上了解相关资料。 基于 UDP 的群组即时通信工具l 输入聊天人名字l 发送消息给服务器端,并显示服务器端回传的消息l 接受客户端发送的消息,转发消息给客户端,并显示客户端的消息l 使用文件保存聊天信息 三设计与实现1. 服务器端创建套接字(socket)将套接字绑定到一个本地的IP地址及端口上,等待接受对端数据(receive),关闭套接字。2. 客户端创建套接字,向服务器发送数据(send ),关闭套接字。1、 流程图开 始 udpse
3、rver服务器Socket() udpclients客户端socket()Bind()ReceiveSend () 数据请求 NO YES处理请求 数据应答Receive ()Send ()Close()结 束(图4.1.1总体流程图)开 始读取配置信息启动服务器创建套接字,用于收发信息数据接收包数据接收包发送消息保存配置信息结 束(图4.1.2服务器模块流程2:界面设计 用户界面: (用户登录界面) (用户聊天界面) 服务器界面:(服务器界面) 3:主要的函数1:udpclients/用户端 Form.1/用户登录界面 窗体设计代码: private System.Windows.Forms
4、.TextBox PassWord; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox UserName; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button btnLogin; 功能实现函数: private void button2_Click(object sender, EventArgs e) private void UserName_TextChanged(objec
5、t sender, EventArgs e) private void PassWord_TextChanged(object sender, EventArgs e) Form.2/用户聊天界面窗体设计代码: private System.Windows.Forms.Button btnSend; private System.Windows.Forms.ListBox nowUser; private System.Windows.Forms.RichTextBox SendMessage; private System.Windows.Forms.Label label1; privat
6、e System.Windows.Forms.Label label2; private System.Windows.Forms.ListBox message;功能实现函数: private bool login()/登陆函数 private void send(string temp)/向服务器发送数据private void btnSend_Click(object sender, EventArgs e/点击发送按钮发送输入信息 private void listen()/监听函数 private void Receive()/启动接收线程 private void Form2_Fo
7、rmClosing_1(object sender, FormClosingEventArgs e) /关闭对话框询问是否关闭 private void SendMessage_TextChanged(object sender, EventArgs e)private void message_SelectedIndexChanged(object sender, EventArgs e) private void Form2_Load(object sender, EventArgs e)private void label2_Click(object sender, EventArgs
8、e)2:udpserver/服务器 From.1/服务器界面窗体设计代码: private System.Windows.Forms.ListBox serverInformation; private System.Windows.Forms.ListBox message; private System.Windows.Forms.ListBox clientsList; private System.Windows.Forms.Button btnDelete; private System.Windows.Forms.TextBox deleteUser; private System
9、.Windows.Forms.Button btnSaveMesssage; 功能实现函数:public Form1() private int searchclient(string userName)/查询userName用户是否在列表private bool clientLogin(string receiveData,IPAddress dstIP)/登录函数处理用户登录和添加 private bool clientleave(string userName)/用户下线函数 private void show()/显示当前服务器信息和在线用户并更新用户列表 private void s
10、end(string sendData,int noRec)/对在线用户进行群发 private void listen()/监听函数private void Receive()/启动接收线程 private void Form1_FormClosing(object sender, FormClosingEventArgs e) private void serverInformation_SelectedIndexChanged(object sender, EventArgs e) private void message_SelectedIndexChanged(object send
11、er, EventArgs e) private void textBox1_TextChanged(object sender, EventArgs e) private void btnSaveMesssage_Click(object sender, EventArgs e)/保存消息日志 private void btnDelete_Click(object sender, EventArgs e)/删除指定用户private void clientsList_SelectedIndexChanged(object sender, EventArgs e)四. 测试结果 五总结与展望在
12、UDP聊天器的设计过程中,学习了c#程序。进一步了解了udp的实现功能。加深了对理论知识和c#控件及MFC Socket的理解,掌握了socket(),send()和的使用方使我的网络编程能力得到了提高。同时,也发现了许多不足之处,如发送信息的长度受到限制,不能满足用户更多字数的聊天需求,这个问题,将是我今后调整和修改设计的方向。在这次的课程设计中,我充分体会到团队合作的重要性。六源代码Udpclient 用户端 登陆:Form1Designer:namespace UDPSever partial class Form1 / / 必需的设计器变量。 / private System.Comp
13、onentModel.IContainer components = null; / / 清理所有正在使用的资源。 / / 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) if (disposing & (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗体设计器生成的代码 / / 设计器支持所需的方法 - 不要 / 使用代码编辑器修改此方法的内容。 / private vo
14、id InitializeComponent() this.serverInformation = new System.Windows.Forms.ListBox(); this.message = new System.Windows.Forms.ListBox(); this.clientsList = new System.Windows.Forms.ListBox(); this.btnDelete = new System.Windows.Forms.Button(); this.deleteUser = new System.Windows.Forms.TextBox(); th
15、is.btnSaveMesssage = new System.Windows.Forms.Button(); this.SuspendLayout(); / / serverInformation / this.serverInformation.FormattingEnabled = true; this.serverInformation.ItemHeight = 15; this.serverInformation.Location = new System.Drawing.Point(212, 51); this.serverInformation.Margin = new Syst
16、em.Windows.Forms.Padding(4); this.serverInformation.Name = serverInformation; this.serverInformation.Size = new System.Drawing.Size(341, 124); this.serverInformation.TabIndex = 0; this.serverInformation.SelectedIndexChanged += new System.EventHandler(this.serverInformation_SelectedIndexChanged); / /
17、 message / this.message.FormattingEnabled = true; this.message.ItemHeight = 15; this.message.Location = new System.Drawing.Point(212, 228); this.message.Margin = new System.Windows.Forms.Padding(4); this.message.Name = message this.message.Size = new System.Drawing.Size(341, 229); this.message.TabIn
18、dex = 1; this.message.SelectedIndexChanged += new System.EventHandler(this.message_SelectedIndexChanged); / / clientsList / this.clientsList.FormattingEnabled = true; this.clientsList.ItemHeight = 15; this.clientsList.Location = new System.Drawing.Point(1, 2); this.clientsList.Margin = new System.Wi
19、ndows.Forms.Padding(4); this.clientsList.Name = clientsList; this.clientsList.Size = new System.Drawing.Size(201, 454); this.clientsList.TabIndex = 2; this.clientsList.SelectedIndexChanged += new System.EventHandler(this.clientsList_SelectedIndexChanged); / / btnDelete / this.btnDelete.BackColor = S
20、ystem.Drawing.Color.FromArgb(int)(byte)(192), (int)(byte)(255), (int)(byte)(192); this.btnDelete.ForeColor = System.Drawing.SystemColors.ControlDarkDark; this.btnDelete.Location = new System.Drawing.Point(453, 13); this.btnDelete.Margin = new System.Windows.Forms.Padding(4); this.btnDelete.Name = bt
21、nDelete; this.btnDelete.Size = new System.Drawing.Size(100, 29); this.btnDelete.TabIndex = 3; this.btnDelete.Text = 删除用户; this.btnDelete.UseVisualStyleBackColor = false this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click); / / deleteUser / this.deleteUser.Location = new System.Drawi
22、ng.Point(212, 18); this.deleteUser.Margin = new System.Windows.Forms.Padding(4); this.deleteUser.Name = deleteUser; this.deleteUser.Size = new System.Drawing.Size(203, 25); this.deleteUser.TabIndex = 4; this.deleteUser.TextChanged += new System.EventHandler(this.textBox1_TextChanged); / / btnSaveMes
23、ssage / this.btnSaveMesssage.BackColor = System.Drawing.Color.FromArgb(int)(byte)(255), (int)(byte)(255), (int)(byte)(192); this.btnSaveMesssage.ForeColor = System.Drawing.SystemColors.ControlDarkDark; this.btnSaveMesssage.Location = new System.Drawing.Point(405, 191); this.btnSaveMesssage.Margin =
24、new System.Windows.Forms.Padding(4); this.btnSaveMesssage.Name = btnSaveMesssage; this.btnSaveMesssage.Size = new System.Drawing.Size(148, 29); this.btnSaveMesssage.TabIndex = 5; this.btnSaveMesssage.Text = 保存消息记录; this.btnSaveMesssage.UseVisualStyleBackColor = false; this.btnSaveMesssage.Click += n
25、ew System.EventHandler(this.btnSaveMesssage_Click); / / Form1 / this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.GradientActiveCaption; this.ClientSize = new System.Drawing.Size(58
26、1, 468); this.Controls.Add(this.btnSaveMesssage); this.Controls.Add(this.deleteUser); this.Controls.Add(this.btnDelete); this.Controls.Add(this.clientsList); this.Controls.Add(this.message); this.Controls.Add(this.serverInformation); this.Margin = new System.Windows.Forms.Padding(4); this.Name = For
27、m1; this.Text = 服务器; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); #endregion private System.Windows.Forms.ListBox serverInformation; private System.
28、Windows.Forms.ListBox message; private System.Windows.Forms.ListBox clientsList; private System.Windows.Forms.Button btnDelete; private System.Windows.Forms.TextBox deleteUser; private System.Windows.Forms.Button btnSaveMesssage; From1.csusing System;using System.Collections.Generic;using System.Com
29、ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace udpClients public partial class Form1 : Form public Form1() InitializeComponent();/初始化工作 加载控件资源 分配资源 private void button2_Click(object sender, EventArgs e) user.userName = User
30、Name.Text.ToString(); user.userKey = PassWord.Text.ToString(); this.DialogResult = DialogResult.OK; /表示点击按钮关闭窗口 private void Form1_Load(object sender, EventArgs e)/分配窗口使用资源 private void UserName_TextChanged(object sender, EventArgs e) private void PassWord_TextChanged(object sender, EventArgs e) Pro
31、gram.cs using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace udpClients static class Program / / 应用程序的主入口点。 / STAThread static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form2
32、(); User.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.Net.Socketsnamespace udpClients class user/本地用户类 public static string userName;/y用户名 public static string userKey;/用户密码 public static int myRecPort;/本地接收端口号 聊天:Form2.Designer:na
33、mespace udpClients partial class Form2 / / Required designer variable. / private System.ComponentModel.IContainer components = null;/ / Clean up any resources being used. / / true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) if (disposing
34、 & (components != null) components.Dispose(); base.Dispose(disposing); #region Windows Form Designer generated code / / Required method for Designer support - do not modify / the contents of this method with the code editor. / private void InitializeComponent() System.ComponentModel.ComponentResourc
35、eManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form2); this.btnSend = new System.Windows.Forms.Button(); this.nowUser = new System.Windows.Forms.ListBox(); this.SendMessage = new System.Windows.Forms.RichTextBox(); this.message = new System.Windows.Forms.ListBox(); th
36、is.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); / btnSend / this.btnSend.Location = new System.Drawing.Point(551, 299); this.btnSend.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.btnSend.Name = btnSend; this.btnSend.Size = new System.Drawing.Size(127, 56); this.btnSend.TabIndex = 0; this.btnSend.Text = 发送; this.btnSend.UseVisualStyleBackColor = true;