Csharp局域网聊天系统Word格式.docx
- 文档编号:16444172
- 上传时间:2022-11-23
- 格式:DOCX
- 页数:16
- 大小:96.01KB
Csharp局域网聊天系统Word格式.docx
《Csharp局域网聊天系统Word格式.docx》由会员分享,可在线阅读,更多相关《Csharp局域网聊天系统Word格式.docx(16页珍藏版)》请在冰豆网上搜索。
ArrayListclients;
privateSystem.Windows.Forms.Buttonbutton1;
Threadclientservice;
privateSystem.Windows.Forms.Labellabel1;
ThreadthreadListen;
publicForm1()
InitializeComponent();
}
///清理所有正在使用的资源。
protectedoverridevoidDispose(booldisposing)
if(disposing)
if(clientservice!
=null)
clientservice.Abort();
if(threadListen!
try
threadListen.Abort();
catch(Exceptionex)
threadListen=null;
}
if(components!
=null)
components.Dispose();
base.Dispose(disposing);
#regionWindows窗体设计器生成的代码
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
privatevoidInitializeComponent()
this.lbClients=newSystem.Windows.Forms.ListBox();
this.button1=newSystem.Windows.Forms.Button();
this.label1=newSystem.Windows.Forms.Label();
this.SuspendLayout();
//
//lbClients
this.lbClients.ItemHeight=12;
this.lbClients.Location=newSystem.Drawing.Point(16,24);
this.lbClients.Name="
lbClients"
;
this.lbClients.Size=newSystem.Drawing.Size(184,268);
this.lbClients.TabIndex=0;
//button1
this.button1.Location=newSystem.Drawing.Point(272,56);
this.button1.Name="
button1"
this.button1.TabIndex=1;
this.button1.Text="
this.button1.Click+=newSystem.EventHandler(this.button1_Click);
//label1
this.label1.Location=newSystem.Drawing.Point(240,136);
this.label1.Name="
label1"
this.label1.Size=newSystem.Drawing.Size(120,32);
this.label1.TabIndex=2;
this.label1.Text="
//Form1
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(368,309);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.lbClients);
this.Name="
Form1"
this.Text="
this.Load+=newSystem.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
#endregion
///应用程序的主入口点。
[STAThread]
staticvoidMain()
Application.Run(newForm1());
privatevoidStartListening()
{
TcpListenerlistener=newTcpListener(listenport);
listener.Start();
label1.Text="
listening...."
while(true)
Sockets=listener.AcceptSocket();
clientsocket=s;
clientservice=newThread(newThreadStart(ServiceClient));
clientservice.Start();
MessageBox.Show("
listeningError:
"
+ex.Message);
privatevoidServiceClient()
Socketclient=clientsocket;
boolkeepalive=true;
while(keepalive)
Byte[]buffer=newByte[1024];
intbufLen=0;
bufLen=client.Available;
client.Receive(buffer,0,bufLen,SocketFlags.None);
if(bufLen==0)
continue;
ReceiveError:
"
return;
stringclientcommand=System.Text.Encoding.ASCII.GetString(buffer).Substring(0,bufLen);
string[]tokens=clientcommand.Split(newChar[]{'
|'
});
Console.WriteLine(clientcommand);
if(tokens[0]=="
CONN"
)
for(intn=0;
n<
clients.Count;
n++)
Clientcl=(Client)clients[n];
SendToClient(cl,"
JOIN|"
+tokens[1]);
EndPointep=client.RemoteEndPoint;
Clientc=newClient(tokens[1],ep,clientservice,client);
stringmessage="
LIST|"
+GetChatterList()+"
/r/n"
SendToClient(c,message);
clients.Add(c);
lbClients.Items.Add(c);
CHAT"
SendToClient(cl,clientcommand);
PRIV"
stringdestclient=tokens[3];
if(cl.Name.CompareTo(tokens[3])==0)
if(cl.Name.CompareTo(tokens[1])==0)
}
GONE"
intremove=0;
boolfound=false;
intc=clients.Count;
remove=n;
found=true;
lbClients.Items.Remove(cl);
if(found)
clients.RemoveAt(remove);
client.Close();
keepalive=false;
privatestringGetChatterList()
stringresult="
for(inti=0;
i<
i++)
result+=((Client)clients[i]).Name+"
|"
returnresult;
privatevoidSendToClient(Clientcl,stringclientCommand)
Byte[]message=System.Text.Encoding.ASCII.GetBytes(clientCommand);
Sockets=cl.Sock;
if(s.Connected)
s.Send(message,message.Length,0);
privatevoidForm1_Load(objectsender,System.EventArgse)
clients=newArrayList();
privatevoidbutton1_Click(objectsender,System.EventArgse)
threadListen=newThread(newThreadStart(StartListening));
threadListen.Start();
/*****************************client类********************/
/**************************放于chatServer项目中*********/
namespaceChat_Server
///
///Client的摘要说明。
publicclassClient
privateThreadclthread;
privateEndPointendpoint;
privatestringname;
privateSocketsock;
publicClient(string_name,EndPoint_endpoint,Thread_thread,Socket_sock)
//TODO:
在此处添加构造函数逻辑
clthread=_thread;
endpoint=_endpoint;
name=_name;
sock=_sock;
publicoverridestringToString()
returnendpoint.ToString()+"
:
+name;
publicThreadCLThread
get{returnclthread;
set{clthread=value;
publicEndPointHost
get{returnendpoint;
set{endpoint=value;
publicstringName
get{returnname;
set{name=value;
publicSocketSock
get{returnsock;
set{sock=value;
/*****************************chatClient************************************/
usingSystem.IO;
namespaceChat_Client
privateSystem.Windows.Forms.CheckBoxcheckBox1;
privateSystem.Windows.Forms.StatusBarstatusBar1;
NetworkStreamns;
StreamReadersr;
TcpClientclientsocket;
boolconnected;
Threadreceive;
stringserveraddress="
219.228.231.85"
intserverport=6666;
privateSystem.Windows.Forms.RichTextBoxrtbChatIn;
privateSystem.Windows.Forms.ListBoxlbChatters;
privateSystem.Windows.Forms.TextBoxChatOut;
privateSystem.Windows.Forms.ButtonbtnDisconnect;
privateSystem.Windows.Forms.ButtonbtnSend;
privateSystem.Windows.Forms.TextBoxclientName;
stringclientname;
privateSystem.Windows.Forms.ButtonbtnConnect;
if(receive!
QuitChat();
this.lbChatters=newSystem.Windows.Forms.ListBox();
this.rtbChatIn=newSystem.Windows.Forms.RichTextBox();
this.checkBox1=newSystem.Windows.Forms.CheckBox();
this.ChatOut=newSystem.Windows.Forms.TextBox();
this.btnSend=newSystem.Windows.Forms.Button();
this.statusBar1=newSystem.Windows.Forms.StatusBar();
this.btnDisconnect=newSystem.Windows.Forms.Button();
this.clientName=newSystem.Windows.Forms.TextBox();
this.btnConnect=newSystem.Windows.Forms.Button();
//lbChatters
this.lbChatters.ItemHeight=12;
this.lbChatters.Location=newSystem.Drawing.Point(32,40);
this.lbChatters.Name="
lbChatters"
this.lbChatters.Size=newSystem.Drawing.Size(112,172);
this.lbChatters.TabIndex=0;
//rt
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Csharp 局域网 聊天 系统