服务器端:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace ChatServer
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox txtHost;
private System.Windows.Forms.TextBox txtPort;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ComboBox curUserList;
private System.Windows.Forms.ListBox lstInfo;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
//该服务器默认的监听端口号
static int port=1234;
private TcpListener listener;
private Socket tmpSocket;
//最多客户端连接数
static int MaxNum=100;
//clients数组保持当前在线的client对象
static ArrayList clients=new ArrayList();
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtHost = new System.Windows.Forms.TextBox();
this.txtPort = new System.Windows.Forms.TextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.label3 = new System.Windows.Forms.Label();
this.curUserList = new System.Windows.Forms.ComboBox();
this.lstInfo = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(0, 16);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "主机号:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this.label2.Location = new System.Drawing.Point(0, 56);
this.label2.Name = "label2";
this.label2.TabIndex = 1;
this.label2.Text = "端口号:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// txtHost
//
this.txtHost.Location = new System.Drawing.Point(104, 16);
this.txtHost.Name = "txtHost";
this.txtHost.Size = new System.Drawing.Size(128, 21);
this.txtHost.TabIndex = 2;
this.txtHost.Text = "192.168.0.163";
//
// txtPort
//
this.txtPort.Location = new System.Drawing.Point(104, 56);
this.txtPort.Name = "txtPort";
this.txtPort.ReadOnly = true;
this.txtPort.Size = new System.Drawing.Size(128, 21);
this.txtPort.TabIndex = 3;
this.txtPort.Text = "";
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(264, 16);
this.btnStart.Name = "btnStart";
this.btnStart.TabIndex = 4;
this.btnStart.Text = "启动";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(264, 56);
this.btnExit.Name = "btnExit";
this.btnExit.TabIndex = 5;
this.btnExit.Text = "退出";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// label3
//
this.label3.Location = new System.Drawing.Point(24, 136);
this.label3.Name = "label3";
this.label3.TabIndex = 6;
this.label3.Text = "当前在线用户:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// curUserList
//
this.curUserList.Location = new System.Drawing.Point(160, 136);
this.curUserList.Name = "curUserList";
this.curUserList.Size = new System.Drawing.Size(121, 20);
this.curUserList.TabIndex = 7;
//
// lstInfo
//
this.lstInfo.ItemHeight = 12;
this.lstInfo.Location = new System.Drawing.Point(24, 184);
this.lstInfo.Name = "lstInfo";
this.lstInfo.Size = new System.Drawi