创建局域网聊天软件.doc_第1页
创建局域网聊天软件.doc_第2页
创建局域网聊天软件.doc_第3页
创建局域网聊天软件.doc_第4页
创建局域网聊天软件.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

实验八 创建局域网聊天软件【实验目的】1 通过创建局域网聊天软件,让学生掌握如何在.net下通过socket类实现tcp和udp通信。【实验要求】1 写出程序,并调试程序,要给出测试数据和实验结果。2 整理上机步骤,总结经验和体会。3 完成上交程序。【案例分析】为实现网络聊天的功能,采用Windows Socket编程,服务器与客户端采用了TCP/IP连接方式,在设计聊天方案时,实行将所有信息发往服务器端,再由服务器进行分别处理的思路,服务器端是所有信息的中心。由于服务器端要保存用户信息,我们利用数据库来实现这一功能,因此首先需要建立用户信息数据库。在信息到来及好友上线时,通过闪动托盘图标和播放不同的音乐进行提示。建立消息链表来保存用户接收的各种消息。登陆到服务器客户端申请新的号码显示在线用户接收各种信息服务器端处理申请信息转发聊天信息处理登陆信息发布系统消息1、 通信服务端服务端代码如下: C# Code 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 using System.Net; 10 using System.Net.Sockets; 11 using System.Threading; 12 using System.Xml; 13 14 namespace Server 15 16 public partial class ServerMain : Form 17 18 public ServerMain() 19 20 InitializeComponent(); 21 22 23 private void ServerMain_Load(object sender, EventArgs e) 24 25 this.CmdStar.Enabled = true; 26 this.CmdStop.Enabled = false; 27 28 29 private void 配置参数ToolStripMenuItem_Click(object sender, EventArgs e) 30 31 Set TSet = new Set(); 32 TSet.ShowDialog(); 33 34 35 private void 关于ToolStripMenuItem_Click(object sender, EventArgs e) 36 37 About TAbout = new About(); 38 TAbout.Show(); 39 40 / 41 / 获得XML文件中的端口号 42 / 43 / 44 private int GetPort() 45 46 try 47 48 XmlDocument TDoc = new XmlDocument(); 49 TDoc.Load(Settings.xml); 50 string TPort = TDoc.GetElementsByTagName(ServerPort)0.InnerXml; 51 return Convert.ToInt32(TPort); 52 53 54 catch return 6600; /默认是6600 55 56 57 /声明将要用到的类 58 private IPEndPoint ServerInfo;/存放服务器的IP和端口信息 59 private Socket ServerSocket;/服务端运行的SOCKET 60 private Thread ServerThread;/服务端运行的线程 61 private Socket ClientSocket;/为客户端建立的SOCKET连接 62 private int ClientNumb;/存放客户端数量 63 private byte MsgBuffer;/存放消息数据 64 65 private void CmdStar_Click(object sender, EventArgs e) 66 67 ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 68 ServerInfo=new IPEndPoint(IPAddress.Any,this.GetPort(); 69 ServerSocket.Bind(ServerInfo);/将SOCKET接口和IP端口绑定 70 ServerSocket.Listen(10);/开始监听,并且挂起数为10 71 72 ClientSocket = new Socket65535;/为客户端提供连接个数 73 MsgBuffer = new byte65535;/消息数据大小 74 ClientNumb = 0;/数量从0开始统计 75 76 ServerThread = new Thread(RecieveAccept);/将接受客户端连接的方法委托给线程 77 ServerThread.Start();/线程开始运行 78 79 CheckForIllegalCrossThreadCalls = false;/不捕获对错误线程的调用 80 81 this.CmdStar.Enabled = false; 82 this.CmdStop.Enabled = true; 83 this.StateMsg.Text = 服务正在运行 运行端口: this.GetPort().ToString(); 84 this.ClientList.Items.Add(服务于 DateTime.Now.ToString() 开始运行.); 85 86 87 /接受客户端连接的方法 88 private void RecieveAccept() 89 90 while (true) 91 92 ClientSocketClientNumb = ServerSocket.Accept(); 93 ClientSocketClientNumb.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(RecieveCallBack),ClientSocketClientNumb); 94 this.ClientList.Items.Add(ClientSocketClientNumb.RemoteEndPoint.ToString() 成功连接服务器.); 95 ClientNumb ; 96 97 98 99 /回发数据给客户端100 private void RecieveCallBack(IAsyncResult AR)101 102 try103 104 Socket RSocket = (Socket)AR.AsyncState;105 int REnd = RSocket.EndReceive(AR);106 for (int i = 0; i ClientNumb; i )107 108 if (ClientSocketi.Connected)109 110 ClientSocketi.Send(MsgBuffer, 0, REnd,0);111 112 RSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(RecieveCallBack), RSocket);113 114 115 116 catch 117 118 119 120 private void CmdStop_Click(object sender, EventArgs e)121 122 ServerThread.Abort();/线程终止123 ServerSocket.Close();/关闭SOCKET124 125 this.CmdStar.Enabled = true;126 this.CmdStop.Enabled = false;127 this.StateMsg.Text = 等待运行;128 this.ClientList.Items.Add(服务于 DateTime.Now.ToString() 停止运行.);129 130 131 132 133 134 2、 通信客户端界面客户端代码: C# Code 1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 9 using System.Net; 10 using System.Net.Sockets; 11 12 namespace Client 13 14 public partial class ClientMain : Form 15 16 public ClientMain() 17 18 InitializeComponent(); 19 20 21 private IPEndPoint ServerInfo; 22 private Socket ClientSocket; 23 private Byte MsgBuffer; 24 private Byte MsgSend; 25 26 private void ClientMain_Load(object sender, EventArgs e) 27 28 this.CmdSend.Enabled = false; 29 this.CmdExit.Enabled = false; 30 31 ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 32 MsgBuffer = new Byte65535; 33 MsgSend = new Byte65535; 34 CheckForIllegalCrossThreadCalls = false; 35 36 Random TRand=new Random(); 37 this.UserName.Text = 用户 TRand.Next(10000).ToString(); 38 39 40 private void CmdEnter_Click(object sender, EventArgs e) 41 42 ServerInfo = new IPEndPoint(IPAddress.Parse(this.ServerIP.Text), Convert.ToInt32(this.ServerPort.Text); 43 44 try 45 46 ClientSocket.Connect(ServerInfo); 47 48 ClientSocket.Send(Encoding.Unicode.GetBytes(用户: this.UserName.Text 进入系统!n); 49 50 ClientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null); 51 52 this.SysMsg.Text = 登录服务器成功!n; 53 this.CmdSend.Enabled = true; 54 this.CmdEnter.Enabled = false; 55 this.CmdExit.Enabled = true; 56 57 catch 58 59 MessageBox.Show(登录服务器失败,请确认服务器是否正常工作!); 60 61 62 63 private void ReceiveCallBack(IAsyncResult AR) 64 65 try 66 67 int REnd = ClientSocket.EndReceive(AR); 68 this.RecieveMsg.AppendText(Encoding.Unicode.GetString(MsgBuffer, 0, REnd); 69 ClientSocket.BeginReceive(MsgBuffer, 0, MsgBuffer.Length, 0, new AsyncCallback(ReceiveCallBack), null); 70 71 72 catch 73 74 MessageBox.Show(已经与服务器断开连接!); 75 this.Close(); 76 77 78 79 80 private void CmdSend_Click(object sender, EventArgs e) 81 82 MsgSend = Encoding.Unicode.GetBytes(this.UserName.Text 说:n this.SendMsg.Text n); 83 if (ClientSocket.Connected) 84 85 ClientSocket.Send(MsgSend); 86 this.SendMsg.Text = ; 87 88 else 89 90 MessageBox.Show(当前与服务器断开连接,无法发送信息!); 91 92 93 94 private void CmdExit_Click(object sender

温馨提示

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

评论

0/150

提交评论