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. SQL查询多行合并成一行

    问题描述:无论是在sql 2000,还是在 sql 2005 中,都没有提供字符串的聚合函数,  所以,当我们在处理下列要求时,会比较麻烦:有表tb, 如下:id    value----- ---- ...

  2. function overloading/ declare function

    Declare a function To declare a function without identifying the argument list, you can do it in thi ...

  3. Lua编程入门-学习笔记1

    第1章:起点 Chunks: 语句块 每个语句结尾的分号是可选的,如果同一行有多个语句最好使用分号分隔: dofile("lib1.lua")  -- 执行lua文件 全局变量:局 ...

  4. 【Android 错误记录】Conversion to Dalvik format failed with error 1 错误

    错误原因:依赖的包中有冲突,比如依赖了同一个jar包的不同版本等   在以往测试的过程中,出现过几次这个问题,根本原因都是因为有冲突了,但是表现形式可能不一样   情况1: 有一个叫DemoAPP的工 ...

  5. Java路径问题最终解决方案—可定位所有资源的相对路径寻址

    1.在Java项目中,应该通过绝对路径访问文件,以下为访问的常用方法: 第一种方法:类名.class.getResource("/").getPath()+文件名 第二种方法:Th ...

  6. Python Tkinter canvas oval原理

    Ovals, mathematically, are ellipses, including circles as a special case. The ellipse is fit into a ...

  7. 小端存储(little Endian)大端存储(big Endian)

    小端存储--低内存低字节 87654321 字节或半字节的最低位字节(Lowest Significant Bit,LSB)存放于内存最低位字节地址上.即最低地址存放的最低字节,为Power PC,I ...

  8. Python即时网络爬虫:API说明

    API说明——下载gsExtractor内容提取器 1,接口名称 下载内容提取器 2,接口说明 如果您想编写一个网络爬虫程序,您会发现大部分时间耗费在调测网页内容提取规则上,不讲正则表达式的语法如何怪 ...

  9. Leetcode 171 Excel Sheet Column Number python

    题目: Given a column title as appear in an Excel sheet, return its corresponding column number. For ex ...

  10. UVA 12657 Boxes in a Line

    双向链表 注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理: 指令3 中交换盒子要注意两个盒子相邻的情况 #include <iostream> #inclu ...