c# winform 网络通讯 c# winform 网络通讯对象传递 就是想通过客户端传出一个Hashtable到服务器端,然后让服务器在去解析获取数据,怎么来实现 最好有代码 在此先谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用webservice,写程序最方便,部署有点麻烦, 简单的话,可以把数据写进一个xml文件,然后保存到服务器的一个共享目录下, 把Hashtable对象序列化成流然后用socket传给服务端,服务端再反序列化成Hashtable对象就可以了。至于代码自己百度吧,很多的。 http://kb.cnblogs.com/page/78824/ Hashtable不就是个 集合么...理论上来说, 你直接 for 之后 send 要比 序列化之类的东西实际,并且可操作性更强.而且不一定增加编码量 -- 如果你是建立在一个比较完善 的socket通信框架之上的话.如果你非要发对象的话... 的确,不管是1,序列化; 2,xml字符串; 3,webservice 都可以. LS几位给了,我就不说了. 不用webservice 方式 有人知道怎么实现吗?? c# winform 网络通讯对象传递 就是想通过客户端传出一个Hashtable到服务器端,然后让服务器在去解析获取数据,怎么来实现 不用 webservice 方式 最好有代码 在此先谢谢了 我以前做过一个数据解析,觉得用WEBSERVICE,方便,你可以把WEBSERVIEC直接布置到服务器上,通过WEBSERVICE对数据解析。TCPClient.GetStream()方法http://www.cnblogs.com/CShapWinForms/archive/2007/06/27/797479.html 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); } } } Hashtable 传送已经搞定了 可是传对象还是有问题 我的最终目标是想传一个对象 搞了好久 还没有搞定 唉有哪位高手还赐教一下 谢谢了 C#桌面应用程序中添加弹出式广告窗口 跪求高人解答wpf无样式窗口改变窗口大小问题 子窗体最大化占用菜单怎么解决?? FTPWebRequest 上传文件的问题,搞不定了,请各位帮忙 如何保留decimal类型数字的小数点后三位?多谢! 对UI开发有兴趣的请进,顶者有分 数据库怎么联不上?我刚学.net.请大家帮忙 GetShortPathName的返回值问题 用C#语言进行图像处理的学习 DataGrid的行高度怎样控制?谢谢 winfrom 操作SQLServer数据库的问题。。 异步服务器端怎么释放断开的客户端资源
理论上来说, 你直接 for 之后 send 要比 序列化之类的东西实际,并且可操作性更强.而且不一定增加编码量 -- 如果你是建立在一个比较完善 的socket通信框架之上的话.如果你非要发对象的话... 的确,不管是1,序列化; 2,xml字符串; 3,webservice 都可以. LS几位给了,我就不说了.
不用 webservice 方式
最好有代码
在此先谢谢了
TCPClient.GetStream()方法
http://www.cnblogs.com/CShapWinForms/archive/2007/06/27/797479.html
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);
}
}
}
有哪位高手还赐教一下 谢谢了