哪位兄弟帮我看看,为什么读不出来!
我刚刚接触C#网络编程的例子,今天拿来看了一下,在本地机上运行正常.
可我把server.exe放到Internet上就不行了,Sorket连接是可以建立起来(Ip地址和tcp端口都没问题,但是从连接缓冲里读数据的时候,却一直阻塞!读不出来!
client.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.Net;
using System.IO ;
using System.Threading ;
namespace Client
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private NetworkStream networkStream ;
private StreamReader streamReader ;
private StreamWriter streamWriter ;
TcpClient myclient ;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
Connect();
//
// 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.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(16, 40);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(256, 136);
this.listBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(176, 224);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "发送信息";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(24, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 23);
this.label1.TabIndex = 3;
this.label1.Text = "信息交换:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 224);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 200);
this.label2.Name = "label2";
this.label2.TabIndex = 4;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "C#的网络编程客户器端!";
this.ResumeLayout(false);
}
#endregion
private void Connect()
{
try
{
myclient = new TcpClient () ;
IPHostEntry IPHost = Dns.Resolve("202.98.201.243");
string []aliases = IPHost.Aliases;
IPAddress[] addr = IPHost.AddressList;
IPEndPoint ep = new IPEndPoint(addr[0], 1234);
myclient.Connect(ep);
}
catch
{
MessageBox.Show ( "没有连接到服务器!" ) ;
return ;
}
//创建networkStream对象通过网络套节字来接受和发送数据
// 尝试与服务器连接
networkStream = myclient.GetStream ( ) ;
streamReader = new StreamReader ( networkStream ) ;
streamWriter = new StreamWriter ( networkStream ) ;
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[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 ( ) ;
}
catch ( Exception ee )
{
MessageBox.Show ( "从服务器端读取数据出现错误,类型为:" + ee.ToString ( ) ) ;
}
}
private void Form1_Closed ( object sender , System.EventArgs e )
{
streamReader.Close ( ) ;
streamWriter.Close ( ) ;
networkStream.Close ( ) ;
}
}
}