資源簡介
用c#實現的tcp局域網聊天程序,用到了NetworkStream,tcpclient,tcplistener等這些類實現,程序中還有狀態欄,可是顯示當先狀態!
另外還增加了對異常的處理,使程序更加健壯!!!

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Text;
using?System.Windows.Forms;
using?System.Net;
using?System.Net.Sockets;
using?System.Threading;
namespace?MyTcpClient
{
????public?partial?class?Form1?:?Form
????{
????????private?bool?isExit?=?false;
????????System.Collections.ArrayList?clientlist?=?new?System.Collections.ArrayList();
????????TcpListener?listener;
????????private?delegate?void?SetListBoxCallBack(string?str);
????????private?SetListBoxCallBack?setlistcallback;
????????private?delegate?void?SetRichTextBoxCallBack(string?str);
????????private?SetRichTextBoxCallBack?setRichTextBoxCallBack;
????????private?delegate?void?SetComboBoxCallBack(string?str);
????????private?SetComboBoxCallBack?setComboBoxCallBack;
????????private?delegate?void?RemoveComboBoxItemsCallBack(DataReadWrite?datareadwrite);
????????private?RemoveComboBoxItemsCallBack?removecomboboxcallback;
????????private?ManualResetEvent?allDone?=?new?ManualResetEvent(false);
????????public?Form1()
????????{
????????????InitializeComponent();
????????????setlistcallback?=?new?SetListBoxCallBack(SetListBox);
????????????setRichTextBoxCallBack?=?new?SetRichTextBoxCallBack(SetReceiveText);
????????????setComboBoxCallBack?=?new?SetComboBoxCallBack(SetComboBox);
????????????removecomboboxcallback?=?new?RemoveComboBoxItemsCallBack(RemoveComboBoxItems);
????????}
????????private?void?buttonstart_Click(object?sender?EventArgs?e)
????????{
????????????Thread?myThread=new?Thread(new?ThreadStart(AcceptConnection));
????????????myThread.Start();
????????????buttonstart.Enabled=false;
????????????buttonstop.Enabled=true;
????????}
????????private?void?AcceptConnection()
????????{
????????????IPAddress[]?ip?=?Dns.GetHostAddresses(Dns.GetHostName());
????????????listener?=?new?TcpListener(ip[0]?9001);
????????????listener.Start();
????????????while?(isExit?==?false)
????????????{
????????????????try
????????????????{
????????????????????allDone.Reset();
????????????????????AsyncCallback?callback?=?new?AsyncCallback(AcceptTcpClientCallBack);
????????????????????listBoxStatus.Invoke(setlistcallback?“開始等待連接“);
????????????????????listener.BeginAcceptTcpClient(callback?listener);
????????????????????allDone.WaitOne();
????????????????}
????????????????catch?(Exception?e)
????????????????{
????????????????????listBoxStatus.Invoke(setlistcallback?e.Message);
????????????????????break;
????????????????}
????????????????finally
????????????????{
????????????????}
????????????}
????????}
????????private?void?AcceptTcpClientCallBack(IAsyncResult?iar)
????????{
????????????try
????????????{
????????????????TcpListener?listener?=?(TcpListener)iar.AsyncState;
????????????????TcpClient?client?=?listener.EndAcceptTcpClient(iar);
????????????????listBoxStatus.Invoke(setlistcallback?“已接收客戶連接:“?+?client.Client.RemoteEndPoint);
???????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????24576??2009-03-01?19:25??tcp聊天程序\MyTcpClient\MyTcpClient\bin\Debug\MyTcpClient.exe
?????文件??????28160??2009-03-01?19:25??tcp聊天程序\MyTcpClient\MyTcpClient\bin\Debug\MyTcpClient.pdb
?????文件???????5632??2005-11-11?22:25??tcp聊天程序\MyTcpClient\MyTcpClient\bin\Debug\MyTcpClient.vshost.exe
?????文件???????7914??2009-03-01?19:17??tcp聊天程序\MyTcpClient\MyTcpClient\Form1.cs
?????文件???????8455??2009-03-01?18:28??tcp聊天程序\MyTcpClient\MyTcpClient\Form1.Designer.cs
?????文件???????5814??2009-03-01?18:28??tcp聊天程序\MyTcpClient\MyTcpClient\Form1.resx
?????文件???????3229??2009-03-01?18:28??tcp聊天程序\MyTcpClient\MyTcpClient\MyTcpClient.csproj
?????文件????????842??2009-03-01?19:25??tcp聊天程序\MyTcpClient\MyTcpClient\obj\Debug\MyTcpClient.csproj.GenerateResource.Cache
?????文件??????24576??2009-03-01?19:25??tcp聊天程序\MyTcpClient\MyTcpClient\obj\Debug\MyTcpClient.exe
?????文件????????180??2009-03-01?19:25??tcp聊天程序\MyTcpClient\MyTcpClient\obj\Debug\MyTcpClient.Form1.resources
?????文件??????28160??2009-03-01?19:25??tcp聊天程序\MyTcpClient\MyTcpClient\obj\Debug\MyTcpClient.pdb
?????文件????????180??2009-03-01?19:25??tcp聊天程序\MyTcpClient\MyTcpClient\obj\Debug\MyTcpClient.Properties.Resources.resources
?????文件????????296??2009-03-01?19:26??tcp聊天程序\MyTcpClient\MyTcpClient\obj\MyTcpClient.csproj.FileList.txt
?????文件????????470??2009-03-01?16:55??tcp聊天程序\MyTcpClient\MyTcpClient\Program.cs
?????文件???????1206??2009-03-01?16:55??tcp聊天程序\MyTcpClient\MyTcpClient\Properties\AssemblyInfo.cs
?????文件???????2877??2009-03-01?16:55??tcp聊天程序\MyTcpClient\MyTcpClient\Properties\Resources.Designer.cs
?????文件???????5612??2009-03-01?16:55??tcp聊天程序\MyTcpClient\MyTcpClient\Properties\Resources.resx
?????文件???????1095??2009-03-01?16:55??tcp聊天程序\MyTcpClient\MyTcpClient\Properties\Settings.Designer.cs
?????文件????????249??2009-03-01?16:55??tcp聊天程序\MyTcpClient\MyTcpClient\Properties\Settings.settings
?????文件????????922??2009-03-01?16:55??tcp聊天程序\MyTcpClient\MyTcpClient.sln
????..A..H.?????12288??2009-03-01?19:27??tcp聊天程序\MyTcpClient\MyTcpClient.suo
?????文件??????24576??2009-03-01?19:24??tcp聊天程序\MyTcpServer\MyTcpServer\bin\Debug\MyTcpServer.exe
?????文件??????24064??2009-03-01?19:24??tcp聊天程序\MyTcpServer\MyTcpServer\bin\Debug\MyTcpServer.pdb
?????文件???????5632??2005-11-11?22:25??tcp聊天程序\MyTcpServer\MyTcpServer\bin\Debug\MyTcpServer.vshost.exe
?????文件???????5013??2009-03-01?19:16??tcp聊天程序\MyTcpServer\MyTcpServer\Form1.cs
?????文件???????6593??2009-03-01?18:29??tcp聊天程序\MyTcpServer\MyTcpServer\Form1.Designer.cs
?????文件???????5814??2009-03-01?18:29??tcp聊天程序\MyTcpServer\MyTcpServer\Form1.resx
?????文件????????474??2009-03-01?16:18??tcp聊天程序\MyTcpServer\MyTcpServer\MyTcpServer.cs
?????文件???????3233??2009-03-01?16:36??tcp聊天程序\MyTcpServer\MyTcpServer\MyTcpServer.csproj
?????文件????????558??2009-03-01?16:36??tcp聊天程序\MyTcpServer\MyTcpServer\MyTcpServer.csproj.user
............此處省略35個文件信息
評論
共有 條評論