MSDN的WEBCAST,感觉单纯看一遍意义不大,做个笔记,以备不时只需查找方便。代码部分本人在XP+VS2005+SQL2005测试通过,请大家指教。

TCP通讯
• TCP协议是一个基本的网络协议,基本上所有的网络服务都是基于TCP协议的,如HTTP、FTP等。
• .NET框架类中提供了两个用于TCP网络通讯的类,TCPClient和TcpListener。
• 位于System.Net.Socket 命名空间中。
• TCPClient:客户端类,通过TCP协议与服务进行通讯并获取信息,内部封装Socket类。
• TcpListener:服务端类,监听客户端传来的请求。

我们下边来看两个例子,建立两个页面,一个服务端一个客户端:
这两个页面上每个页面放一个按钮就可以了。
首先来看服务器端的后台编码:
Asp.net中实现网络通讯之TCP通讯using System;
Asp.net中实现网络通讯之TCP通讯
using System.Collections;
Asp.net中实现网络通讯之TCP通讯
using System.ComponentModel;
Asp.net中实现网络通讯之TCP通讯
using System.Data;
Asp.net中实现网络通讯之TCP通讯
using System.Drawing;
Asp.net中实现网络通讯之TCP通讯
using System.Web;
Asp.net中实现网络通讯之TCP通讯
using System.Web.SessionState;
Asp.net中实现网络通讯之TCP通讯
using System.Web.UI;
Asp.net中实现网络通讯之TCP通讯
using System.Web.UI.WebControls;
Asp.net中实现网络通讯之TCP通讯
using System.Web.UI.HtmlControls;
Asp.net中实现网络通讯之TCP通讯
using System.Net.Sockets;
Asp.net中实现网络通讯之TCP通讯
using System.Text;
Asp.net中实现网络通讯之TCP通讯
Asp.net中实现网络通讯之TCP通讯
namespace WebApplication1.UseTcp
}

再来看客户端编码:
Asp.net中实现网络通讯之TCP通讯using System;
Asp.net中实现网络通讯之TCP通讯
using System.Collections;
Asp.net中实现网络通讯之TCP通讯
using System.ComponentModel;
Asp.net中实现网络通讯之TCP通讯
using System.Data;
Asp.net中实现网络通讯之TCP通讯
using System.Drawing;
Asp.net中实现网络通讯之TCP通讯
using System.Web;
Asp.net中实现网络通讯之TCP通讯
using System.Web.SessionState;
Asp.net中实现网络通讯之TCP通讯
using System.Web.UI;
Asp.net中实现网络通讯之TCP通讯
using System.Web.UI.WebControls;
Asp.net中实现网络通讯之TCP通讯
using System.Web.UI.HtmlControls;
Asp.net中实现网络通讯之TCP通讯
using System.Net.Sockets;
Asp.net中实现网络通讯之TCP通讯
using System.Text;
Asp.net中实现网络通讯之TCP通讯
namespace WebApplication2

我的方法是打开两个VS2005,然后先启动服务器端,再启动客户端,这时候就可以测试了。服务端把当前时间做为数据传送到客户端,可以点客户端按钮观察变化。

 

相关文章: