using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Threading;
using System.Net.Sockets;
namespace 聊天程序
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmServer : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblServer;
private System.Windows.Forms.ListBox lstClients;
private System.Windows.Forms.Button btnExit;
/// <summary>
/// 必需的设计器变量。
/// </summary>
/// private ArrayList clients;
private int listenport=5555;
private TcpListener listener;
private Thread processor;
private Socket clientsocket;
private Thread clientservice;
private System.ComponentModel.Container components = null; public frmServer()
{
InitializeComponent();
clients=new ArrayList();
processor=new Thread(new ThreadStart(StartListening));
processor.Start();
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} protected override void OnClosed(EventArgs e)
{
try
{
for(int n=0;n<clients.Count;n++)
{
Client cl=(Client)clients[n];
SendToClient(cl,"QUIT|");
}
}
catch{};
}
private void StartListening()
{
listener=new TcpListener(listenport);
listener.Start();
while(true)
{
try
{
Socket a=listener.AcceptSocket();
clientsocket=s;
clientservice=new Thread(new ThreadStart(serviceClient));
clientservice.Start();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
private void serviceClient()
{
Socket client =clientsocket;
bool keepalive=true;
while(keepalive)
{
Byte[] buffer=new Byte[1024];
client.Receive(buffer);
string clientcommand=System.Text.Encoding.ASCII.GetString(buffer);
string[] tokens=clientcommand.Split(new char[]{'|'});
Console.WriteLine(clientcommand);
if(tokens[0])
{
for(int n=0;n<clients.Count;n++)
{
Client cl=(Client)clients[n];
SendToClient(cl,"JOIN|"+tokens[1]);
}
EndPointep=client.RemoteEndPoint;
Client c=new Client(tokens[1],ep,clientservice,client);
clients.Add(c); }
}
}
private void SendToClient(Client cl,string message)
{
}
private string GetChatterList()
{
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblServer = new System.Windows.Forms.Label();
this.lstClients = new System.Windows.Forms.ListBox();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblServer
//
this.lblServer.Location = new System.Drawing.Point(16, 8);
this.lblServer.Name = "lblServer";
this.lblServer.Size = new System.Drawing.Size(136, 24);
this.lblServer.TabIndex = 0;
this.lblServer.Text = "现在在线聊天的有:";
//
// lstClients
//
this.lstClients.ItemHeight = 12;
this.lstClients.Location = new System.Drawing.Point(16, 32);
this.lstClients.Name = "lstClients";
this.lstClients.Size = new System.Drawing.Size(296, 208);
this.lstClients.TabIndex = 1;
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(240, 256);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(72, 32);
this.btnExit.TabIndex = 2;
this.btnExit.Text = "退出(&E)";
//
// frmServer
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(320, 293);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.lstClients);
this.Controls.Add(this.lblServer);
this.Name = "frmServer";
this.Text = "服务器端聊天程序";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmServer());
}
}
}
在。NET2005里运行下报错。G:\C#\聊天程序\聊天程序\ChatServerForm.cs(115): 找不到类型或命名空间名称“Client”(是否缺少 using 指令或程序集引用?)
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Threading;
using System.Net.Sockets;
namespace 聊天程序
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmServer : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblServer;
private System.Windows.Forms.ListBox lstClients;
private System.Windows.Forms.Button btnExit;
/// <summary>
/// 必需的设计器变量。
/// </summary>
/// private ArrayList clients;
private int listenport=5555;
private TcpListener listener;
private Thread processor;
private Socket clientsocket;
private Thread clientservice;
private System.ComponentModel.Container components = null; public frmServer()
{
InitializeComponent();
clients=new ArrayList();
processor=new Thread(new ThreadStart(StartListening));
processor.Start();
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} protected override void OnClosed(EventArgs e)
{
try
{
for(int n=0;n<clients.Count;n++)
{
Client cl=(Client)clients[n];
SendToClient(cl,"QUIT|");
}
}
catch{};
}
private void StartListening()
{
listener=new TcpListener(listenport);
listener.Start();
while(true)
{
try
{
Socket a=listener.AcceptSocket();
clientsocket=s;
clientservice=new Thread(new ThreadStart(serviceClient));
clientservice.Start();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
private void serviceClient()
{
Socket client =clientsocket;
bool keepalive=true;
while(keepalive)
{
Byte[] buffer=new Byte[1024];
client.Receive(buffer);
string clientcommand=System.Text.Encoding.ASCII.GetString(buffer);
string[] tokens=clientcommand.Split(new char[]{'|'});
Console.WriteLine(clientcommand);
if(tokens[0])
{
for(int n=0;n<clients.Count;n++)
{
Client cl=(Client)clients[n];
SendToClient(cl,"JOIN|"+tokens[1]);
}
EndPointep=client.RemoteEndPoint;
Client c=new Client(tokens[1],ep,clientservice,client);
clients.Add(c); }
}
}
private void SendToClient(Client cl,string message)
{
}
private string GetChatterList()
{
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblServer = new System.Windows.Forms.Label();
this.lstClients = new System.Windows.Forms.ListBox();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lblServer
//
this.lblServer.Location = new System.Drawing.Point(16, 8);
this.lblServer.Name = "lblServer";
this.lblServer.Size = new System.Drawing.Size(136, 24);
this.lblServer.TabIndex = 0;
this.lblServer.Text = "现在在线聊天的有:";
//
// lstClients
//
this.lstClients.ItemHeight = 12;
this.lstClients.Location = new System.Drawing.Point(16, 32);
this.lstClients.Name = "lstClients";
this.lstClients.Size = new System.Drawing.Size(296, 208);
this.lstClients.TabIndex = 1;
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(240, 256);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(72, 32);
this.btnExit.TabIndex = 2;
this.btnExit.Text = "退出(&E)";
//
// frmServer
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(320, 293);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.lstClients);
this.Controls.Add(this.lblServer);
this.Name = "frmServer";
this.Text = "服务器端聊天程序";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmServer());
}
}
}
在。NET2005里运行下报错。G:\C#\聊天程序\聊天程序\ChatServerForm.cs(115): 找不到类型或命名空间名称“Client”(是否缺少 using 指令或程序集引用?)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货