diff --git a/Program.cs b/Program.cs index fa51374..9426b2c 100644 --- a/Program.cs +++ b/Program.cs @@ -22,17 +22,19 @@ class Program static Socket udpReceiver; static void Main(string[] args) - { + { 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):"); + //实现IP检测,即通信开始前发送IP string choice = Console.ReadLine(); if (choice == "1") { //do Send + InitSocket("UDP", true); IPAddress targetIP = null; bool flag = false; while (!flag) @@ -54,7 +56,7 @@ static void Main(string[] args) do { str = Console.ReadLine(); - if (str.Length > (MAX_BYTE_SIZE / 2 - 2)) + if (str.Length > (MAX_BYTE_SIZE / 2 - 8)) { Console.WriteLine("<<<<<<<<<< 0) { count--; - if (UDPSend(ANSWER_FLAG, answerIPE)) + if (UDPSend(ANSWER_FLAG + receiveStr, answerIPE)) { #if DEBUG Console.WriteLine("<<<<<<<>>>>>>>>get '{0}' from: {1}", answerStr, remoteIPE); + Console.WriteLine(">>>>>>>>>get '{0}' from: {1}", answerStr, remoteIPE); #endif - if (answerStr == ANSWER_FLAG) - return true; + if (answerStr != null && + answerStr.StartsWith(ANSWER_FLAG) && answerStr.EndsWith(str)) + return true; + } while (answerStr != null); } 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); @@ -179,19 +181,8 @@ static bool UDPSend(string str, EndPoint targetIPE) return false; } - static string UDPReceive(EndPoint listenIPE, ref EndPoint remoteIPE, bool blocked = true) + static string UDPReceive(ref EndPoint remoteIPE) { - 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]; @@ -209,5 +200,44 @@ static string UDPReceive(EndPoint listenIPE, ref EndPoint remoteIPE, bool blocke return null; } + static void InitSocket(string protocolMode, bool sendMode) + { + //Init Socket + if (protocolMode == "UDP") + { + udpReceiver = new Socket(AddressFamily.InterNetwork, + SocketType.Dgram, ProtocolType.Udp); + udpSender = new Socket(AddressFamily.InterNetwork, + SocketType.Dgram, ProtocolType.Udp) + { + SendTimeout = 800 + }; + if (sendMode) + { + udpReceiver.Bind(new IPEndPoint(localIP, AS_PORT)); + udpReceiver.ReceiveTimeout = 2500; + } + else + udpReceiver.Bind(new IPEndPoint(localIP, PORT)); + } + } + + static void FlushReceiveBuf() + { + //this method will only be invoked before receiving a answer + IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); + EndPoint remoteIPE = sender; + //clean up the Receive Buffer + while (udpReceiver.Available != 0) + { +#if DEBUG + Console.WriteLine("---------get '{0}' from: {1}", + UDPReceive(ref remoteIPE), remoteIPE); +#else + UDPReceive(ref remoteIPE); +#endif + } + } + } }