ImageVerifierCode 换一换
格式:DOCX , 页数:16 ,大小:96.01KB ,
资源ID:3538835      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3538835.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Csharp局域网聊天系统.docx)为本站会员(b****3)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Csharp局域网聊天系统.docx

1、Csharp局域网聊天系统多人聊天(C#.Socket).using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Threading;using System.Net.Sockets;using System.Net;namespace Chat_Server/ / Form1 的摘要说明。/ public class Form1 : System.Windows

2、.Forms.Form/ / 必需的设计器变量。/ private System.ComponentModel.Container components = null;static int listenport=6666;Socket clientsocket;private System.Windows.Forms.ListBox lbClients;ArrayList clients;private System.Windows.Forms.Button button1;Thread clientservice;private System.Windows.Forms.Label labe

3、l1;Thread threadListen;public Form1()InitializeComponent();/ / 清理所有正在使用的资源。/ protected override void Dispose( bool disposing )if( disposing )if(clientservice != null)clientservice.Abort();if(threadListen != null)trythreadListen.Abort();catch(Exception ex)threadListen = null;if (components != null) c

4、omponents.Dispose();base.Dispose( disposing );#region Windows 窗体设计器生成的代码/ / 设计器支持所需的方法 - 不要使用代码编辑器修改/ 此方法的内容。/ private void InitializeComponent()this.lbClients = new System.Windows.Forms.ListBox();this.button1 = new System.Windows.Forms.Button();this.label1 = new System.Windows.Forms.Label();this.Su

5、spendLayout();/ / lbClients/ this.lbClients.ItemHeight = 12;this.lbClients.Location = new System.Drawing.Point(16, 24);this.lbClients.Name = lbClients;this.lbClients.Size = new System.Drawing.Size(184, 268);this.lbClients.TabIndex = 0;/ / button1/ this.button1.Location = new System.Drawing.Point(272

6、, 56);this.button1.Name = button1;this.button1.TabIndex = 1;this.button1.Text = button1;this.button1.Click += new System.EventHandler(this.button1_Click);/ / label1/ this.label1.Location = new System.Drawing.Point(240, 136);this.label1.Name = label1;this.label1.Size = new System.Drawing.Size(120, 32

7、);this.label1.TabIndex = 2;this.label1.Text = label1;/ / Form1/ this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(368, 309);this.Controls.Add(this.label1);this.Controls.Add(this.button1);this.Controls.Add(this.lbClients);this.Name = Form1;this.Text = F

8、orm1;this.Load += new System.EventHandler(this.Form1_Load);this.ResumeLayout(false);#endregion/ / 应用程序的主入口点。/ STAThreadstatic void Main() Application.Run(new Form1();private void StartListening() TcpListener listener = new TcpListener(listenport);listener.Start();label1.Text = listening.;while (true

9、) trySocket s = listener.AcceptSocket(); clientsocket = s; clientservice = new Thread(new ThreadStart(ServiceClient); clientservice.Start(); catch(Exception ex)MessageBox.Show(listening Error: +ex.Message);private void ServiceClient() Socket client = clientsocket; bool keepalive = true; while (keepa

10、live) Byte buffer = new Byte1024; int bufLen = 0;trybufLen = client.Available ;client.Receive(buffer,0,bufLen,SocketFlags.None); if(bufLen=0)continue;catch(Exception ex)MessageBox.Show(Receive Error:+ex.Message);return;string clientcommand = System.Text.Encoding.ASCII.GetString(buffer).Substring(0,b

11、ufLen); string tokens = clientcommand.Split(new Char|); Console.WriteLine(clientcommand); if (tokens0 = CONN) for(int n=0; nclients.Count;n+) Client cl = (Client)clientsn; SendToClient(cl, JOIN| + tokens1); EndPoint ep = client.RemoteEndPoint; Client c = new Client(tokens1, ep, clientservice, client

12、); string message = LIST| + GetChatterList() +/r/n; SendToClient(c, message); clients.Add(c); lbClients.Items.Add(c); if (tokens0 = CHAT) for(int n=0; nclients.Count;n+) Client cl = (Client)clientsn; SendToClient(cl, clientcommand); if (tokens0 = PRIV) string destclient = tokens3; for(int n=0; nclie

13、nts.Count;n+) Client cl = (Client)clientsn; if(cl.Name.CompareTo(tokens3) = 0) SendToClient(cl, clientcommand); if(cl.Name.CompareTo(tokens1) = 0) SendToClient(cl, clientcommand); if (tokens0 = GONE) int remove = 0; bool found = false; int c = clients.Count; for(int n=0; nclients.Count;n+) Client cl

14、 = (Client)clientsn; SendToClient(cl, clientcommand); if(cl.Name.CompareTo(tokens1) = 0) remove = n; found = true; lbClients.Items.Remove(cl); if(found) clients.RemoveAt(remove); client.Close(); keepalive = false; private string GetChatterList()string result = ;for(int i=0;iclients.Count;i+)result +

15、= (Client)clientsi).Name+|;return result;private void SendToClient(Client cl,string clientCommand)Byte message = System.Text.Encoding.ASCII.GetBytes(clientCommand);Socket s = cl.Sock;if(s.Connected)s.Send(message,message.Length,0);private void Form1_Load(object sender, System.EventArgs e)clients = n

