server.cs
******************************************
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 MyCsPro
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ListBox listBox1;
private Socket socketForClient ;
private NetworkStream networkStream ;
private TcpListener tcpListener ;
private StreamWriter streamWriter ;
private StreamReader streamReader ;
private Thread _thread1 ;
private System.Windows.Forms.Button button3;
/// <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.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
// 
// label1
// 
this.label1.Location = new System.Drawing.Point(24, 216);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(120, 16);
this.label1.TabIndex = 0;
this.label1.Text = "往客户端发送信息:";
// 
// button1
// 
this.button1.Location = new System.Drawing.Point(16, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "启动";
this.button1.Click += new System.EventHandler(this.button1_Click);
// 
// button2
// 
this.button2.Location = new System.Drawing.Point(112, 16);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "中断";
this.button2.Click += new System.EventHandler(this.button2_Click);
// 
// textBox1
// 
this.textBox1.Location = new System.Drawing.Point(144, 208);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
// 
// listBox1
// 
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(0, 48);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(280, 148);
this.listBox1.TabIndex = 4;
// 
// button3
// 
this.button3.Location = new System.Drawing.Point(32, 240);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(216, 23);
this.button3.TabIndex = 5;
this.button3.Text = "发送";
this.button3.Click += new System.EventHandler(this.button3_Click);
// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button3);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Closed += new System.EventHandler(this.Form1_Closed);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
private void Listen ( )
{
//创建一个tcpListener对象,此对象主要是对给定端口进行侦听
tcpListener = new TcpListener ( 9002 ) ;
//开始侦听
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 ( ) ; 
}

}
}
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 button3_Click(object sender, System.EventArgs e)
{
MessageBox.Show("不怎么知道发送!");
} }
}

解决方案 »

  1.   

    两个部分呀
    一个是server.cs
    一个是client.csc/s结构
      

  2.   

    检查一下你的client在button1_click之前text1.Text是不是==""
    还有server在button1_click发生的时候text1.TExt!=""满足吗?
    你的server程序中要求汛序执行,如果执行到if(text1.Text!="")结果是false,服务器就不会发送任何消息,客户端一直在那里ReadLine(),也read不到什么,就会阻塞了。
      

  3.   

    呵呵.自己搞定了端口的问题.
    我所用的9002端口,已经被IIS的一个网站占着用了.关掉那个站,释放那个端口就可以了,另外我放server.exe的那个服务器的网卡邦定了多个ip,因此IPHost.AddressList就有多个值,我把 Dns.Resolve里的Ip参数换成了指定的Ip..谢谢 suiyun(始终搞不懂com+)的回答.
    接分