You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.8 KiB
80 lines
1.8 KiB
4 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Net;
|
||
|
using System.Net.Sockets;
|
||
|
|
||
|
namespace EQUIPINTERFACETEST
|
||
|
{
|
||
|
public class LeafTCPClient
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// 当前客户端名称
|
||
|
/// </summary>
|
||
|
private string _Name = "未定义";
|
||
|
public string Name
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return _Name;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void SetName()
|
||
|
{
|
||
|
if (_NetWork.Connected)
|
||
|
{
|
||
|
IPEndPoint iepR = (IPEndPoint)_NetWork.Client.RemoteEndPoint;
|
||
|
IPEndPoint iepL = (IPEndPoint)_NetWork.Client.LocalEndPoint;
|
||
|
//_Name = iepL.Port + "->" + iepR.ToString();
|
||
|
_Name = iepR.ToString();
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// TCP客户端
|
||
|
/// </summary>
|
||
|
private TcpClient _NetWork = null;
|
||
|
public TcpClient NetWork
|
||
|
{
|
||
|
get
|
||
|
{
|
||
|
return _NetWork;
|
||
|
}
|
||
|
set
|
||
|
{
|
||
|
_NetWork = value;
|
||
|
SetName();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// 数据接收缓存区
|
||
|
/// </summary>
|
||
|
public byte[] buffer = new byte[1024];
|
||
|
|
||
|
/// <summary>
|
||
|
/// 断开客户端连接
|
||
|
/// </summary>
|
||
|
public void DisConnect()
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
if (_NetWork != null && _NetWork.Connected)
|
||
|
{
|
||
|
NetworkStream ns = _NetWork.GetStream();
|
||
|
ns.Close();
|
||
|
_NetWork.Close();
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
//MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|