資源簡介
C# tcp 多線程聊天室 C# tcp 多線程聊天室C# tcp 多線程聊天室C# tcp 多線程聊天室C# tcp 多線程聊天室C# tcp 多線程聊天室
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Text;
using?System.Threading;
using?System.Net.Sockets;
using?System.IO;
using?FeiMsgType;
namespace?Fei
{
????///?
????///?和客戶端連接的通道類(內含Socket及ClientUser)
????///?
????class?ClientConnection
????{
????????#region?連接通道所屬用戶信息?-?User
????????ClientUser?user?=?null;
????????///?
????????///?連接通道所屬用戶信息
????????///?
????????public?ClientUser?User
????????{
????????????get?{?return?user;?}
????????????set?{?user?=?value;?}
????????}?
????????#endregion
????????Thread?threadClient?=?null;
????????Socket?socket?=?null;
????????string?strEndpoint;
????????FrmMain?frmMain?=?null;
????????///?
????????///?是否退出-true退出/false運行
????????///?
????????bool?doesClose?=?false;
????????#region?構造函數
????????public?ClientConnection(FrmMain?frmMain?Socket?socket)
????????{
????????????InitConnection(frmMain?socket);
????????}
????????public?ClientConnection(FrmMain?frmMain?Socket?socket?ClientUser?user)
????????{
????????????InitConnection(frmMain?socket);
????????????this.user?=?user;
????????}?
????????#endregion
????????#region?初始化連接?-?InitConnection(FrmMain?frmMain?Socket?socket)
????????///?
????????///?初始化連接
????????///?
????????///?窗體
????????///?連接套接字
????????public?void?InitConnection(FrmMain?frmMain?Socket?socket)
????????{
????????????if?(null?==?frmMain?||?null?==?socket)?throw?new?UnPrepareConnectionException();//如果參數不足則拋出自定義異常
????????????this.frmMain?=?frmMain;
????????????this.socket?=?socket;
????????????this.strEndpoint?=?socket.RemoteEndPoint.ToString();
????????????threadClient?=?new?Thread(WatchMsg);
????????????threadClient.IsBackground?=?true;
????????????threadClient.Start();
????????}?
????????#endregion
????????#region?-監聽客戶端消息?-?WatchMsg()
????????private?void?WatchMsg()
????????{
????????????string?strMsgRec?=?string.Empty;
????????????while?(!doesClose)
????????????{
????????????????try
????????????????{
????????????????????byte[]?byteMsgRec?=?new?byte[1024?*?1024?*?4];
????????????????????int?length?=?socket.Receive(byteMsgRec?byteMsgRec.Length?SocketFlags.None);
????????????????????if?(length?>?0)
????????????????????{
????????????????????????strMsgRec?=?Encoding.UTF8.GetString(byteMsgRec?1?length?-?1);//獲取標志符后的消息字符串
????????????????????????if?(byteMsgRec[0]?==?(byte)MsgType.UserMsg)//用戶消息
????????????????????????{
????????????????????????????MsgInfo?msgInfo?=?new?MsgInfo(strMsgRec);
????????????????????????????if?(msgInfo.UId?!=?string.Empty)//有效消息
????????????????????????????{
????????????????????????????????if?(msgInfo.ToUid?!=?string.Empty?&&?msgInfo.IsPrivate)//如果接收人不為空而且是私聊信息
????????????????????????????????{
????????????????????????????????????frmMain.SendMsgUserToSingle(msgInfo.ToUid?msgInfo.ToTransString()?MsgType.UserMsg);
??????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????1890??2011-04-12?10:18??ChatRoom.sln
????..A..H.?????41472??2011-04-14?14:20??ChatRoom.suo
?????文件??????40960??2011-04-14?13:54??Fei\bin\Debug\Fei.exe
?????文件??????58880??2011-04-14?13:54??Fei\bin\Debug\Fei.pdb
?????文件???????5632??2005-11-11?22:25??Fei\bin\Debug\Fei.vshost.exe
?????文件??????20480??2011-04-12?10:18??Fei\bin\Debug\FeiMsgType.dll
?????文件??????32256??2011-04-12?10:18??Fei\bin\Debug\FeiMsgType.pdb
?????文件??????11028??2011-04-10?19:45??Fei\ClientConnection.cs
?????文件???????3547??2011-04-10?19:39??Fei\Fei.csproj
?????文件??????18760??2011-04-12?10:36??Fei\FrmMain.cs
?????文件??????31576??2011-04-11?08:24??Fei\FrmMain.Designer.cs
?????文件???????5814??2011-04-11?08:24??Fei\FrmMain.resx
?????文件????????844??2011-04-12?10:18??Fei\obj\Debug\Fei.csproj.GenerateResource.Cache
?????文件??????40960??2011-04-14?13:54??Fei\obj\Debug\Fei.exe
?????文件????????180??2011-04-12?10:18??Fei\obj\Debug\Fei.FrmMain.resources
?????文件??????58880??2011-04-14?13:54??Fei\obj\Debug\Fei.pdb
?????文件????????180??2011-04-12?10:18??Fei\obj\Debug\Fei.Properties.Resources.resources
?????文件???????2570??2011-04-12?10:18??Fei\obj\Debug\ResolveAssemblyReference.cache
?????文件???????1548??2011-04-14?14:15??Fei\obj\Fei.csproj.FileListAbsolute.txt
?????文件????????464??2011-04-02?18:13??Fei\Program.cs
?????文件???????1166??2011-04-02?18:03??Fei\Properties\AssemblyInfo.cs
?????文件???????2862??2011-04-02?18:03??Fei\Properties\Resources.Designer.cs
?????文件???????5612??2011-04-02?18:03??Fei\Properties\Resources.resx
?????文件???????1088??2011-04-02?18:03??Fei\Properties\Settings.Designer.cs
?????文件????????249??2011-04-02?18:03??Fei\Properties\Settings.settings
?????文件????????422??2011-04-07?23:22??Fei\UnPrepareConnectionException.cs
?????文件??????20480??2011-04-12?10:18??FeiMsgType\bin\Debug\FeiMsgType.dll
?????文件??????32256??2011-04-12?10:18??FeiMsgType\bin\Debug\FeiMsgType.pdb
?????文件???????3777??2011-04-10?20:13??FeiMsgType\ClientUser.cs
?????文件???????2240??2011-04-10?20:06??FeiMsgType\FeiMsgType.csproj
............此處省略68個文件信息
- 上一篇:C# 城市公交查詢系統
- 下一篇:C#做的QQ仿QQ
評論
共有 條評論