Skip to content

Commit

Permalink
添加项目文件。
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrysanHua committed Mar 8, 2019
1 parent 11da965 commit 6f0f7ec
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 0 deletions.
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
</configuration>
169 changes: 169 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace SocketSingleSend
{
class Program
{
private const string ANSWER_FLAG = ">>OK<<";
private const int MAX_BYTE_SIZE = 256;
static readonly IPAddress localIP = Dns.GetHostByName(Dns.GetHostName()).AddressList[0];
static readonly int PORT = 10019;
static readonly int AS_PORT = 11019;

static Socket udpSender;
static Socket udpReceiver;

static void Main(string[] args)
{
Console.WriteLine("1: Send ;\r\n2: Receive ;");
Console.WriteLine("Make a choice to do:");

string choice = Console.ReadLine();
if (choice == "1")
{
//do Send
IPAddress targetIP = null;
bool flag = false;
while (!flag)
{
Console.WriteLine("Enter the other side's IP:");
try
{
targetIP = IPAddress.Parse(Console.ReadLine());
flag = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Console.WriteLine("Write down your text:");
Console.WriteLine("-----------------------------------");
string str = null;
do
{
str = Console.ReadLine();
if (str.Length > (MAX_BYTE_SIZE / 2 - 2))
{
Console.WriteLine("<<<<<<<<<<<Too Long!");
continue;
}
if (str.Length == 0)
{
Console.WriteLine("<<<<<<<<<<<Can not be empty!");
continue;
}
if (!SendText(str, targetIP))
Console.WriteLine("<<<<<<<<<<<The other side may not have received!");
} while (str != "exit");
}
else if (choice == "2")
{
//do Receive
Console.WriteLine("Local IP is: " + localIP);
Console.WriteLine("Waiting to receive the Msg...");
Console.WriteLine("-----------------------------------");
string msg = null;
do
{
msg = ReceiveText(localIP);
if (msg != null)
Console.WriteLine(msg);
} while (msg != "exit");
}
udpReceiver?.Close();
udpSender?.Close();
}

static string ReceiveText(IPAddress listenIP)
{
IPEndPoint listenIPE = new IPEndPoint(listenIP, PORT);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint remoteIPE = sender;
//UDP Mode
string receiveStr = UDPReceive(listenIPE, ref remoteIPE);
if (receiveStr != null)
{
//Send answer
IPEndPoint answerIPE = (IPEndPoint)remoteIPE;
answerIPE.Port = AS_PORT;
int count = 3;
while (count > 0)
{
count--;
if (UDPSend(ANSWER_FLAG, answerIPE))
break;
}
}
return receiveStr;
}

static bool SendText(string str, IPAddress targetIP)
{
IPEndPoint targetIPE = new IPEndPoint(targetIP, PORT);
IPEndPoint answerIPE = new IPEndPoint(localIP, AS_PORT);
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint remoteIPE = sender;
//UDP Mode
if (UDPSend(str, targetIPE))
{
//Receive answer
if (UDPReceive(answerIPE, ref remoteIPE, false) == ANSWER_FLAG)
return true;
}
return false;
}

static bool UDPSend(string str, EndPoint targetIPE)
{
if (udpSender == null)
{
udpSender = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
udpSender.SendTimeout = 800;
}

try
{
byte[] strByte = Encoding.UTF8.GetBytes(str);
int sendLen = udpSender.SendTo(strByte, SocketFlags.None, targetIPE);
if (sendLen == strByte.Length)
return true;
}
catch { }
return false;
}

static string UDPReceive(EndPoint listenIPE, ref EndPoint remoteIPE, bool blocked = true)
{
if (udpReceiver == null)
{
//Receive_Socket的首次创建导致错过Answer信号的接收-------提升Socket创建顺序
//answerReceive网络缓冲区的旧数据导致收到“假Answer”------添加Answer校对
udpReceiver = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
udpReceiver.Bind(listenIPE);
if (!blocked)
udpReceiver.ReceiveTimeout = 2500;
}

try
{
byte[] strByte = new byte[MAX_BYTE_SIZE];
int receiveLen = udpReceiver.ReceiveFrom(strByte,
SocketFlags.None, ref remoteIPE);
if (receiveLen > 0)
return Encoding.UTF8.GetString(strByte, 0, receiveLen);
}
catch { }
return null;
}

}
}
36 changes: 36 additions & 0 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("SocketSingleSend")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SocketSingleSend")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]

// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("10f1df60-2525-41d6-8d30-ce0c22c0332c")]

// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
53 changes: 53 additions & 0 deletions SocketSingleSend.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" 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>{10F1DF60-2525-41D6-8D30-CE0C22C0332C}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>SocketSingleSend</RootNamespace>
<AssemblyName>SocketSingleSend</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</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|AnyCPU' ">
<PlatformTarget>AnyCPU</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.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
25 changes: 25 additions & 0 deletions SocketSingleSend.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.136
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketSingleSend", "SocketSingleSend.csproj", "{10F1DF60-2525-41D6-8D30-CE0C22C0332C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{10F1DF60-2525-41D6-8D30-CE0C22C0332C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{10F1DF60-2525-41D6-8D30-CE0C22C0332C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{10F1DF60-2525-41D6-8D30-CE0C22C0332C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{10F1DF60-2525-41D6-8D30-CE0C22C0332C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AEDFDC66-2A93-4E14-B270-5554BCE31708}
EndGlobalSection
EndGlobal

0 comments on commit 6f0f7ec

Please sign in to comment.