diff --git a/App.config b/App.config new file mode 100644 index 0000000..b50c74f --- /dev/null +++ b/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..6d9eb55 --- /dev/null +++ b/Program.cs @@ -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("<<<<<<<<<< 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; + } + + } +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e12067c --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -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")] diff --git a/SocketSingleSend.csproj b/SocketSingleSend.csproj new file mode 100644 index 0000000..7e68b36 --- /dev/null +++ b/SocketSingleSend.csproj @@ -0,0 +1,53 @@ + + + + + Debug + AnyCPU + {10F1DF60-2525-41D6-8D30-CE0C22C0332C} + Exe + SocketSingleSend + SocketSingleSend + v4.6.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/SocketSingleSend.sln b/SocketSingleSend.sln new file mode 100644 index 0000000..9468d3a --- /dev/null +++ b/SocketSingleSend.sln @@ -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