MSDN的WEBCAST,感觉单纯看一遍意义不大,做个笔记,以备不时只需查找方便。代码部分本人在XP+VS2005+SQL2005测试通过,请大家指教。
TCP通讯
• TCP协议是一个基本的网络协议,基本上所有的网络服务都是基于TCP协议的,如HTTP、FTP等。
• .NET框架类中提供了两个用于TCP网络通讯的类,TCPClient和TcpListener。
• 位于System.Net.Socket 命名空间中。
• TCPClient:客户端类,通过TCP协议与服务进行通讯并获取信息,内部封装Socket类。
• TcpListener:服务端类,监听客户端传来的请求。
我们下边来看两个例子,建立两个页面,一个服务端一个客户端:
这两个页面上每个页面放一个按钮就可以了。
首先来看服务器端的后台编码:
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
System.Net.Sockets;
using
System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace
WebApplication1.UseTcp
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public partial class TcpServer : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//设置端口,客户端和这个一样
private const int portNum = 15;
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void stBtn_Click(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool done = false;
TcpListener listener = new TcpListener(portNum);
listener.Start();
while (!done)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("Waiting for connection
");
TcpClient client = listener.AcceptTcpClient();
Response.Write("Connection accepted.");
NetworkStream ns = client.GetStream();
byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ns.Write(byteTime, 0, byteTime.Length);
ns.Close();
client.Close();
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(ex.ToString());
}
}
listener.Stop();
}
}
}
再来看客户端编码:
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
System.Net.Sockets;
using
System.Text;
namespace
WebApplication2
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public partial class _Default : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private const int portNum = 15;//服务器端口,可以随意修改
private const string hostName = "127.0.0.1";//服务器地址,127.0.0.1指本机
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/InBlock.gif)
protected void Button1_Click(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("Try to connect to " + hostName + ":" + portNum.ToString() + "<br>");
TcpClient client = new TcpClient(hostName, portNum);
NetworkStream ns = client.GetStream();
byte[] bytes = new byte[1024];
int bytesRead = ns.Read(bytes, 0, bytes.Length);
Response.Write(Encoding.ASCII.GetString(bytes, 0, bytesRead));
client.Close();
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write(ex.Message.ToString());
}
}
}
}
我的方法是打开两个VS2005,然后先启动服务器端,再启动客户端,这时候就可以测试了。服务端把当前时间做为数据传送到客户端,可以点客户端按钮观察变化。