为什么我的服务端起动后,我用一个客户端连上可以正常互发消息,而当我这客户端刚一关闭,服务段就出现异常"System.io.ioexception:无法从传输连接中读取数据.--->System.net.sockets.socketexception:你的主机中的软件放弃了一个已建立的连接"报错?
...................//服务端代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;using System.Net.Sockets ;
using System.IO ;
using System.Threading ;
using System.Net ;
//导入程序中使用到的名字空间namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; private Socket socketForClient ;
private NetworkStream networkStream ;
private TcpListener tcpListener ;
private StreamWriter streamWriter ;
private StreamReader streamReader ;
private Thread _thread1 ;
public Form1()
{
InitializeComponent(); } protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null) 
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(24, 272);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(432, 21);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
// 
// button1
// 
this.button1.Enabled = false;
this.button1.Location = new System.Drawing.Point(488, 40);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 23);
this.button1.TabIndex = 1;
this.button1.Text = "startservice";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(488, 88);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(112, 23);
this.button2.TabIndex = 2;
this.button2.Text = "stopservice";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// listBox1
// 
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(16, 16);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(440, 196);
this.listBox1.TabIndex = 3;
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(24, 232);
this.label1.Name = "label1";
this.label1.TabIndex = 4;
this.label1.Text = "回复客户端";
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(632, 357);
this.Controls.Add(this.label1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "网络服务器端";
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);
this.ResumeLayout(false); }
#endregion private void Listen ( )
{
//创建一个tcpListener对象,此对象主要是对给定端口进行侦听
tcpListener = new TcpListener ( 1234 ) ;
//开始侦听
tcpListener.Start ( ) ; 
//返回可以用以处理连接的Socket实例
socketForClient = tcpListener.AcceptSocket ( ) ; 
try
{
//如果返回值是"true",则产生的套节字已经接受来自远方的连接请求
if ( socketForClient.Connected )
{
listBox1.Items.Add ( "已经和管理学院西院客户端成功连接!" ) ;
while ( true )
{
//创建networkStream对象通过网络套节字来接受和发送数据
networkStream = new NetworkStream ( socketForClient ) ;
//从当前数据流中读取一行字符,返回值是字符串
streamReader = new StreamReader ( networkStream ) ;
string msg = streamReader.ReadLine ( ) ;
listBox1.Items.Add ( "收到客户端信息:" + msg ) ;
streamWriter = new StreamWriter ( networkStream ) ;
if ( textBox1.Text != "" )
{
listBox1.Items.Add ( "往客户端反馈信息:" + textBox1.Text ) ;
//往当前的数据流中写入一行字符串
streamWriter.WriteLine ( textBox1.Text ) ;
//刷新当前数据流中的数据
streamWriter.Flush ( ) ; 
}
else
{
listBox1.Items.Add ( "往客户端反馈信息:来自西院"  ) ;
//往当前的数据流中写入一行字符串
streamWriter.WriteLine ( "往客户端反馈信息: ----我来自西院" ) ;
//刷新当前数据流中的数据
streamWriter.Flush ( ) ; 

}

}
}
catch ( Exception ey )
{
MessageBox.Show ( ey.ToString ( ) ) ;
}
}

[STAThread]
static void Main() 
{
Application.Run(new Form1());
} private void Form1_Closed(object sender, System.EventArgs e)
{
//关闭线程和流
networkStream.Close ( ) ;
streamReader.Close ( ) ;
streamWriter.Close ( ) ;
_thread1.Abort ( ) ;
tcpListener.Stop ( ) ;
socketForClient.Shutdown ( SocketShutdown.Both ) ;
socketForClient.Close ( ) ;
} private void button1_Click(object sender, System.EventArgs e)
{
// listBox1.Items .Add ( "服务已经启动!" ) ;
// _thread1 = new Thread ( new ThreadStart ( Listen ) ) ; 
// _thread1.Start ( ) ; } private void button2_Click(object sender, System.EventArgs e)
{
//关闭线程和流
networkStream.Close ( ) ;
streamReader.Close ( ) ;
streamWriter.Close ( ) ;
_thread1.Abort ( ) ;
tcpListener.Stop ( ) ;
socketForClient.Shutdown ( SocketShutdown.Both ) ;
socketForClient.Close ( ) ; } private void Form1_Load(object sender, System.EventArgs e)
{
listBox1.Items .Add ( "服务已经启动!" ) ;
_thread1 = new Thread ( new ThreadStart ( Listen ) ) ; 
_thread1.Start ( ) ;
}
}
}
.....................................

解决方案 »

  1.   

    ....................................
    //客户端代码
    using System ;
    using System.Drawing ;
    using System.Collections ;
    using System.ComponentModel ;
    using System.Windows.Forms ;
    using System.Data ;
    using System.Net.Sockets ;
    using System.IO ;
    using System.Threading ;
    //导入程序中使用到的名字空间
    namespace WindowsApplication1
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;//        我新加的
    private NetworkStream networkStream ;
    private StreamReader streamReader ;
    private StreamWriter streamWriter ;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.TextBox textBox2;
    TcpClient myclient ;
    public Form1()
    {
    InitializeComponent();
    } protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.listBox1 = new System.Windows.Forms.ListBox();
    this.label2 = new System.Windows.Forms.Label();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(16, 280);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(368, 21);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "张先生";
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(8, 8);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(200, 23);
    this.label1.TabIndex = 1;
    this.label1.Text = "服务器的IP";
    // 
    // listBox1
    // 
    this.listBox1.ItemHeight = 12;
    this.listBox1.Location = new System.Drawing.Point(8, 32);
    this.listBox1.Name = "listBox1";
    this.listBox1.Size = new System.Drawing.Size(400, 184);
    this.listBox1.TabIndex = 2;
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(16, 248);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(224, 23);
    this.label2.TabIndex = 3;
    this.label2.Text = "message to server";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(424, 280);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(96, 23);
    this.button1.TabIndex = 4;
    this.button1.Text = "sendtoserver";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(432, 32);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(88, 23);
    this.button2.TabIndex = 5;
    this.button2.Text = "beginconnect";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(232, 8);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(160, 21);
    this.textBox2.TabIndex = 6;
    this.textBox2.Text = "192.168.0.2";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(528, 333);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.listBox1);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.textBox1);
    this.Name = "Form1";
    this.Text = "C#的网络编程客户器端";
    this.Closed += new System.EventHandler(this.Form1_Closed);
    this.ResumeLayout(false); }
    #endregion private void ClientConnect()
    {
    //连接到服务器端口,在这里是选用本地机器作为服务器,你可以通过修改IP地址来改变服务器
    try
    {
    myclient = new TcpClient ( textBox2.Text.ToString().Trim() , 1234 ) ;
    // myclient = new TcpClient ( "192.168.0.2" , 1234 ) ;
    // myclient = new TcpClient ( "localhost" , 1234 ) ;
    // myclient = new TcpClient ( "192.168.0.1" , 1234 ) ;
    }
    catch
    {
    MessageBox.Show ( "没有连接到服务器!" ) ;
    return ;
    }
    //创建networkStream对象通过网络套节字来接受和发送数据
    networkStream = myclient.GetStream ( ) ;
    streamReader = new StreamReader ( networkStream ) ;
    streamWriter = new StreamWriter ( networkStream ) ;

    }
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {

    if ( textBox1.Text == "" )
    {
    MessageBox.Show ( "请确定文本框为非空!" ) ;
    textBox1.Focus ( ) ;
    return ;
    }
    try
    {
    string s ;
    //往当前的数据流中写入一行字符串
    streamWriter.WriteLine ( textBox1.Text ) ;
    //刷新当前数据流中的数据
    streamWriter.Flush ( ) ;
    //从当前数据流中读取一行字符,返回值是字符串
    s = streamReader.ReadLine ( ) ;
    listBox1.Items.Add ( "读取服务器端发送内容:" + s ) ;
    }
    catch ( Exception ee )
    {
    MessageBox.Show ( "从服务器端读取数据出现错误,类型为:" + ee.ToString ( ) ) ;
    }  } private void Form1_Closed ( object sender , System.EventArgs e )
    {
    streamReader.Close ( ) ;
    streamWriter.Close ( ) ;
    networkStream.Close ( ) ; } private void button2_Click(object sender, System.EventArgs e)
    {
    ClientConnect();
    this.button2.Enabled=false;
    }
    }
    }
    .....................................
      

  2.   

    你客户端关闭后,服务器得NetWorkstream并没有关闭,依然处于打开状态,但是又连不上客户端,这时从里边read()时,就会报错。
      

  3.   

    Tcp的连接,断开命令呢?如STAT,Quit等,没有通知主机客户端断开连接,而主机的端口一值在等待客户端的数据,主机当然会报错。  或者加个异常处理,如果客户端长时间没有响应,则主机自动关闭socket连接,停止线程!
      

  4.   

    必须定义异步委托调用,好像是这样的,myCallBack不太记得了
    delegate string myRead();
    AsyncCallback a = new AsyncCallback(myCallBack);//void myCallBack(IAsyncResult ar){...}
    (new myRead(sr.ReadLine)).BeginInvoke(a,null);
      

  5.   

    把你的read() 放到try里面:
    try {
      ...read();
    }
    catch(Exception) {
      连接中断,关闭
    }
      

  6.   

    定义异步调用也没有用的,异步调用只是为了避免网络的block,而不能判断是否断开。
    只能自己判断,我一般是用捕获异常来判断的。
    自己捕获异常,然后处理。
      

  7.   

    我改成了下术爬p2p程序,在我的本机上中文字体发送正常,可用两台局狱网上的机器同时运行通信是,却互相收到的是乱码.为什么呀?.......................
    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 qq
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.RichTextBox richTextBox1;
    private System.Windows.Forms.RichTextBox richTextBox2;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.StatusBar statusBar1;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.GroupBox groupBox1; private Thread listenThread;
    private bool listen=false;
    private TcpListener tcpListener;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.Button button4; /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; 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.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.richTextBox1 = new System.Windows.Forms.RichTextBox();
    this.richTextBox2 = new System.Windows.Forms.RichTextBox();
    this.label1 = new System.Windows.Forms.Label();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.statusBar1 = new System.Windows.Forms.StatusBar();
    this.label3 = new System.Windows.Forms.Label();
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.groupBox2 = new System.Windows.Forms.GroupBox();
    this.button4 = new System.Windows.Forms.Button();
    this.groupBox1.SuspendLayout();
    this.groupBox2.SuspendLayout();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(104, 16);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(120, 21);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "192.168.0.2";
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(16, 32);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(80, 21);
    this.textBox2.TabIndex = 1;
    this.textBox2.Text = "兵哥哥";
    // 
    // richTextBox1
    // 
    this.richTextBox1.Location = new System.Drawing.Point(176, 256);
    this.richTextBox1.Name = "richTextBox1";
    this.richTextBox1.Size = new System.Drawing.Size(352, 24);
    this.richTextBox1.TabIndex = 2;
    this.richTextBox1.Text = "你好";
    // 
    // richTextBox2
    // 
    this.richTextBox2.BackColor = System.Drawing.SystemColors.Info;
    this.richTextBox2.Location = new System.Drawing.Point(16, 64);
    this.richTextBox2.Name = "richTextBox2";
    this.richTextBox2.Size = new System.Drawing.Size(544, 160);
    this.richTextBox2.TabIndex = 3;
    this.richTextBox2.Text = "";
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(16, 16);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(72, 23);
    this.label1.TabIndex = 4;
    this.label1.Text = "目标地址IP";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(264, 16);
    this.button1.Name = "button1";
    this.button1.TabIndex = 5;
    this.button1.Text = "开始监听";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(376, 16);
    this.button2.Name = "button2";
    this.button2.TabIndex = 6;
    this.button2.Text = "停止监听";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // button3
    // 
    this.button3.Location = new System.Drawing.Point(568, 256);
    this.button3.Name = "button3";
    this.button3.TabIndex = 7;
    this.button3.Text = "发送";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    // 
    // statusBar1
    // 
    this.statusBar1.Location = new System.Drawing.Point(0, 327);
    this.statusBar1.Name = "statusBar1";
    this.statusBar1.Size = new System.Drawing.Size(656, 22);
    this.statusBar1.TabIndex = 9;
    this.statusBar1.Text = "statusBar1";
    // 
    // label3
    // 
    this.label3.Location = new System.Drawing.Point(120, 32);
    this.label3.Name = "label3";
    this.label3.Size = new System.Drawing.Size(40, 23);
    this.label3.TabIndex = 10;
    this.label3.Text = "说";
    // 
    // groupBox1
    // 
    this.groupBox1.Controls.Add(this.label3);
    this.groupBox1.Controls.Add(this.textBox2);
    this.groupBox1.Location = new System.Drawing.Point(8, 224);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Size = new System.Drawing.Size(552, 72);
    this.groupBox1.TabIndex = 11;
    this.groupBox1.TabStop = false;
    this.groupBox1.Text = "发言区";
    // 
    // groupBox2
    // 
    this.groupBox2.Controls.Add(this.button4);
    this.groupBox2.Location = new System.Drawing.Point(8, 0);
    this.groupBox2.Name = "groupBox2";
    this.groupBox2.Size = new System.Drawing.Size(592, 48);
    this.groupBox2.TabIndex = 12;
    this.groupBox2.TabStop = false;
    // 
    // button4
    // 
    this.button4.Location = new System.Drawing.Point(488, 17);
    this.button4.Name = "button4";
    this.button4.TabIndex = 0;
    this.button4.Text = "退出系统";
    this.button4.Click += new System.EventHandler(this.button4_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(656, 349);
    this.ControlBox = false;
    this.Controls.Add(this.statusBar1);
    this.Controls.Add(this.button3);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.richTextBox2);
    this.Controls.Add(this.richTextBox1);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.groupBox1);
    this.Controls.Add(this.groupBox2);
    this.Name = "Form1";
    this.Text = "聊天程序(天大43--127 兵哥哥开发)";
    this.groupBox1.ResumeLayout(false);
    this.groupBox2.ResumeLayout(false);
    this.ResumeLayout(false); }
    #endregion
      

  8.   

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    listenThread =new Thread(new ThreadStart(StartListen));
    listenThread.Start();
    } private void StartListen()
    {
    try
    { tcpListener=new TcpListener(8888);
    tcpListener.Start();
    statusBar1.Text="兵哥哥说:正在监听.....,你可以互相聊天了";
    this.listen=true;
    while(listen)
    {
    Socket s=tcpListener.AcceptSocket();
    Byte[] stream=new byte[100];
    int i=s.Receive(stream);
    string message=System.Text.Encoding.UTF8.GetString(stream);
    this.richTextBox2.AppendText(message);
    }
    }
    catch(Exception ex)
    { statusBar1.Text="兵哥哥说:停止监听....";
    }
    } private void button2_Click(object sender, System.EventArgs e)
    {
    if (this.listen)
    {
    this.listen=false;
    this.tcpListener.Stop();
    this.listenThread.Abort();
    }
    statusBar1.Text="兵哥哥说:停止监听....";
    } private void button3_Click(object sender, System.EventArgs e)
    {
    try
    {
    string msg="<"+this.textBox2.Text+"> 说:"+this.richTextBox1.Text+"\n";
    TcpClient tcpClient=new TcpClient(textBox1.Text,8888);
    NetworkStream tcpStream=tcpClient.GetStream();
    StreamWriter streamW=new StreamWriter(tcpStream);
    streamW.Write(msg);
    streamW.Flush();
    tcpStream.Close();
    this.richTextBox2.AppendText(msg);
    //this.richTextBox1.Clear(); }
    catch(Exception ex)
    { statusBar1.Text="兵哥哥说:目标计算机拒绝请求,你是否输入对方的正确IP,并点击了--开始监听--按纽";
    }
    } private void button4_Click(object sender, System.EventArgs e)
    {
    if (this.listen)
    {
    this.listen=false;
    this.tcpListener.Stop();
    this.listenThread.Abort();
    }
    Application.Exit();
    }
    }
    }
      

  9.   

    搂主:
    1。顶楼的错误应该是你没有关闭文件读取流的异常,是IO的显然不是线程方面的问题。在客户端断开连接的时候要将streamWriter,streamReader 关闭,有客户端的时候重新再建。
    2。你的p2p的乱嘛是你再向数据流中写入的数据没有进行正确编码。
    string msg="<"+this.textBox2.Text+"> 说:"+this.richTextBox1.Text+"\n";
    //这里要添加:byte[] sendname=System.Text.Encoding.UTF8.GetBytes(msg);
    TcpClient tcpClient=new TcpClient(textBox1.Text,8888);
    NetworkStream tcpStream=tcpClient.GetStream();
    streamWriter streamW=new StreamWriter(tcpStream);
    //这里改为:
    streamW.Write(sendname,0,sendname.Length);//下面不变