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

推荐订阅源

S
Schneier on Security
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
D
DataBreaches.Net
F
Full Disclosure
腾讯CDC
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Schneier on Security
Schneier on 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,然后先启动服务器端,再启动客户端,这时候就可以测试了。服务端把当前时间做为数据传送到客户端,可以点客户端按钮观察变化。