16、ew ArrayList();private void button1_Click(object sender, System.EventArgs e)threadListen = new Thread(new ThreadStart(StartListening);threadListen.Start(); /* client类 */* 放于 chatServer 项目中 */using System; using System.Threading; namespace Chat_Server using System.Net.Sockets; using System.Net; / / C

17、lient 的摘要说明。 / public class Client private Thread clthread; private EndPoint endpoint; private string name; private Socket sock; public Client(string _name, EndPoint _endpoint, Thread _thread, Socket _sock) / TODO: 在此处添加构造函数逻辑 clthread = _thread; endpoint = _endpoint; name = _name; sock = _sock; pub

18、lic override string ToString() return endpoint.ToString()+ : + name; public Thread CLThread getreturn clthread; setclthread = value; public EndPoint Host getreturn endpoint; setendpoint = value; public string Name getreturn name; setname = value; public Socket Sock getreturn sock; setsock = value; /

19、* chatClient */using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.IO;using System.Net;using System.Net.Sockets;using System.Threading;namespace Chat_Client/ / Form1 的摘要说明。/ public class Form1 : System.Windo

20、ws.Forms.Formprivate System.Windows.Forms.CheckBox checkBox1;private System.Windows.Forms.StatusBar statusBar1;NetworkStream ns;StreamReader sr;TcpClient clientsocket;bool connected;Thread receive;string serveraddress = 219.228.231.85;int serverport = 6666;private System.Windows.Forms.RichTextBox rt

21、bChatIn;private System.Windows.Forms.ListBox lbChatters;private System.Windows.Forms.TextBox ChatOut;private System.Windows.Forms.Button btnDisconnect;private System.Windows.Forms.Button btnSend;private System.Windows.Forms.TextBox clientName;string clientname;private System.Windows.Forms.Button btn

22、Connect;private System.ComponentModel.Container components = null;public Form1()InitializeComponent();/ / 清理所有正在使用的资源。/ protected override void Dispose( bool disposing )if( disposing )if(receive != null)QuitChat();if (components != null) components.Dispose();base.Dispose( disposing );#region Windows

23、 窗体设计器生成的代码/ / 设计器支持所需的方法 - 不要使用代码编辑器修改/ 此方法的内容。/ private void InitializeComponent()this.lbChatters = new System.Windows.Forms.ListBox();this.rtbChatIn = new System.Windows.Forms.RichTextBox();this.checkBox1 = new System.Windows.Forms.CheckBox();this.ChatOut = new System.Windows.Forms.TextBox();this

24、.btnSend = new System.Windows.Forms.Button();this.statusBar1 = new System.Windows.Forms.StatusBar();this.btnDisconnect = new System.Windows.Forms.Button();this.clientName = new System.Windows.Forms.TextBox();this.btnConnect = new System.Windows.Forms.Button();this.SuspendLayout();/ / lbChatters/ this.lbChatters.ItemHeight = 12;this.lbChatters.Location = new System.Drawing.Point(32, 40);this.lbChatters.Name = lbChatters;this.lbChatters.Size = new System.Drawing.Size(112, 172);this.lbChatters.TabIndex = 0;/ / rt

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

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