資源簡介
用c#寫了一個比較健壯的多人聊天室 可以多個人一起聊天

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?System.Net;
using?System.Threading;
using?System.Net.Sockets;
namespace?客戶端
{
????public?partial?class?Form1?:?Form
????{
????????Socket?lian;
????????public?Form1()
????????{?
????????????InitializeComponent();
????????}
????????///?
????????///?不停接收信息的線程
????????///?
????????void?jieshou()
????????{
????????????while?(true)
????????????{
????????????????Byte[]?buff?=?new?Byte[1000];
????????????????try
????????????????{
????????????????????int?s?=?lian.Receive(buff);
????????????????????if?(s?==?0)
????????????????????????break;
????????????????????string?str?=?Encoding.UTF8.GetString(buff?0?s);
????????????????????this.Invoke(new?Action(()?=>
????????????????????{
????????????????????????this.neirong.AppendText(str?+?“\r\n“);
????????????????????}));
????????????????}
????????????????catch?{?}
????????????}
????????}
????????///?
????????///?發送消息
????????///?
????????///?
????????///?
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????if?(this.button2.Text?!=?“關閉“)
????????????{
????????????????MessageBox.Show(“請先連接“);
????????????????return;
????????????}
????????????string?str?=?this.liaotian.Text;
????????????string?strr?=?this.name.Text?+?“:“?+?str;
????????????byte[]?buf?=?Encoding.UTF8.GetBytes(strr);
????????????try
????????????{
????????????????lian.Send(buf);
????????????}
????????????catch
????????????{
????????????????//服務器關閉?或者發生異常?斷開連接
????????????????lian.Close();
????????????????MessageBox.Show(“請重新連接“);
????????????????this.button2.Text?=?“連接“;
????????????????return;
????????????}
????????????this.liaotian.Clear();
????????????this.neirong.AppendText(“我:“?+?str?+?“\r\n“);
????????}
????????///?
????????///?點第一次連接??第二次關閉連接?
????????///?
????????///?
????????///?
????????private?void?button2_Click(object?sender?EventArgs?e)
????????{
????????????if?(this.button2.Text?==?“關閉“)
????????????{
????????????????lian.Close();
????????????????this.button2.Text?=?“連接“;
????????????????return;
????????????}
????????????try
????????????{
????????????????//創建負責通信的Socket
????????????????lian?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????????IPAddress?ip?=?IPAddress.Parse(“127.0.0.1“);
????????????????IPEndPoint?point?=?new?IPEndPoint(ip?3333);
????????????????//獲得要連接的遠程服務器應用程序的IP地址和端口號
????????????????lian.Connect(point);
????????????????//開啟一個新的線程不停的接收服務端發來的消息
????????????????Thread?t?=?new?Thread(jieshou);
????????????????t.IsBackground?=?true;
????????????????t.Start();
????????????????this.button2.Text?=?“關閉“;
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????10752??2017-08-20?17:34??聊天室\客戶端\bin\Debug\客戶端.exe
?????文件??????26112??2017-08-20?17:34??聊天室\客戶端\bin\Debug\客戶端.pdb
?????文件??????14328??2017-08-20?17:32??聊天室\客戶端\bin\Debug\客戶端.vshost.exe
?????文件????????490??2009-06-11?05:14??聊天室\客戶端\bin\Debug\客戶端.vshost.exe.manifest
?????文件???????3324??2017-08-20?17:34??聊天室\客戶端\Form1.cs
?????文件???????5089??2017-08-20?14:39??聊天室\客戶端\Form1.Designer.cs
?????文件???????5814??2017-08-20?14:39??聊天室\客戶端\Form1.resx
?????文件????????722??2017-08-20?17:32??聊天室\客戶端\obj\Debug\客戶端.csproj.FileListAbsolute.txt
?????文件????????847??2017-08-20?14:39??聊天室\客戶端\obj\Debug\客戶端.csproj.GenerateResource.Cache
?????文件??????10752??2017-08-20?17:34??聊天室\客戶端\obj\Debug\客戶端.exe
?????文件????????180??2017-08-20?14:39??聊天室\客戶端\obj\Debug\客戶端.Form1.resources
?????文件??????26112??2017-08-20?17:34??聊天室\客戶端\obj\Debug\客戶端.pdb
?????文件????????180??2017-08-19?16:09??聊天室\客戶端\obj\Debug\客戶端.Properties.Resources.resources
?????文件????????490??2017-08-19?15:53??聊天室\客戶端\Program.cs
?????文件???????1350??2017-08-19?15:53??聊天室\客戶端\Properties\AssemblyInfo.cs
?????文件???????2868??2017-08-19?15:53??聊天室\客戶端\Properties\Resources.Designer.cs
?????文件???????5612??2017-08-19?15:53??聊天室\客戶端\Properties\Resources.resx
?????文件???????1094??2017-08-19?15:53??聊天室\客戶端\Properties\Settings.Designer.cs
?????文件????????249??2017-08-19?15:53??聊天室\客戶端\Properties\Settings.settings
?????文件???????3764??2017-08-19?16:09??聊天室\客戶端\客戶端.csproj
?????文件??????10752??2017-08-20?15:12??聊天室\聊天室\bin\Debug\聊天室.exe
?????文件??????28160??2017-08-20?15:12??聊天室\聊天室\bin\Debug\聊天室.pdb
?????文件??????14328??2017-08-19?16:10??聊天室\聊天室\bin\Debug\聊天室.vshost.exe
?????文件????????490??2009-06-11?05:14??聊天室\聊天室\bin\Debug\聊天室.vshost.exe.manifest
?????文件???????4855??2017-08-20?15:12??聊天室\聊天室\Form1.cs
?????文件???????4361??2017-08-19?17:27??聊天室\聊天室\Form1.Designer.cs
?????文件???????5814??2017-08-19?17:27??聊天室\聊天室\Form1.resx
?????文件????????722??2017-08-20?15:01??聊天室\聊天室\obj\Debug\聊天室.csproj.FileListAbsolute.txt
?????文件????????847??2017-08-19?17:27??聊天室\聊天室\obj\Debug\聊天室.csproj.GenerateResource.Cache
?????文件??????10752??2017-08-20?15:12??聊天室\聊天室\obj\Debug\聊天室.exe
............此處省略32個文件信息
評論
共有 條評論