添加项目文件。
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
/********************************************************************
|
||||
* *
|
||||
* * Copyright (C) 2013-? Corporation All rights reserved.
|
||||
* * 作者: BinGoo QQ:315567586
|
||||
* * 请尊重作者劳动成果,请保留以上作者信息,禁止用于商业活动。
|
||||
* *
|
||||
* * 创建时间:2014-08-05
|
||||
* * 说明:
|
||||
* *
|
||||
********************************************************************/
|
||||
namespace SocketHelper
|
||||
{
|
||||
public class DelegateHelper
|
||||
{
|
||||
#region 委托方法
|
||||
/// <summary>
|
||||
/// 接收数据委托方法
|
||||
/// </summary>
|
||||
public static SocketReadCallBack SocketReceive;
|
||||
/// <summary>
|
||||
/// 接收数据的委托
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
public delegate void SocketReadCallBack(string msg);
|
||||
|
||||
/// <summary>
|
||||
/// 接收信息委托方法
|
||||
/// </summary>
|
||||
public static SocketReadInfoCallBack SocketInfo;
|
||||
/// <summary>
|
||||
/// 接收数据的委托
|
||||
/// </summary>
|
||||
/// <param name="msg"></param>
|
||||
public delegate void SocketReadInfoCallBack(string msg);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的常规信息通过以下
|
||||
// 特性集控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("SocketHelper")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("SocketHelper")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2014")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 使此程序集中的类型
|
||||
// 对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型,
|
||||
// 则将该类型上的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("0bbfb4a9-e262-4bbc-a53c-bd61b56c7cf9")]
|
||||
|
||||
// 程序集的版本信息由下面四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 生成号
|
||||
// 修订号
|
||||
//
|
||||
// 可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
|
||||
// 方法是按如下所示使用“*”:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -0,0 +1,53 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{2E9AC112-75CC-4FB6-B058-F9C7424514EF}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>SocketHelper</RootNamespace>
|
||||
<AssemblyName>SocketHelper</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>..\..\..\..\JY.Inspection\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DelegateHelper.cs" />
|
||||
<Compile Include="TCPClient.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
@@ -0,0 +1,256 @@
|
||||
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
namespace SocketHelper
|
||||
{
|
||||
public class TCPClient
|
||||
{
|
||||
#region 属性
|
||||
private string _serverip;
|
||||
/// <summary>
|
||||
/// 服务端IP
|
||||
/// </summary>
|
||||
public string ServerIp
|
||||
{
|
||||
set { _serverip = value; }
|
||||
get { return _serverip; }
|
||||
}
|
||||
private int _serverport;
|
||||
/// <summary>
|
||||
/// 服务端监听端口
|
||||
/// </summary>
|
||||
public int ServerPort
|
||||
{
|
||||
set { _serverport = value; }
|
||||
get { return _serverport; }
|
||||
}
|
||||
private TcpClient _tcpclient = null;
|
||||
/// <summary>
|
||||
/// TcpClient客户端
|
||||
/// </summary>
|
||||
public TcpClient Tcpclient
|
||||
{
|
||||
set { _tcpclient = value; }
|
||||
get { return _tcpclient; }
|
||||
}
|
||||
private Thread _tcpthread = null;
|
||||
/// <summary>
|
||||
/// Tcp客户端连接线程
|
||||
/// </summary>
|
||||
public Thread Tcpthread
|
||||
{
|
||||
set { _tcpthread = value; }
|
||||
get { return _tcpthread; }
|
||||
}
|
||||
private bool _isStarttcpthreading = false;
|
||||
/// <summary>
|
||||
/// 是否启动Tcp连接线程
|
||||
/// </summary>
|
||||
public bool IsStartTcpthreading
|
||||
{
|
||||
set { _isStarttcpthreading = value; }
|
||||
get { return _isStarttcpthreading; }
|
||||
}
|
||||
private bool _isclosed;
|
||||
/// <summary>
|
||||
/// 连接是否关闭
|
||||
/// </summary>
|
||||
public bool Isclosed
|
||||
{
|
||||
set { _isclosed = value; }
|
||||
get { return _isclosed; }
|
||||
}
|
||||
private string _receivestr;
|
||||
/// <summary>
|
||||
/// 接收Socket数据包 缓存字符串
|
||||
/// </summary>
|
||||
public string Receivestr
|
||||
{
|
||||
set { _receivestr = value; }
|
||||
get { return _receivestr; }
|
||||
}
|
||||
/// <summary>
|
||||
/// 重连次数
|
||||
/// </summary>
|
||||
private int _reConectedCount = 0;
|
||||
public int ReConectedCount {
|
||||
get { return _reConectedCount; }
|
||||
set { _reConectedCount = value; }
|
||||
}
|
||||
|
||||
#endregion
|
||||
#region 方法
|
||||
/// <summary>
|
||||
/// 十六进制字字符串转为节数组
|
||||
/// </summary>
|
||||
/// <param name="s"></param>
|
||||
/// <returns></returns>
|
||||
public static byte[] StringToHexByteArray(string s)
|
||||
{
|
||||
s = s.Replace(" ", "");
|
||||
if ((s.Length % 2) != 0)
|
||||
s += " ";
|
||||
byte[] returnBytes = new byte[s.Length / 2];
|
||||
for (int i = 0; i < returnBytes.Length; i++)
|
||||
returnBytes[i] = Convert.ToByte(s.Substring(i * 2, 2), 16);
|
||||
return returnBytes;
|
||||
}
|
||||
/// <summary>
|
||||
/// 启动连接Socket服务器
|
||||
/// </summary>
|
||||
public void StartConnection()
|
||||
{
|
||||
try
|
||||
{
|
||||
Isclosed = false;
|
||||
CreateTcpClient();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//LogHelper.WriteLog(ex.Message);
|
||||
DelegateHelper.SocketInfo("错误信息:" + ex.Message);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 创建线程连接
|
||||
/// </summary>
|
||||
private void CreateTcpClient()
|
||||
{
|
||||
if (Isclosed)
|
||||
return;
|
||||
|
||||
Tcpclient = new TcpClient();
|
||||
Tcpthread = new Thread(StartTcpThread);
|
||||
IsStartTcpthreading = true;
|
||||
Tcpthread.Start();
|
||||
}
|
||||
/// <summary>
|
||||
/// 线程接收Socket上传的数据
|
||||
/// </summary>
|
||||
private void StartTcpThread()
|
||||
{
|
||||
byte[] receivebyte = new byte[128];
|
||||
int bytelen;
|
||||
try
|
||||
{
|
||||
while (IsStartTcpthreading)
|
||||
{
|
||||
if (!Tcpclient.Connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (ReConectedCount != 0)
|
||||
{
|
||||
DelegateHelper.SocketInfo(string.Format("正在第{0}次重新连接FMS服务器... ...", ReConectedCount));
|
||||
}
|
||||
else
|
||||
{
|
||||
DelegateHelper.SocketInfo("正在连接FMS服务器... ...");
|
||||
}
|
||||
Tcpclient.Connect(IPAddress.Parse(ServerIp), ServerPort);
|
||||
DelegateHelper.SocketInfo("已连接FMS服务器");
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
//连接失败
|
||||
ReConectedCount++;
|
||||
IsStartTcpthreading = false;
|
||||
Thread.Sleep(3000);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
bytelen = Tcpclient.Client.Receive(receivebyte);
|
||||
// 连接断开
|
||||
if (bytelen == 0)
|
||||
{
|
||||
DelegateHelper.SocketInfo("与服务器断开连接... ...");
|
||||
ReConectedCount = 1;
|
||||
IsStartTcpthreading = false;
|
||||
continue;
|
||||
}
|
||||
Receivestr = ASCIIEncoding.Default.GetString(receivebyte, 0, bytelen);
|
||||
if (Receivestr.Trim() != "")
|
||||
{
|
||||
//接收数据
|
||||
DelegateHelper.SocketReceive(Receivestr);
|
||||
}
|
||||
}
|
||||
CreateTcpClient();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 异常退出时、需要重新连接
|
||||
CreateTcpClient();
|
||||
DelegateHelper.SocketInfo("错误信息:" + ex.Message);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送Socket消息
|
||||
/// </summary>
|
||||
/// <param name="cmdstr"></param>
|
||||
public void SendCommand(string cmdstr)
|
||||
{
|
||||
try
|
||||
{
|
||||
//byte[] _out=Encoding.GetEncoding("GBK").GetBytes(cmdstr);
|
||||
byte[] _out = Encoding.Default.GetBytes(cmdstr);
|
||||
Tcpclient.Client.Send(_out);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 发送Socket消息
|
||||
/// </summary>
|
||||
/// <param name="byteMsg"></param>
|
||||
public void SendCommand(byte[] byteMsg)
|
||||
{
|
||||
try
|
||||
{
|
||||
Tcpclient.Client.Send(byteMsg);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DelegateHelper.SocketInfo("错误信息:" + ex.Message);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void Close()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (Tcpclient.Client.Connected)
|
||||
{
|
||||
Tcpclient.Client.Close();
|
||||
Tcpclient.Client.Disconnect(true);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DelegateHelper.SocketInfo("错误信息:" + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
#region 构造函数
|
||||
/// <summary>
|
||||
/// 初始化TCPClient类
|
||||
/// </summary>
|
||||
/// <param name="ip">服务端IP</param>
|
||||
/// <param name="port">监听端口</param>
|
||||
public TCPClient(string ip, int port)
|
||||
{
|
||||
ServerIp = ip;
|
||||
ServerPort = port;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user