客户端程序代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Net.Sockets;
using System.IO;namespace Client
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Client : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox txtInput;
private System.Windows.Forms.TextBox txtDisplay;
private NetworkStream output;
private BinaryReader reader;
private BinaryWriter writer;
private string message="";
private Thread readThread;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Client()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
readThread=new Thread(new ThreadStart(Runclient));
readThread.Start();
//
// 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.txtInput = new System.Windows.Forms.TextBox();
this.txtDisplay = new System.Windows.Forms.TextBox();
this.SuspendLayout();
// 
// txtInput
// 
this.txtInput.Location = new System.Drawing.Point(0, 8);
this.txtInput.Name = "txtInput";
this.txtInput.Size = new System.Drawing.Size(288, 21);
this.txtInput.TabIndex = 0;
this.txtInput.Text = "";
this.txtInput.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtInput_KeyDown);
// 
// txtDisplay
// 
this.txtDisplay.Location = new System.Drawing.Point(0, 40);
this.txtDisplay.Multiline = true;
this.txtDisplay.Name = "txtDisplay";
this.txtDisplay.Size = new System.Drawing.Size(288, 224);
this.txtDisplay.TabIndex = 1;
this.txtDisplay.Text = "";
// 
// Client
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.txtDisplay);
this.Controls.Add(this.txtInput);
this.Name = "Client";
this.Text = "客户端";
this.Closing += new System.ComponentModel.CancelEventHandler(this.Client_Closing);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main() 
{
Application.Run(new Client());
} private void Client_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
} private void txtInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
try
{
if(e.KeyCode==Keys.Enter)
{
writer.Write("CLIENT>>>"+txtInput.Text);
txtDisplay.Text+="\r\nCLIENT>>"+txtInput.Text;
txtInput.Clear();
}
}
catch(SocketException ioe)
{
txtDisplay.Text+="\nError writing object";
}
}
private void Runclient()
{
TcpClient client;
try
{
txtDisplay.Text+="Attempting connection.\r\n";
client=new TcpClient();
client.Connect("10.1.75.13",5000);
output=client.GetStream();
writer=new BinaryWriter(output);
reader=new BinaryReader(output);
txtDisplay.Text+="\r\nGot I/O streams\r\n";
txtInput.ReadOnly=false;
do
{
try
{
message=reader.ReadString();
txtDisplay.Text+="\r\n"+message;
}
catch(Exception)
{
System.Environment.Exit(System.Environment.ExitCode);
}
}while(message!="SERVER>>>TERMINATE");
txtDisplay.Text+="\r\nClosing connection.\r\n";
writer.Close();
reader.Close();
output.Close();
client.Close();
Application.Exit();
}
catch(Exception error)
{
MessageBox.Show(error.ToString());
}
}
}
}

解决方案 »

  1.   

    服务端的connection用一个数组列表来管理
    ArrayList
    最好用异步传输数据 
    看看下面两个方法的帮助
    BeginReceive
    BeginAccept
      

  2.   

    感觉好象用一个类去WRAP 一下SOCKET对象比较好
    例如SOCKETWRAPPER类型拥有一个SOCKET私有成员(可以用属性GET ,SET封装一下),一个窗体引用成员,一个关键的HANDELSOCKET方法。然后:
    try
    {
    listener=new TcpListener(listenport);
    listener.Start();
    while(true)
    {
    txtDisplay.Text="Waiting for connection\r\n";
                                                 Socket con=new Socket();
                                                 con=listener.AcceptSocket();
                              SOCKETWRAPPER SW=new SOCKETWRAPPER(con,this);
                                                 Thread service=new Thread(); service=new Thread(new ThreadStart(SW.HANDELSOCKET));
    service.Start();
    }
    }应该可以吧,我没有试过,是参照用MFC时的类似写法,这样就有一个新的连接就新一个线程运行处理。
      

  3.   

    Thread service=new Thread();应该是
    Thread service;