基线 SVN r240
SVN-Revision: r240
This commit is contained in:
Generated
+158
@@ -0,0 +1,158 @@
|
||||
namespace Lskj.SocketClient
|
||||
{
|
||||
partial class Form1
|
||||
{
|
||||
/// <summary>
|
||||
/// 必需的设计器变量。
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// 清理所有正在使用的资源。
|
||||
/// </summary>
|
||||
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows 窗体设计器生成的代码
|
||||
|
||||
/// <summary>
|
||||
/// 设计器支持所需的方法 - 不要
|
||||
/// 使用代码编辑器修改此方法的内容。
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.button1 = new System.Windows.Forms.Button();
|
||||
this.button2 = new System.Windows.Forms.Button();
|
||||
this.textBox1 = new System.Windows.Forms.TextBox();
|
||||
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
|
||||
this.label1 = new System.Windows.Forms.Label();
|
||||
this.label2 = new System.Windows.Forms.Label();
|
||||
this.label3 = new System.Windows.Forms.Label();
|
||||
this.button3 = new System.Windows.Forms.Button();
|
||||
this.button4 = new System.Windows.Forms.Button();
|
||||
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
|
||||
this.SuspendLayout();
|
||||
//
|
||||
// button1
|
||||
//
|
||||
this.button1.Location = new System.Drawing.Point(13, 13);
|
||||
this.button1.Name = "button1";
|
||||
this.button1.Size = new System.Drawing.Size(75, 23);
|
||||
this.button1.TabIndex = 0;
|
||||
this.button1.Text = "连接";
|
||||
this.button1.UseVisualStyleBackColor = true;
|
||||
this.button1.Click += new System.EventHandler(this.button1_Click);
|
||||
//
|
||||
// button2
|
||||
//
|
||||
this.button2.Location = new System.Drawing.Point(103, 13);
|
||||
this.button2.Name = "button2";
|
||||
this.button2.Size = new System.Drawing.Size(75, 23);
|
||||
this.button2.TabIndex = 1;
|
||||
this.button2.Text = "上传";
|
||||
this.button2.UseVisualStyleBackColor = true;
|
||||
this.button2.Click += new System.EventHandler(this.button2_Click);
|
||||
//
|
||||
// textBox1
|
||||
//
|
||||
this.textBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
|
||||
this.textBox1.Location = new System.Drawing.Point(0, 173);
|
||||
this.textBox1.Multiline = true;
|
||||
this.textBox1.Name = "textBox1";
|
||||
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
|
||||
this.textBox1.Size = new System.Drawing.Size(604, 249);
|
||||
this.textBox1.TabIndex = 2;
|
||||
//
|
||||
// openFileDialog1
|
||||
//
|
||||
this.openFileDialog1.FileName = "openFileDialog1";
|
||||
//
|
||||
// label1
|
||||
//
|
||||
this.label1.AutoSize = true;
|
||||
this.label1.Location = new System.Drawing.Point(12, 102);
|
||||
this.label1.Name = "label1";
|
||||
this.label1.Size = new System.Drawing.Size(41, 12);
|
||||
this.label1.TabIndex = 3;
|
||||
this.label1.Text = "label1";
|
||||
//
|
||||
// label2
|
||||
//
|
||||
this.label2.AutoSize = true;
|
||||
this.label2.Location = new System.Drawing.Point(12, 123);
|
||||
this.label2.Name = "label2";
|
||||
this.label2.Size = new System.Drawing.Size(41, 12);
|
||||
this.label2.TabIndex = 4;
|
||||
this.label2.Text = "label2";
|
||||
//
|
||||
// label3
|
||||
//
|
||||
this.label3.AutoSize = true;
|
||||
this.label3.Location = new System.Drawing.Point(12, 146);
|
||||
this.label3.Name = "label3";
|
||||
this.label3.Size = new System.Drawing.Size(41, 12);
|
||||
this.label3.TabIndex = 5;
|
||||
this.label3.Text = "label3";
|
||||
//
|
||||
// button3
|
||||
//
|
||||
this.button3.Location = new System.Drawing.Point(103, 42);
|
||||
this.button3.Name = "button3";
|
||||
this.button3.Size = new System.Drawing.Size(75, 23);
|
||||
this.button3.TabIndex = 6;
|
||||
this.button3.Text = "下载";
|
||||
this.button3.UseVisualStyleBackColor = true;
|
||||
this.button3.Click += new System.EventHandler(this.button3_Click);
|
||||
//
|
||||
// button4
|
||||
//
|
||||
this.button4.Location = new System.Drawing.Point(13, 42);
|
||||
this.button4.Name = "button4";
|
||||
this.button4.Size = new System.Drawing.Size(75, 23);
|
||||
this.button4.TabIndex = 0;
|
||||
this.button4.Text = "连接";
|
||||
this.button4.UseVisualStyleBackColor = true;
|
||||
this.button4.Click += new System.EventHandler(this.button4_Click);
|
||||
//
|
||||
// Form1
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(604, 422);
|
||||
this.Controls.Add(this.button3);
|
||||
this.Controls.Add(this.label3);
|
||||
this.Controls.Add(this.label2);
|
||||
this.Controls.Add(this.label1);
|
||||
this.Controls.Add(this.textBox1);
|
||||
this.Controls.Add(this.button2);
|
||||
this.Controls.Add(this.button4);
|
||||
this.Controls.Add(this.button1);
|
||||
this.Name = "Form1";
|
||||
this.Text = "Form1";
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private System.Windows.Forms.Button button1;
|
||||
private System.Windows.Forms.Button button2;
|
||||
private System.Windows.Forms.TextBox textBox1;
|
||||
private System.Windows.Forms.OpenFileDialog openFileDialog1;
|
||||
private System.Windows.Forms.Label label1;
|
||||
private System.Windows.Forms.Label label2;
|
||||
private System.Windows.Forms.Label label3;
|
||||
private System.Windows.Forms.Button button3;
|
||||
private System.Windows.Forms.Button button4;
|
||||
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,210 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.Threading;
|
||||
using System.IO;
|
||||
|
||||
namespace Lskj.SocketClient
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
/// <summary>
|
||||
/// 每次上传数据包大小
|
||||
/// </summary>
|
||||
public static int UploadPacketSize = 32 * 1024;
|
||||
/// <summary>
|
||||
/// 每次下载数据包大小
|
||||
/// </summary>
|
||||
public static int DownLoadPacketSize = 64 * 1024;
|
||||
private ClientUploadSocket _uploadSocket;
|
||||
private ClientDownLoadSocket _downSocket;
|
||||
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
_uploadSocket = new ClientUploadSocket();
|
||||
_uploadSocket.Connect("127.0.0.1", 9999);
|
||||
ShowMsg("Connect Server Success.127.0.0.1:9999");
|
||||
_uploadSocket.DoActive();
|
||||
_uploadSocket.DoLogin("admin", "admin");
|
||||
ShowMsg("Login Server Success");
|
||||
ShowMsg("Please Input Upload FileName");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowMsg("Connect Server Fault." + ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private void button2_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (openFileDialog1.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
for (int i = 0; i < 3; i++) //发送失败后,尝试3次重发
|
||||
{
|
||||
if (SendFile(openFileDialog1.FileName))
|
||||
{
|
||||
ShowMsg("Upload File Success");
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(3 * 1000); //发送失败等待3S后重连
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowMsg("SendFile Fault." + ex.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
private bool SendFile(string fileName)
|
||||
{
|
||||
FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
long fileSize = fileStream.Length;
|
||||
if (!_uploadSocket.DoUpload("", Path.GetFileName(fileName), ref fileSize))
|
||||
throw new Exception(_uploadSocket.ErrorString);
|
||||
fileStream.Position = fileSize;
|
||||
label1.Text = Path.GetFileName(fileName) + ":" + fileSize + "/" + Convert.ToInt32(fileStream.Length / 1024 / 1024) + "MB";
|
||||
byte[] readBuffer = new byte[UploadPacketSize];
|
||||
long uploadSize = 0;
|
||||
while (fileStream.Position < fileStream.Length)
|
||||
{
|
||||
int count = fileStream.Read(readBuffer, 0, UploadPacketSize);
|
||||
if (!_uploadSocket.DoData(readBuffer, 0, count))
|
||||
throw new Exception(_uploadSocket.ErrorString);
|
||||
uploadSize += count;
|
||||
Application.DoEvents();
|
||||
label1.Text = Path.GetFileName(fileName) + ":" + Convert.ToInt32(uploadSize / 1024 / 1024) + "MB/" + Convert.ToInt32(fileStream.Length / 1024 / 1024) + "MB";
|
||||
ShowMsg("本地传输数据大小:" + uploadSize);
|
||||
}
|
||||
if (!_uploadSocket.DoEof(fileStream.Length))
|
||||
throw new Exception(_uploadSocket.ErrorString);
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception E)
|
||||
{
|
||||
ShowMsg("Upload File Error: " + E.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
fileStream.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public bool DownFile(string fileName)
|
||||
{
|
||||
FileStream fileStream = null;
|
||||
try
|
||||
{
|
||||
saveFileDialog1.FileName = fileName;
|
||||
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
long localSize = 0;
|
||||
long fileSize = 0;
|
||||
if (File.Exists(saveFileDialog1.FileName))
|
||||
{
|
||||
fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);
|
||||
localSize = fileStream.Length;
|
||||
}
|
||||
|
||||
if (!_downSocket.DoDownLoad("", fileName, ref fileSize))
|
||||
throw new Exception(_uploadSocket.ErrorString);
|
||||
ShowMsg(fileName + string.Format("({0}/{1}kb)", localSize, fileSize));
|
||||
// 开始下载
|
||||
byte[] readBuffer = new byte[DownLoadPacketSize];
|
||||
long downloadSize = localSize;
|
||||
//while (fileStream.Position < fileStream.Length)
|
||||
//{
|
||||
// fileStream.Write(readBuffer, 0, DownLoadPacketSize);
|
||||
|
||||
// if (!_uploadSocket.DoData(readBuffer, 0, count))
|
||||
// throw new Exception(_uploadSocket.ErrorString);
|
||||
// uploadSize += count;
|
||||
// Application.DoEvents();
|
||||
// label1.Text = Path.GetFileName(fileName) + ":" + Convert.ToInt32(uploadSize / 1024 / 1024) + "MB/" + Convert.ToInt32(fileStream.Length / 1024 / 1024) + "MB";
|
||||
// ShowMsg("本地下载数据大小:" + uploadSize);
|
||||
//}
|
||||
//if (!_uploadSocket.DoEof(fileStream.Length))
|
||||
// throw new Exception(_uploadSocket.ErrorString);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowMsg("DownLoadFile Fault." + ex.StackTrace);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (fileStream != null) fileStream.Close();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void ShowMsg(string msg)
|
||||
{
|
||||
this.Invoke((EventHandler)delegate
|
||||
{
|
||||
this.textBox1.Text += msg + "\r\n";
|
||||
this.textBox1.ScrollToCaret();
|
||||
});
|
||||
}
|
||||
|
||||
private void button3_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
string fileName = "代码注释工具.rar";
|
||||
for (int i = 0; i < 3; i++) //发送失败后,尝试3次重发
|
||||
{
|
||||
if (DownFile(fileName))
|
||||
{
|
||||
ShowMsg("DownLoad File Success");
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(3 * 1000); //发送失败等待3S后重连
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowMsg("SendFile Fault." + ex.StackTrace);
|
||||
}
|
||||
}
|
||||
|
||||
private void button4_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
_downSocket = new ClientDownLoadSocket();
|
||||
_downSocket.Connect("127.0.0.1", 9999);
|
||||
ShowMsg("Connect Server Success.127.0.0.1:9999");
|
||||
_downSocket.DoActive();
|
||||
_downSocket.DoLogin("admin", "admin");
|
||||
ShowMsg("Login Server Success");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ShowMsg(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<metadata name="openFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>17, 17</value>
|
||||
</metadata>
|
||||
<metadata name="saveFileDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
|
||||
<value>165, 17</value>
|
||||
</metadata>
|
||||
</root>
|
||||
@@ -0,0 +1,99 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
|
||||
<ProductVersion>8.0.30703</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{D8BF9C4A-1680-420F-8B3D-A2FAAE94593F}</ProjectGuid>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Lskj.SocketClient</RootNamespace>
|
||||
<AssemblyName>Lskj.SocketClient</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<TargetFrameworkProfile>
|
||||
</TargetFrameworkProfile>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
|
||||
<PlatformTarget>x86</PlatformTarget>
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Deployment" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="..\Lskj.SocketService\AsyncSocketCore\DynamicBufferManager.cs">
|
||||
<Link>SyncSocketCore\DynamicBufferManager.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Lskj.SocketService\AsyncSocketCore\IncomingDataParser.cs">
|
||||
<Link>SyncSocketCore\IncomingDataParser.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Lskj.SocketService\AsyncSocketCore\OutgoingDataAssembler.cs">
|
||||
<Link>SyncSocketCore\OutgoingDataAssembler.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Lskj.SocketService\AsyncSocketProtocolCore\ProtocolKey.cs">
|
||||
<Link>SyncSocketProtocolCore\ProtocolKey.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="..\Lskj.SocketService\AsyncSocketPublic\BasicFunc.cs">
|
||||
<Link>AsyncSocketPublic\BasicFunc.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Form1.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Form1.Designer.cs">
|
||||
<DependentUpon>Form1.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Program.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SyncSocketCore\SyncSocketInvokeElement.cs" />
|
||||
<Compile Include="SyncSocketProtocolCore\ClientBaseSocket.cs" />
|
||||
<Compile Include="SyncSocketProtocol\ClientDownLoadSocket.cs" />
|
||||
<Compile Include="SyncSocketProtocol\ClientUploadSocket.cs" />
|
||||
<EmbeddedResource Include="Form1.resx">
|
||||
<DependentUpon>Form1.cs</DependentUpon>
|
||||
</EmbeddedResource>
|
||||
<EmbeddedResource Include="Properties\Resources.resx">
|
||||
<Generator>ResXFileCodeGenerator</Generator>
|
||||
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
|
||||
<SubType>Designer</SubType>
|
||||
</EmbeddedResource>
|
||||
<Compile Include="Properties\Resources.Designer.cs">
|
||||
<AutoGen>True</AutoGen>
|
||||
<DependentUpon>Resources.resx</DependentUpon>
|
||||
<DesignTime>True</DesignTime>
|
||||
</Compile>
|
||||
</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,35 @@
|
||||
<GhostDoc>
|
||||
<SpellChecker>
|
||||
<IncludeExtensions>
|
||||
</IncludeExtensions>
|
||||
<IgnoreExtensions>
|
||||
</IgnoreExtensions>
|
||||
<IgnoreFiles>
|
||||
</IgnoreFiles>
|
||||
</SpellChecker>
|
||||
<HelpConfigurations selected="HelpFile">
|
||||
<HelpConfiguration name="HelpFile">
|
||||
<OutputPath>.\Help</OutputPath>
|
||||
<ImageFolderPath />
|
||||
<HtmlFormats>
|
||||
<HtmlHelp>true</HtmlHelp>
|
||||
<MSHelpViewer>false</MSHelpViewer>
|
||||
<MSHelp2>false</MSHelp2>
|
||||
<Website>false</Website>
|
||||
</HtmlFormats>
|
||||
<IncludeScopes>
|
||||
<Public>true</Public>
|
||||
<Internal>false</Internal>
|
||||
<Protected>false</Protected>
|
||||
<Private>false</Private>
|
||||
<Inherited>true</Inherited>
|
||||
<EnableTags>false</EnableTags>
|
||||
<TagList />
|
||||
</IncludeScopes>
|
||||
<ResolveCrefLinks>true</ResolveCrefLinks>
|
||||
<HeaderText />
|
||||
<FooterText />
|
||||
<SelectedProjects />
|
||||
</HelpConfiguration>
|
||||
</HelpConfigurations>
|
||||
</GhostDoc>
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Lskj.SocketClient
|
||||
{
|
||||
static class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// 应用程序的主入口点。
|
||||
/// </summary>
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new Form1());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// 有关程序集的常规信息通过以下
|
||||
// 特性集控制。更改这些特性值可修改
|
||||
// 与程序集关联的信息。
|
||||
[assembly: AssemblyTitle("Lskj.SocketClient")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Lskj.SocketClient")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2018")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// 将 ComVisible 设置为 false 使此程序集中的类型
|
||||
// 对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型,
|
||||
// 则将该类型上的 ComVisible 特性设置为 true。
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
|
||||
[assembly: Guid("658f2349-edae-4588-bb75-15e1f555809b")]
|
||||
|
||||
// 程序集的版本信息由下面四个值组成:
|
||||
//
|
||||
// 主版本
|
||||
// 次版本
|
||||
// 内部版本号
|
||||
// 修订号
|
||||
//
|
||||
// 可以指定所有这些值,也可以使用“内部版本号”和“修订号”的默认值,
|
||||
// 方法是按如下所示使用“*”:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -0,0 +1,63 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// <auto-generated>
|
||||
// 此代码由工具生成。
|
||||
// 运行时版本:4.0.30319.42000
|
||||
//
|
||||
// 对此文件的更改可能会导致不正确的行为,并且如果
|
||||
// 重新生成代码,这些更改将会丢失。
|
||||
// </auto-generated>
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
namespace Lskj.SocketClient.Properties {
|
||||
using System;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 一个强类型的资源类,用于查找本地化的字符串等。
|
||||
/// </summary>
|
||||
// 此类是由 StronglyTypedResourceBuilder
|
||||
// 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
|
||||
// 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
|
||||
// (以 /str 作为命令选项),或重新生成 VS 项目。
|
||||
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
|
||||
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
|
||||
internal class Resources {
|
||||
|
||||
private static global::System.Resources.ResourceManager resourceMan;
|
||||
|
||||
private static global::System.Globalization.CultureInfo resourceCulture;
|
||||
|
||||
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
|
||||
internal Resources() {
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回此类使用的缓存的 ResourceManager 实例。
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Resources.ResourceManager ResourceManager {
|
||||
get {
|
||||
if (object.ReferenceEquals(resourceMan, null)) {
|
||||
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Lskj.SocketClient.Properties.Resources", typeof(Resources).Assembly);
|
||||
resourceMan = temp;
|
||||
}
|
||||
return resourceMan;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用此强类型资源类,为所有资源查找
|
||||
/// 重写当前线程的 CurrentUICulture 属性。
|
||||
/// </summary>
|
||||
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
|
||||
internal static global::System.Globalization.CultureInfo Culture {
|
||||
get {
|
||||
return resourceCulture;
|
||||
}
|
||||
set {
|
||||
resourceCulture = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<root>
|
||||
<!--
|
||||
Microsoft ResX Schema
|
||||
|
||||
Version 2.0
|
||||
|
||||
The primary goals of this format is to allow a simple XML format
|
||||
that is mostly human readable. The generation and parsing of the
|
||||
various data types are done through the TypeConverter classes
|
||||
associated with the data types.
|
||||
|
||||
Example:
|
||||
|
||||
... ado.net/XML headers & schema ...
|
||||
<resheader name="resmimetype">text/microsoft-resx</resheader>
|
||||
<resheader name="version">2.0</resheader>
|
||||
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
|
||||
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
|
||||
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
|
||||
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
|
||||
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
|
||||
<value>[base64 mime encoded serialized .NET Framework object]</value>
|
||||
</data>
|
||||
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
|
||||
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
|
||||
<comment>This is a comment</comment>
|
||||
</data>
|
||||
|
||||
There are any number of "resheader" rows that contain simple
|
||||
name/value pairs.
|
||||
|
||||
Each data row contains a name, and value. The row also contains a
|
||||
type or mimetype. Type corresponds to a .NET class that support
|
||||
text/value conversion through the TypeConverter architecture.
|
||||
Classes that don't support this are serialized and stored with the
|
||||
mimetype set.
|
||||
|
||||
The mimetype is used for serialized objects, and tells the
|
||||
ResXResourceReader how to depersist the object. This is currently not
|
||||
extensible. For a given mimetype the value must be set accordingly:
|
||||
|
||||
Note - application/x-microsoft.net.object.binary.base64 is the format
|
||||
that the ResXResourceWriter will generate, however the reader can
|
||||
read any of the formats listed below.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.binary.base64
|
||||
value : The object must be serialized with
|
||||
: System.Serialization.Formatters.Binary.BinaryFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.soap.base64
|
||||
value : The object must be serialized with
|
||||
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
|
||||
: and then encoded with base64 encoding.
|
||||
|
||||
mimetype: application/x-microsoft.net.object.bytearray.base64
|
||||
value : The object must be serialized into a byte array
|
||||
: using a System.ComponentModel.TypeConverter
|
||||
: and then encoded with base64 encoding.
|
||||
-->
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
||||
@@ -0,0 +1,241 @@
|
||||
/******************************
|
||||
* 说明:Socket传输基类
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-01-23
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Net.Sockets;
|
||||
using Lskj.SocketService;
|
||||
|
||||
namespace Lskj.SocketClient
|
||||
{
|
||||
/// <summary>
|
||||
/// 异步Socket调用对象,所有的协议处理都从本类继承
|
||||
/// </summary>
|
||||
public class SyncSocketInvokeElement
|
||||
{
|
||||
private bool _netByteOrder;
|
||||
/// <summary>
|
||||
/// TCP客户端
|
||||
/// </summary>
|
||||
protected TcpClient _tcpClient;
|
||||
/// <summary>
|
||||
/// 服务器地址
|
||||
/// </summary>
|
||||
protected string _host;
|
||||
/// <summary>
|
||||
/// 端口
|
||||
/// </summary>
|
||||
protected int _port;
|
||||
/// <summary>
|
||||
/// 传输标识
|
||||
/// </summary>
|
||||
protected ProtocolFlag _protocolFlag;
|
||||
/// <summary>
|
||||
/// 传输时间设置
|
||||
/// </summary>
|
||||
/// <value>The socket time out ms.</value>
|
||||
protected int SocketTimeOutMS
|
||||
{
|
||||
get
|
||||
{
|
||||
return _tcpClient.SendTimeout;
|
||||
}
|
||||
set
|
||||
{
|
||||
_tcpClient.SendTimeout = value;
|
||||
_tcpClient.ReceiveTimeout = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 协议组装器,用来组装往外发送的命令
|
||||
/// </summary>
|
||||
protected OutgoingDataAssembler _outgoingDataAssembler;
|
||||
/// <summary>
|
||||
/// 接收数据的缓存
|
||||
/// </summary>
|
||||
protected DynamicBufferManager _recvBuffer;
|
||||
/// <summary>
|
||||
/// 收到数据的解析器,用于解析返回的内容
|
||||
/// </summary>
|
||||
protected IncomingDataParser _incomingDataParser;
|
||||
/// <summary>
|
||||
/// 发送数据的缓存,统一写到内存中,调用一次发送
|
||||
/// </summary>
|
||||
protected DynamicBufferManager _sendBuffer;
|
||||
/// <summary>
|
||||
/// 长度是否使用网络字节顺序
|
||||
/// </summary>
|
||||
/// <value><c>true</c> if [net byte order]; otherwise, <c>false</c>.</value>
|
||||
public bool NetByteOrder
|
||||
{
|
||||
get { return _netByteOrder; }
|
||||
set { _netByteOrder = value; }
|
||||
}
|
||||
|
||||
public SyncSocketInvokeElement()
|
||||
{
|
||||
_tcpClient = new TcpClient();
|
||||
_tcpClient.Client.Blocking = true; //使用阻塞模式,即同步模式
|
||||
_protocolFlag = ProtocolFlag.None;
|
||||
SocketTimeOutMS = ProtocolConst.SocketTimeOutMS;
|
||||
_outgoingDataAssembler = new OutgoingDataAssembler();
|
||||
_recvBuffer = new DynamicBufferManager(ProtocolConst.ReceiveBufferSize);
|
||||
_incomingDataParser = new IncomingDataParser();
|
||||
_sendBuffer = new DynamicBufferManager(ProtocolConst.ReceiveBufferSize);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>说明:连接Socket服务器</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="host">The host.</param>
|
||||
/// <param name="port">The port.</param>
|
||||
public void Connect(string host, int port)
|
||||
{
|
||||
_tcpClient.Connect(host, port);
|
||||
byte[] socketFlag = new byte[1];
|
||||
socketFlag[0] = (byte)_protocolFlag;
|
||||
_tcpClient.Client.Send(socketFlag, SocketFlags.None); //发送标识
|
||||
_host = host;
|
||||
_port = port;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:关闭Socket连接</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
public void Disconnect()
|
||||
{
|
||||
_tcpClient.Close();
|
||||
_tcpClient = new TcpClient();
|
||||
//_tcpClient.Client.
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:发送命令</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
public void SendCommand()
|
||||
{
|
||||
string commandText = _outgoingDataAssembler.GetProtocolText();
|
||||
byte[] bufferUTF8 = Encoding.UTF8.GetBytes(commandText);
|
||||
int totalLength = sizeof(int) + bufferUTF8.Length; //获取总大小
|
||||
_sendBuffer.Clear();
|
||||
_sendBuffer.WriteInt(totalLength, false); //写入总大小
|
||||
_sendBuffer.WriteInt(bufferUTF8.Length, false); //写入命令大小
|
||||
_sendBuffer.WriteBuffer(bufferUTF8); //写入命令内容
|
||||
_tcpClient.Client.Send(_sendBuffer.Buffer, 0, _sendBuffer.DataCount, SocketFlags.None); //使用阻塞模式,Socket会一次发送完所有数据后才返回
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:发送命令</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="buffer">The buffer.</param>
|
||||
/// <param name="offset">The offset.</param>
|
||||
/// <param name="count">The count.</param>
|
||||
public void SendCommand(byte[] buffer, int offset, int count)
|
||||
{
|
||||
string commandText = _outgoingDataAssembler.GetProtocolText();
|
||||
byte[] bufferUTF8 = Encoding.UTF8.GetBytes(commandText);
|
||||
int totalLength = sizeof(int) + bufferUTF8.Length + count; //获取总大小
|
||||
_sendBuffer.Clear();
|
||||
_sendBuffer.WriteInt(totalLength, false); //写入总大小
|
||||
_sendBuffer.WriteInt(bufferUTF8.Length, false); //写入命令大小
|
||||
_sendBuffer.WriteBuffer(bufferUTF8); //写入命令内容
|
||||
_sendBuffer.WriteBuffer(buffer, offset, count); //写入二进制数据
|
||||
_tcpClient.Client.Send(_sendBuffer.Buffer, 0, _sendBuffer.DataCount, SocketFlags.None);
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:接收命令</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
public bool RecvCommand()
|
||||
{
|
||||
_recvBuffer.Clear();
|
||||
_tcpClient.Client.Receive(_recvBuffer.Buffer, sizeof(int), SocketFlags.None);
|
||||
int packetLength = BitConverter.ToInt32(_recvBuffer.Buffer, 0); //获取包长度
|
||||
if (NetByteOrder)
|
||||
packetLength = System.Net.IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序
|
||||
_recvBuffer.SetBufferSize(sizeof(int) + packetLength); //保证接收有足够的空间
|
||||
_tcpClient.Client.Receive(_recvBuffer.Buffer, sizeof(int), packetLength, SocketFlags.None);
|
||||
int commandLen = BitConverter.ToInt32(_recvBuffer.Buffer, sizeof(int)); //取出命令长度
|
||||
string tmpStr = Encoding.UTF8.GetString(_recvBuffer.Buffer, sizeof(int) + sizeof(int), commandLen);
|
||||
if (!_incomingDataParser.DecodeProtocolText(tmpStr)) //解析命令
|
||||
return false;
|
||||
else
|
||||
return true;
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:接收命令</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="buffer">The buffer.</param>
|
||||
/// <param name="offset">The offset.</param>
|
||||
/// <param name="size">The size.</param>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
public bool RecvCommand(out byte[] buffer, out int offset, out int size)
|
||||
{
|
||||
_recvBuffer.Clear();
|
||||
_tcpClient.Client.Receive(_recvBuffer.Buffer, sizeof(int), SocketFlags.None);
|
||||
int packetLength = BitConverter.ToInt32(_recvBuffer.Buffer, 0); //获取包长度
|
||||
if (NetByteOrder)
|
||||
packetLength = System.Net.IPAddress.NetworkToHostOrder(packetLength); //把网络字节顺序转为本地字节顺序
|
||||
_recvBuffer.SetBufferSize(sizeof(int) + packetLength); //保证接收有足够的空间
|
||||
_tcpClient.Client.Receive(_recvBuffer.Buffer, sizeof(int), packetLength, SocketFlags.None);
|
||||
int commandLen = BitConverter.ToInt32(_recvBuffer.Buffer, sizeof(int)); //取出命令长度
|
||||
string tmpStr = Encoding.UTF8.GetString(_recvBuffer.Buffer, sizeof(int) + sizeof(int), commandLen);
|
||||
if (!_incomingDataParser.DecodeProtocolText(tmpStr)) //解析命令
|
||||
{
|
||||
buffer = null;
|
||||
offset = 0;
|
||||
size = 0;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer = _recvBuffer.Buffer;
|
||||
offset = commandLen + sizeof(int) + sizeof(int);
|
||||
size = packetLength - offset;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/******************************
|
||||
* 说明:下载Scoket通讯
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-01-23
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Lskj.SocketService;
|
||||
|
||||
namespace Lskj.SocketClient
|
||||
{
|
||||
public class ClientDownLoadSocket : ClientBaseSocket
|
||||
{
|
||||
public ClientDownLoadSocket()
|
||||
: base()
|
||||
{
|
||||
_protocolFlag = ProtocolFlag.Download;
|
||||
}
|
||||
|
||||
public bool DoDownLoad(string dirName, string fileName, ref long fileSize)
|
||||
{
|
||||
bool bConnect = ReConnectAndLogin(); //检测连接是否还在,如果断开则重连并登录
|
||||
if (!bConnect)
|
||||
return bConnect;
|
||||
try
|
||||
{
|
||||
_outgoingDataAssembler.Clear();
|
||||
_outgoingDataAssembler.AddRequest();
|
||||
_outgoingDataAssembler.AddCommand(ProtocolKey.Download);
|
||||
_outgoingDataAssembler.AddValue(ProtocolKey.DirName, dirName);
|
||||
_outgoingDataAssembler.AddValue(ProtocolKey.FileName, fileName);
|
||||
SendCommand();
|
||||
bool bSuccess = RecvCommand();
|
||||
if (bSuccess)
|
||||
{
|
||||
bSuccess = CheckErrorCode();
|
||||
if (bSuccess)
|
||||
{
|
||||
bSuccess = _incomingDataParser.GetValue(ProtocolKey.FileSize, ref fileSize);
|
||||
}
|
||||
return bSuccess;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
catch (Exception E)
|
||||
{
|
||||
//记录日志
|
||||
_errorString = E.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DoData(byte[] buffer, int offset, int count)
|
||||
{
|
||||
try
|
||||
{
|
||||
_outgoingDataAssembler.Clear();
|
||||
_outgoingDataAssembler.AddRequest();
|
||||
_outgoingDataAssembler.AddCommand(ProtocolKey.Data);
|
||||
SendCommand(buffer, offset, count);
|
||||
return true;
|
||||
}
|
||||
catch (Exception E)
|
||||
{
|
||||
//记录日志
|
||||
_errorString = E.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DoEof(Int64 fileSize)
|
||||
{
|
||||
try
|
||||
{
|
||||
_outgoingDataAssembler.Clear();
|
||||
_outgoingDataAssembler.AddRequest();
|
||||
_outgoingDataAssembler.AddCommand(ProtocolKey.Eof);
|
||||
SendCommand();
|
||||
bool bSuccess = RecvCommand();
|
||||
if (bSuccess)
|
||||
return CheckErrorCode();
|
||||
else
|
||||
return false;
|
||||
}
|
||||
catch (Exception E)
|
||||
{
|
||||
//记录日志
|
||||
_errorString = E.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/******************************
|
||||
* 说明:上传Socket通讯
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-01-23
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Lskj.SocketService;
|
||||
|
||||
namespace Lskj.SocketClient
|
||||
{
|
||||
/// <summary>
|
||||
/// 上传Socket通讯协议
|
||||
/// </summary>
|
||||
class ClientUploadSocket : ClientBaseSocket
|
||||
{
|
||||
public ClientUploadSocket()
|
||||
: base()
|
||||
{
|
||||
_protocolFlag = ProtocolFlag.Upload;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>说明:上传</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="dirName">目录</param>
|
||||
/// <param name="fileName">文件</param>
|
||||
/// <param name="fileSize">文件大小</param>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
public bool DoUpload(string dirName, string fileName, ref long fileSize)
|
||||
{
|
||||
bool bConnect = ReConnectAndLogin(); //检测连接是否还在,如果断开则重连并登录
|
||||
if (!bConnect)
|
||||
return bConnect;
|
||||
try
|
||||
{
|
||||
_outgoingDataAssembler.Clear();
|
||||
_outgoingDataAssembler.AddRequest();
|
||||
_outgoingDataAssembler.AddCommand(ProtocolKey.Upload);
|
||||
_outgoingDataAssembler.AddValue(ProtocolKey.DirName, dirName);
|
||||
_outgoingDataAssembler.AddValue(ProtocolKey.FileName, fileName);
|
||||
_outgoingDataAssembler.AddValue(ProtocolKey.FileSize, fileSize);
|
||||
SendCommand();
|
||||
bool bSuccess = RecvCommand();
|
||||
if (bSuccess)
|
||||
{
|
||||
bSuccess = CheckErrorCode();
|
||||
if (bSuccess)
|
||||
{
|
||||
bSuccess = _incomingDataParser.GetValue(ProtocolKey.FileSize, ref fileSize);
|
||||
}
|
||||
return bSuccess;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
catch (Exception E)
|
||||
{
|
||||
//记录日志
|
||||
_errorString = E.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DoData(byte[] buffer, int offset, int count)
|
||||
{
|
||||
try
|
||||
{
|
||||
_outgoingDataAssembler.Clear();
|
||||
_outgoingDataAssembler.AddRequest();
|
||||
_outgoingDataAssembler.AddCommand(ProtocolKey.Data);
|
||||
SendCommand(buffer, offset, count);
|
||||
return true;
|
||||
}
|
||||
catch (Exception E)
|
||||
{
|
||||
//记录日志
|
||||
_errorString = E.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DoEof(Int64 fileSize)
|
||||
{
|
||||
try
|
||||
{
|
||||
_outgoingDataAssembler.Clear();
|
||||
_outgoingDataAssembler.AddRequest();
|
||||
_outgoingDataAssembler.AddCommand(ProtocolKey.Eof);
|
||||
SendCommand();
|
||||
bool bSuccess = RecvCommand();
|
||||
if (bSuccess)
|
||||
return CheckErrorCode();
|
||||
else
|
||||
return false;
|
||||
}
|
||||
catch (Exception E)
|
||||
{
|
||||
//记录日志
|
||||
_errorString = E.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
/******************************
|
||||
* 说明:Socket传输基类
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-01-23
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Lskj.SocketService;
|
||||
|
||||
namespace Lskj.SocketClient
|
||||
{
|
||||
/// <summary>
|
||||
/// Socket传输基类
|
||||
/// </summary>
|
||||
public class ClientBaseSocket : SyncSocketInvokeElement
|
||||
{
|
||||
protected string _errorString;
|
||||
/// <summary>
|
||||
/// 用户名
|
||||
/// </summary>
|
||||
protected string _userName;
|
||||
/// <summary>
|
||||
/// 密码
|
||||
/// </summary>
|
||||
protected string _password;
|
||||
/// <summary>
|
||||
/// 错误码
|
||||
/// </summary>
|
||||
/// <value>The error string.</value>
|
||||
public string ErrorString { get { return _errorString; } }
|
||||
|
||||
public ClientBaseSocket()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <para>说明:检查是否传输结果</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
public bool CheckErrorCode()
|
||||
{
|
||||
int errorCode = 0;
|
||||
_incomingDataParser.GetValue(ProtocolKey.Code, ref errorCode);
|
||||
if (errorCode == ProtocolCode.Success)
|
||||
return true;
|
||||
else
|
||||
{
|
||||
_errorString = ProtocolCode.GetErrorCodeString(errorCode);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:发送Active命令</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
public bool DoActive()
|
||||
{
|
||||
try
|
||||
{
|
||||
_outgoingDataAssembler.Clear();
|
||||
_outgoingDataAssembler.AddRequest();
|
||||
_outgoingDataAssembler.AddCommand(ProtocolKey.Active);
|
||||
SendCommand();
|
||||
bool bSuccess = RecvCommand();
|
||||
if (bSuccess)
|
||||
return CheckErrorCode();
|
||||
else
|
||||
return false;
|
||||
}
|
||||
catch (Exception E)
|
||||
{
|
||||
//记录日志
|
||||
_errorString = E.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:发送登录命令</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <param name="userName">Name of the user.</param>
|
||||
/// <param name="password">The password.</param>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
public bool DoLogin(string userName, string password)
|
||||
{
|
||||
try
|
||||
{
|
||||
_outgoingDataAssembler.Clear();
|
||||
_outgoingDataAssembler.AddRequest();
|
||||
_outgoingDataAssembler.AddCommand(ProtocolKey.Login);
|
||||
_outgoingDataAssembler.AddValue(ProtocolKey.UserName, userName);
|
||||
_outgoingDataAssembler.AddValue(ProtocolKey.Password, BasicFunc.MD5String(password));
|
||||
SendCommand();
|
||||
bool bSuccess = RecvCommand();
|
||||
if (bSuccess)
|
||||
{
|
||||
bSuccess = CheckErrorCode();
|
||||
if (bSuccess)
|
||||
{
|
||||
_userName = userName;
|
||||
_password = password;
|
||||
}
|
||||
return bSuccess;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
catch (Exception E)
|
||||
{
|
||||
//记录日志
|
||||
_errorString = E.Message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:检查连接是否存在,不存在则重连服务器</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
public bool ReConnect()
|
||||
{
|
||||
if (_tcpClient.Connected && (!DoActive()))
|
||||
return true;
|
||||
else
|
||||
{
|
||||
if (!_tcpClient.Connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
Connect(_host, _port);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
return true;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:检查连接是否存在,不存在则重连并登录</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-01-24 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
|
||||
public bool ReConnectAndLogin()
|
||||
{
|
||||
if (_tcpClient.Connected && (!DoActive()))
|
||||
return true;
|
||||
else
|
||||
{
|
||||
if (!_tcpClient.Connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
Disconnect();
|
||||
Connect(_host, _port);
|
||||
return DoLogin(_userName, _password);
|
||||
}
|
||||
catch (Exception E)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user