惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Cloudbric
Cloudbric
G
GRAHAM CLULEY
S
Securelist
Schneier on Security
Schneier on Security
Help Net Security
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
Spread Privacy
Spread Privacy
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
Cisco Talos Blog
Cisco Talos Blog
T
Tailwind CSS Blog
博客园_首页
有赞技术团队
有赞技术团队
Simon Willison's Weblog
Simon Willison's Weblog
Stack Overflow Blog
Stack Overflow Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Latest news
Latest news
T
Tor Project blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Attack and Defense Labs
Attack and Defense Labs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
O
OpenAI News
J
Java Code Geeks
T
Tenable Blog
K
Kaspersky official blog
AWS News Blog
AWS News Blog
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
T
Threatpost
月光博客
月光博客
H
Heimdal Security Blog
Security Latest
Security Latest
The Hacker News
The Hacker News
Y
Y Combinator Blog
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
C
Cisco Blogs
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
T
The Blog of Author Tim Ferriss
C
CERT Recently Published Vulnerability Notes
D
Docker
Google Online Security Blog
Google Online Security Blog
D
DataBreaches.Net
V
Visual Studio Blog
H
Help Net Security

博客园 - Yeemio

项目管理 SQL2008多语言版本共存 (WF索引)基于WF设计业务流程平台 常用的40个网站制作技巧 - Yeemio - 博客园 新闻更新延时引发的学习,CACHE的利用。 在Web页面上使用字体的简单原则 Asp.net 备份、还原Ms SQLServer及压缩Access数据库 Access数据库导入SQL数据库的方法之一。。 Windows2003配置ASP我遇到的小问题。。 工作了。。第一天。新的环境需要适应! Asp.Net文件处理 深入解析ASP.NET架构 Asp.Net事务和异常处理 ASP.NET安全性 C#.NET常用函数 转:Javascript中最常用的55个经典技巧 RegularExpressionValidator控件中正则表达式用法~ 正则表达式。。 SQL 2005 EXPRESS版本不能附加数据库的解决办法。。
Asp.net中实现网络通讯之TCP通讯
Yeemio · 2007-08-30 · via 博客园 - Yeemio

2007-08-30 00:26  Yeemio  阅读(2405)  评论()    收藏  举报

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;

namespace WebApplication1.UseTcp
{
    
public partial class TcpServer : System.Web.UI.Page
    
{
        
//设置端口,客户端和这个一样
        private const int portNum = 15
        
protected void Page_Load(object sender, EventArgs e)
        
{

        }


        
protected void stBtn_Click(object sender, EventArgs e)
        
{
            
bool done = false;
            TcpListener listener 
= new TcpListener(portNum);
            listener.Start();
            
while (!done)
            
{
                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
                
{
                    ns.Write(byteTime, 
0, byteTime.Length);
                    ns.Close();
                    client.Close();
                }

                
catch (Exception ex)
                
{
                    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
{
    
public partial class _Default : System.Web.UI.Page
    
{
        
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)
        
{

        }


        
protected void Button1_Click(object sender, EventArgs e)
        
{
            
try
            
{
                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)
            
{
                Response.Write(ex.Message.ToString());
            }
 
        }

    }

}

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