关于C#网络数据传输的问题 我用C# 1.1的,写了一个测试的服务端和客户端。不管用Socket还是TcpClient,我从客户端发送数据到服务端,服务端不能马上收到数据,只有当在客户端调用close()方法的时候,服务端才一次性收到所有消息。我试过flush()什么的都不行。难道这就意味着我从客户端每发送一个指令就要建立一次连接然后断开? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 数据是收到了,保存在streamReader中,只是这个地方会造成程序阻塞,所以只有断开,才能继续。如果使用streamreader.ReadLine();方法,当字符串中出现回车它就能继续运行了。 晕我觉得自己写的SOCKET通讯程序比你们多,可是从来没碰到这样的问题我发之前都是将对象序列花2进制流,再接受后再将流反序列化成对象就可以了没出现过你们说的那样情况我猜想 streamreader这个对象类似于文件流样,当你想再次用它输出里面的信息时,必须得把他先CLOSE掉,才可以,你们查资料吧 streamreader.ReadLine必须读到换行才返回 使用TCPClient.GetStream()方法可以获得一个NetWorkStream对象,网络流对象中有个Read方法执行时会产生阻塞。使用这个方法就可以解决这个问题。//以下是TCP通信的客户端代码,全部复制测试看看//===============================================using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net.Sockets;using System.Text;using System.Threading;namespace TCP_Client{ /// <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.Button button1; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.TextBox textBox4; private System.Windows.Forms.Button button2; private TcpClient tc; //TCP客户端 private Thread thr; //接收使用的线程 private NetworkStream ns; //用户接收和发送数据的网络流对象 /// <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.button1 = new System.Windows.Forms.Button(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox4 = new System.Windows.Forms.TextBox(); this.button2 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(8, 8); this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox1.Size = new System.Drawing.Size(320, 176); this.textBox1.TabIndex = 0; this.textBox1.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(8, 192); this.textBox2.Multiline = true; this.textBox2.Name = "textBox2"; this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; this.textBox2.Size = new System.Drawing.Size(320, 88); this.textBox2.TabIndex = 1; this.textBox2.Text = ""; // // button1 // this.button1.Location = new System.Drawing.Point(256, 296); this.button1.Name = "button1"; this.button1.TabIndex = 2; this.button1.Text = "发送"; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox3 // this.textBox3.Location = new System.Drawing.Point(8, 296); this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(80, 21); this.textBox3.TabIndex = 3; this.textBox3.Text = "127.0.0.1"; // // textBox4 // this.textBox4.Location = new System.Drawing.Point(96, 296); this.textBox4.Name = "textBox4"; this.textBox4.Size = new System.Drawing.Size(40, 21); this.textBox4.TabIndex = 4; this.textBox4.Text = "5000"; // // button2 // this.button2.Location = new System.Drawing.Point(144, 296); this.button2.Name = "button2"; this.button2.Size = new System.Drawing.Size(64, 23); this.button2.TabIndex = 5; this.button2.Text = "连接"; this.button2.Click += new System.EventHandler(this.button2_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(336, 341); this.Controls.Add(this.button2); this.Controls.Add(this.textBox4); this.Controls.Add(this.textBox3); this.Controls.Add(this.button1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void connect() { string ip=textBox3.Text; int port = Convert.ToInt32(textBox4.Text); tc = new TcpClient(ip,port); //连接服务器 ns = tc.GetStream(); //获得网络流对象 byte[] bytes = new byte[8192]; //设置缓冲区 int c; while(true) //循环接收数据 { c = ns.Read(bytes,0,bytes.Length); //此处会产生阻塞,获得数据时继续运行。 if(c==0) break; textBox1.AppendText(Encoding.Default.GetString(bytes)); } } //启动侦听线程 private void button2_Click(object sender, System.EventArgs e) { thr = new Thread(new ThreadStart(connect)); thr.Start(); } //使用ns的Write方法发送数据 private void button1_Click(object sender, System.EventArgs e) { byte[] bytes = Encoding.Default.GetBytes(textBox2.Text); ns.Write(bytes,0,bytes.Length); } }} 求 c#正则表达式教材 关于C#括号的问题 关于vs2008的两个疑惑 有关在ACCESS数据库中创建一个表,SQL语句问题请教 请问自己用C#编写的Windows Service可以访问SQL数据库中的某个表么? 关于RSA加密和签名的困惑 vs.net2003的安装问题!!!!!!!!!!! RichTextBox的文本怎样使关键字显示不同的颜色字体 请教:win form的datagrid问题 怎样在控件上画线,或者说怎样获取控件的Graphics对象. 一个小问题,怎么获取或设置文件夹的大小? C#里如何修改鼠标灵敏度?
我觉得自己写的SOCKET通讯程序比你们多,可是从来没碰到这样的问题
我发之前都是将对象序列花2进制流,再接受后再将流反序列化成对象就可以了
没出现过你们说的那样情况
我猜想 streamreader这个对象类似于文件流样,当你想再次用它输出里面的信息时,必须得把他先CLOSE掉,才可以,你们查资料吧
//===============================================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net.Sockets;
using System.Text;
using System.Threading;namespace TCP_Client
{
/// <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.Button button1;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Button button2;
private TcpClient tc; //TCP客户端
private Thread thr; //接收使用的线程
private NetworkStream ns; //用户接收和发送数据的网络流对象
/// <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.button1 = new System.Windows.Forms.Button();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(320, 176);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(8, 192);
this.textBox2.Multiline = true;
this.textBox2.Name = "textBox2";
this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox2.Size = new System.Drawing.Size(320, 88);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(256, 296);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "发送";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(8, 296);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(80, 21);
this.textBox3.TabIndex = 3;
this.textBox3.Text = "127.0.0.1";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(96, 296);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(40, 21);
this.textBox4.TabIndex = 4;
this.textBox4.Text = "5000";
//
// button2
//
this.button2.Location = new System.Drawing.Point(144, 296);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 5;
this.button2.Text = "连接";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(336, 341);
this.Controls.Add(this.button2);
this.Controls.Add(this.textBox4);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void connect()
{
string ip=textBox3.Text;
int port = Convert.ToInt32(textBox4.Text); tc = new TcpClient(ip,port); //连接服务器
ns = tc.GetStream(); //获得网络流对象
byte[] bytes = new byte[8192]; //设置缓冲区
int c;
while(true) //循环接收数据
{
c = ns.Read(bytes,0,bytes.Length); //此处会产生阻塞,获得数据时继续运行。
if(c==0)
break;
textBox1.AppendText(Encoding.Default.GetString(bytes));
}
}
//启动侦听线程
private void button2_Click(object sender, System.EventArgs e)
{
thr = new Thread(new ThreadStart(connect));
thr.Start();
}
//使用ns的Write方法发送数据
private void button1_Click(object sender, System.EventArgs e)
{
byte[] bytes = Encoding.Default.GetBytes(textBox2.Text);
ns.Write(bytes,0,bytes.Length);
}
}
}