TcpClient
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的更多相关文章
- C# - 网络编程 之 TcpClient与TcpListener
TcpClient类 TcpListener类 TCP通信 UDP通信 参考:
- TcpClient 有好多坑
下面2篇文章里头的问题都碰到了,真是好坑哈, 在此留念. 使用 TcpClient 與 NetworkStream 類別開發時的注意事項 [C#] NetworkStream.Write()存在严重b ...
- Socket聊天室-TcpListener,TcpClient
参考自:http://blog.csdn.net/liguo9860/article/details/6148614 服务端:
- TcpClient类与TcpListener类
TcpClient类 //构造方法1 TcpClient t = new TcpClient(); t.Connect(); //构造方法2 IPEndPoint iep = ); TcpClient ...
- Socket的三个功能类TCPClient、TCPListener 和 UDPClient (转)
应用程序可以通过 TCPClient.TCPListener 和 UDPClient 类使用传输控制协议 (TCP) 和用户数据文报协议 (UDP) 服务.这些协议类建立在 System.Net.So ...
- c# TCPclient
服务端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...
- C# TcpClient TcpListener 简单练习01
下面是读<Visual C#.Net 网络编程>整理的练习代码. 客户端发送命令给服务端,从服务器端获取所有人员的成绩或者指定人员的成绩. 命令格式为 GET 0|1 [Name].0为获 ...
- Socket与TcpClient的区别(转载)
Socket和TcpClient有什么区别 原文:http://wxwinter.spaces.live.com/blog/cns!C36588978AFC344A!322.entry 回答: &qu ...
- TcpClient 错误"不能做任何连接,因为目标机器积极地拒绝它" 的解决
TcpClient 错误"不能做任何连接,因为目标机器积极地拒绝它" 的解决 //以下是tcpclient服务器端的监听程序,假设服务器端和客户端在同一台机器上,//为了使客户端可 ...
- C#TCPClient应用-一个简单的消息发送和接收
TcpSend窗口用于发送消息,另外写一个用于接收消息的应用程序,消息接受到以后,必须要关闭接收消息的窗口,才能在接收新的消息,不知道怎么能解决这个问题. 源代码: 发送消息的窗口代码 using S ...
随机推荐
- stagefright框架(一)Video Playback的流程
在Android上,預設的多媒體框架(multimedia framework)是OpenCORE. OpenCORE的優點是兼顧了跨平台的移植性,而且已經過多方驗證,所以相對來說較為穩定:但是其缺點 ...
- CSS的base文件常用代码
article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body,but ...
- java学习——数组
元素类型[] 数组名 = new 元素类型[元素个数或数组长度]; array 为引用数据类型|-数组数据类型 | 内存结构:程序在运行时,需要在内存中的分配空间.为了提高运行的效率,有对空间进行不同 ...
- Xcode5 配置 github
首先,要在github上,进行如下的操作: 1. github 官网 https://github.com 注册github账号. 2. 创建一个repository,命名为项目的名称,如 Gith ...
- java web 之 WebRoot和WebContent目录
WebRoot和WebContent都是程序的根文件夹,无本质区别,一下是两者的共同点和不同点: 共同点:都有一个WEB-INF文件夹,其下文件不可直接访问: WEB-INF是安全目录,所谓安全,就是 ...
- git 快速使用(本地仓库同步到远程仓库)
学git一段时间,可惜公司用的是svn,平时少用,又忘了,总结一下,免得下次又得重新学习.得多多用才是正道! 一. 将本地的提交到网上git仓库 1.在git创建仓库 ...
- Centos6.5下安装php
安装php: yum -y install php 重启httpd服务激活php: /etc/init.d/httpd restart 测试php是否安装完成 vim /var/www/html/in ...
- SQL Server 解决CPU 高占用的一般step
第一步: 看这些CPU是不是SQL Server用的. 第二步: 确定SQL Server 有没有引发17883\17884错误 第三步: 找出使用CPU最高的语句进行优化.(sys.dm_exec_ ...
- Nginx Upload Module 上传模块
传统站点在处理文件上传请求时,普遍使用后端编程语言处理,如:Java.PHP.Python.Ruby等.今天给大家介绍Nginx的一个模块,Upload Module上传模块,此模块的原理是先把用户上 ...
- JavaScript 问答 - No.1
1 什么是JavaScript? JavaScript 是世界上最流行的脚本语言. JavaScript 是属于 web 的语言,它适用于 PC.笔记本电脑.平板电脑和移动电话. JavaSc ...