public static void Main(String[] args) { bool reportError = true; if (args.Length == 1) { if ("send".CompareTo(args[0]) == 0) { UDP_protocol uf = new UDP_protocol(); uf.OpenSenderSocket(IPAddress.Parse("127.0.0.1"), 667); for (int i = 0; i < 100; i++) { uf.SendPlayerInfoData((byte)(i % 20), (byte)i, (byte)(i + 1)); Thread.Sleep(10); } Thread.Sleep(9000); reportError = false; } else if ("receive".CompareTo(args[0]) == 0) { UDP_protocol uf = new UDP_protocol(); uf.OpenReceiverSocket(667); Thread.Sleep(10000); reportError = false; } } if (reportError) { Console.WriteLine("Use: UDP_protocol send/receive"); } }
static void OnReceiveAck(IAsyncResult result) { UDP_protocol uf = (UDP_protocol)result.AsyncState; EndPoint remoteEndPoint = new IPEndPoint(0, 0); int bytesRead = 0; // full implementation will try..catch EndReceiveFrom bytesRead = uf.senderSocket.EndReceiveFrom(result, ref remoteEndPoint); uf.ProcessIncomingAck(uf.bufferAck, bytesRead); uf.ListenForAcks(); }
static void OnReceiveData(IAsyncResult result) { UDP_protocol uf = (UDP_protocol)result.AsyncState; EndPoint remoteEndPoint = new IPEndPoint(0, 0); int bytesRead = 0; // full implementation will try..catch EndReceiveFrom // update sender Ack address; this sample assumes only one possible sender bytesRead = uf.receiverSocket.EndReceiveFrom(result, ref uf.receiverAckEndPoint); uf.ProcessIncomingPlayerInfo(uf.bufferData, bytesRead); uf.ListenForData(); }