From 9dbd73cc8c22cda9d2239719e4e550ac372874b8 Mon Sep 17 00:00:00 2001 From: Chrysanthes Date: Wed, 20 Mar 2019 14:18:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A5UDP=E5=B9=BF=E6=92=AD=E7=9A=84?= =?UTF-8?q?=E6=96=B9=E5=BC=8F=E5=AE=9E=E7=8E=B0=E4=BA=86=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Messenger.cs | 99 ++++++++++++++++++++++++++++++++++++++++++++++++---- Program.cs | 54 +++++++++++----------------- 2 files changed, 113 insertions(+), 40 deletions(-) diff --git a/Messenger.cs b/Messenger.cs index 28fc796..9922bf0 100644 --- a/Messenger.cs +++ b/Messenger.cs @@ -4,13 +4,24 @@ using System.Net; using System.Net.Sockets; using System.Text; +using System.Threading; using System.Threading.Tasks; namespace SocketSingleSend { class Messenger { + class StateObject + { + public Socket workSocket; + public byte[] dataByte; + public IPEndPoint ipe; + } + + public const int MAX_BYTE_SIZE = 512; + public const int MIN_BYTE_SIZE = 64; + public const int BROADCAST_INTERVAL = 3000; private Socket socket; @@ -101,10 +112,10 @@ public bool UDPSend(byte[] strByte, EndPoint targetIPE, bool firstSend = true) return true; } } - catch (Exception ex) + catch (SocketException ex) { #if DEBUG - Console.WriteLine(ex.Message); + Console.WriteLine(ex.Message + " errorCode: " + ex.ErrorCode); #endif } return false; @@ -128,6 +139,53 @@ public bool SteadySend(string str, EndPoint targetIPE) return false; } + private void BroadcastCallback(IAsyncResult ar) + { + StateObject stateObj = (StateObject)ar.AsyncState; + try + { + int sendLen = stateObj.workSocket.EndSendTo(ar); +#if DEBUG + Console.WriteLine("<<<<<<< 0) + { + string bcMsg = CryptoUtil.Decrypt(CryptoUtil.ByteToStr( + receiveByte, receiveLen)); +#if DEBUG + Console.WriteLine(">>>>>>>>get broadcast '{0}' from: {1}", + bcMsg, remoteIPE); +#endif + return bcMsg; + } + } + catch (SocketException ex) + { +#if DEBUG + Console.WriteLine(ex.Message + " errorCode: " + ex.ErrorCode); +#endif + } + return null; + } } diff --git a/Program.cs b/Program.cs index 21f171a..4b7fdd1 100644 --- a/Program.cs +++ b/Program.cs @@ -2,8 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Net; -using System.Net.NetworkInformation; -using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -20,10 +18,7 @@ class Program private const int AS_PORT = 11019; private const int BC_PORT = 19019; static readonly IPAddress localIP = Dns.GetHostAddresses(Dns.GetHostName()).Last(); - //实现加密广播自动连接 Ipv4 - //实现不定长数据发送 - //重构 抽出Socket通信类 - //answer使用hash值作对比 + static bool sendFail = true; static Messenger udpSender; static Messenger udpReceiver; @@ -60,36 +55,13 @@ static EndPoint CreateEmptyEP() static void Main(string[] args) { - //while (true) - //{ - // string str = Console.ReadLine(); - // string key = "SimpleSocketChat"; - // //string iv = key; - // //CryptoUtil.Init(key, iv); - // //string enStr = CryptoUtil.Encrypt(str); - // //Console.WriteLine(enStr); - // //string deStr = CryptoUtil.Decrypt(enStr); - // //Console.WriteLine(deStr); - // //string hashStr = CryptoUtil.SHA256Hash(str); - // //Console.WriteLine(hashStr); - // byte[] a = CryptoUtil.StrToByte(str); - // byte[] b = CryptoUtil.StrToByte(key); - // b = Messenger.ConcatByte(a, b); - // a = Messenger.SplitByte(b, a.Length, out b); - // Console.WriteLine(CryptoUtil.ByteToStr(a)); - // Console.WriteLine(CryptoUtil.ByteToStr(b)); - // Console.WriteLine("-----------------------------"); - //} - - - Console.WriteLine("1. Send ;\r\n2. Receive ;"); #if DEBUG Console.WriteLine("3. Loop Send ;"); #endif Console.WriteLine("Make a choice to do(1 or 2):"); - string choice = Console.ReadLine(); + string choice = Console.ReadLine().Trim(); if (choice == "1") { //do Send @@ -97,21 +69,33 @@ static void Main(string[] args) IPAddress targetIP = null; while (targetIP == null) { - Console.WriteLine("Enter the other side's IP:"); - targetIP = StrToIP(Console.ReadLine().Trim()); + Console.WriteLine("Enter the other side's IP or press Enter directly:"); + string ipStr = Console.ReadLine().Trim(); + if (string.IsNullOrEmpty(ipStr)) + { + Console.WriteLine("Waiting to auto connect..."); + EndPoint remoteIPE = CreateEmptyEP(); + ipStr = broadcaster.ReceiveBroadcast(ref remoteIPE); + if (ipStr != null && ipStr.StartsWith(IP_FLAG)) + ipStr = ipStr.Substring(IP_FLAG.Length); + } + targetIP = StrToIP(ipStr); } + Console.WriteLine("Connect to Receiver: {0}", targetIP); Console.WriteLine("Write down your text:"); Console.WriteLine("-----------------------------------"); string str = null; do { str = Console.ReadLine(); - if (str.Length > MAX_STR_LEN) + if (str == null) + break; + else if (str.Length > MAX_STR_LEN) { Console.WriteLine("<<<<<<<<<<