public class TcpClientSession
{
protected TcpClient Client { get; set; }
/// <summary>
/// 远程地址
/// </summary>
protected IPEndPoint RemoteEndPoint { get; set; }
/// <summary>
/// 是否已经连接
/// </summary>
public bool IsConnected { get; private set; }
/// <summary>
/// 接收缓存区大小
/// </summary>
public int ReceiveBufferSize = ;
/// <summary>
/// 数据流对象
/// </summary>
protected NetworkStream _NetStream; /// <summary>
/// 已连接事件
/// </summary>
public event Action OnConnected;
/// <summary>
/// 断开事件
/// </summary>
public event Action OnClosed; public TcpClientSession(IPEndPoint remoteEndPoint)
{
if (remoteEndPoint == null)
throw new ArgumentNullException("remoteEndPoint"); RemoteEndPoint = remoteEndPoint;
} public TcpClientSession(string server, int port)
{
if (server != null && port > )
{
if (!Regex.IsMatch(server, @"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})"))
{
try
{
IPHostEntry ipHostEntry = System.Net.Dns.GetHostEntry(server);
server = ipHostEntry.AddressList[].ToString();
}
catch (Exception)
{
throw new ArgumentNullException("远程IP地址或域名不正确");
}
} RemoteEndPoint = new IPEndPoint(IPAddress.Parse(server), port);
}
else
throw new ArgumentNullException("remoteEndPoint");
} public void Connect()
{
Client = new TcpClient(RemoteEndPoint.AddressFamily);
Client.ReceiveBufferSize = ReceiveBufferSize;
Client.Connect(RemoteEndPoint);
if (Client.Client.Connected)
{
_NetStream = Client.GetStream(); IsConnected = true;
if (this.OnConnected != null)
OnConnected();
}
else
throw new Exception("Unable to connect to a remote device"); } public byte[] Received()
{
if (Client.Client.Connected)
{
byte[] buffer = null;
buffer = new byte[ReceiveBufferSize];
_NetStream.Read(buffer, , buffer.Length);
return buffer;
}
else
{
Close();
}
return null;
} public void Send(byte[] bs)
{
if (Client.Client.Connected)
{
_NetStream.Write(bs, , bs.Length);
}
else
{
Close();
}
} public void Close()
{
if (Client.Client.Connected)
{
Client.Close();
_NetStream.Close();
} IsConnected = false;
if (this.OnClosed != null)
OnClosed();
}
}

TcpClient的更多相关文章

  1. C# - 网络编程 之 TcpClient与TcpListener

    TcpClient类 TcpListener类 TCP通信 UDP通信 参考:

  2. TcpClient 有好多坑

    下面2篇文章里头的问题都碰到了,真是好坑哈, 在此留念. 使用 TcpClient 與 NetworkStream 類別開發時的注意事項 [C#] NetworkStream.Write()存在严重b ...

  3. Socket聊天室-TcpListener,TcpClient

    参考自:http://blog.csdn.net/liguo9860/article/details/6148614 服务端:

  4. TcpClient类与TcpListener类

    TcpClient类 //构造方法1 TcpClient t = new TcpClient(); t.Connect(); //构造方法2 IPEndPoint iep = ); TcpClient ...

  5. Socket的三个功能类TCPClient、TCPListener 和 UDPClient (转)

    应用程序可以通过 TCPClient.TCPListener 和 UDPClient 类使用传输控制协议 (TCP) 和用户数据文报协议 (UDP) 服务.这些协议类建立在 System.Net.So ...

  6. c# TCPclient

    服务端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...

  7. C# TcpClient TcpListener 简单练习01

    下面是读<Visual C#.Net 网络编程>整理的练习代码. 客户端发送命令给服务端,从服务器端获取所有人员的成绩或者指定人员的成绩. 命令格式为 GET 0|1 [Name].0为获 ...

  8. Socket与TcpClient的区别(转载)

    Socket和TcpClient有什么区别 原文:http://wxwinter.spaces.live.com/blog/cns!C36588978AFC344A!322.entry 回答: &qu ...

  9. TcpClient 错误"不能做任何连接,因为目标机器积极地拒绝它" 的解决

    TcpClient 错误"不能做任何连接,因为目标机器积极地拒绝它" 的解决 //以下是tcpclient服务器端的监听程序,假设服务器端和客户端在同一台机器上,//为了使客户端可 ...

  10. C#TCPClient应用-一个简单的消息发送和接收

    TcpSend窗口用于发送消息,另外写一个用于接收消息的应用程序,消息接受到以后,必须要关闭接收消息的窗口,才能在接收新的消息,不知道怎么能解决这个问题. 源代码: 发送消息的窗口代码 using S ...

随机推荐

  1. yum安裝的包如何保留到本地

    一, 很多时候,我们一直用yum安装的软件,但是毫无疑问,很多人都会想yum安装的软件的包存放在哪里了呢? 这是因为yum默认并不保存你所安装的包,那么如何才能保留安装的软件包呢? 方法很简单:修改y ...

  2. UVA 12125 March of the Penguins

    题意: 给定一些冰块,每个冰块上有一些企鹅,每个冰块有一个可以跳出的次数限制,每个冰块位于一个坐标,现在每个企鹅跳跃力为d,问所有企鹅能否跳到一点上,如果可以输出所有落脚冰块,如果没有方案就打印-1 ...

  3. win10下安装通过Hyper-v安装Ubuntu

    一直也来在做C#的开发,Winform及Web都有所涉及,想在闲暇之余学习下Python,拓展一下自己的知识.既然决定学习Python那么就直接在Linux下进行吧,由于Ubuntu最近很火而且也有方 ...

  4. 伪元素”:after” , “:before"

    伪元素就是源码html中不存在,而视觉上又存在的元素     简单用法: blockquote:before {      content: open-quote;      // 其他样式 } // ...

  5. Redis 作为缓存服务器的配置

    随着redis的发展,越来越多的架构用它取代了memcached作为缓存服务器的角色,它有几个很突出的特点:1. 除了Hash,还提供了Sorted Set, List等数据结构2. 可以持久化到磁盘 ...

  6. SSO单点登录(转载)

    昨天和几位朋友探讨到了这个话题,发现虽然单点登录,或者叫做独立的passport登录虽然已经有了很多实现方法,但是能真正了解并实现的人却并不太多,所以些下此文,希望从原理到实现,能让大家了解的多一些 ...

  7. Nginx 配置指令的执行顺序(八)

    前面我们详细讨论了 rewrite.access 和 content 这三个最为常见的 Nginx 请求处理阶段,在此过程中,也顺便介绍了运行在这三个阶段的众多 Nginx 模块及其配置指令.同时可以 ...

  8. XML的DTD约束

    DTD约束:DTD的声明和引用 1.内部DTD文档 <!DOCTYPE 根元素 [定义内容]> 2.外部DTD文档 <!DOCTYPE 根元素 SYSTEM "DTD文件路 ...

  9. 【stm32】用TIM1产生6路ADC,用CCR4触发ADC1的注入通道采样

    这几天一直在使用STM32来写sensorless BLDC的驱动框架,那么必须会用到TIM1的CCR1/CCR2/CCR3产生的六路互补PWM,以及用CCR4来产生一个中断,用来在PWM-ON的时候 ...

  10. EPiServer 简单项目总结

    国内用到的EPiServer应该不多,所以记录点用到过的东西,以便分享 1.EPiServer office site http://www.episerver.com/ 2.EPiServer CM ...