static void Main(string[] args) { ReedSolomon _rs = new ReedSolomon(); //initialize parity bytes int encodelength = 1472; //Byteencodelength int dataleng = _rs.GetDataLeng(encodelength); int[] dataInt = new int[dataleng / 4]; //initialize for (int i = 0; i < dataInt.Length; i++) { dataInt[i] = i; } byte[] data = new byte[dataInt.Length * sizeof(int)]; Buffer.BlockCopy(dataInt, 0, data, 0, data.Length); byte[] encode = new byte[encodelength]; //_rs.encode(data, encode); ////add error //Random _random = new Random(); //for(int i = 0; i < 29; i++) { // encode[_random.Next(encode.Length - 1)] ^= (byte)_random.Next(); //} ////decode //byte[] decodedata = new byte[data.Length]; //int errorDecode = _rs.decode(encode, decodedata); ////check correct decode //for(int i = 0; i < dataleng; i++) { // if(data[i] != decodedata[i]) { // Console.WriteLine("error"); // break; // } //} clientList = new List <client_IPEndPoint>() { new client_IPEndPoint() { ID_client = "20154023", On = true, NumSend = 1 }, new client_IPEndPoint() { ID_client = "00000001", On = true } }; UDPsocket udpsocket = new UDPsocket(clientList, encode, data); udpsocket.UDPsocketSend(); //Console.WriteLine("Hello World!"); }
void receive() { EndPoint dummyEndpoint = new IPEndPoint(IPAddress.Any, 0); ReedSolomon rs = new ReedSolomon(); //check error bits int encodelength = 1472; //Byteencodelength byte[] _encode = new byte[encodelength]; { int dataleng = rs.GetDataLeng(encodelength); int[] dataInt = new int[dataleng / 4]; //initialize for (int i = 0; i < dataInt.Length; i++) { dataInt[i] = i; } byte[] datarand = new byte[dataInt.Length * sizeof(int)]; Buffer.BlockCopy(dataInt, 0, datarand, 0, datarand.Length); rs.encode(datarand, _encode); } int Frame = 0, error = 0, lost = 0; int maxError = 0, minError = 100000; while (true) { try { int length = socket.ReceiveFrom(receiveBuffer, ref dummyEndpoint); Frame++; int res = checkBitDiff(_encode, receiveBuffer, length); maxError = (res > maxError) ? res : maxError; minError = (res < minError) ? res : minError; int decodedataleng = rs.GetDataLeng(length); byte[] decodedata = new byte[decodedataleng]; int sign = rs.decode(receiveBuffer, length, decodedata); if (sign == 0) //can't correct { error++; } else { int[] data = new int[decodedata.Length / 4]; Buffer.BlockCopy(decodedata, 0, data, 0, decodedata.Length); //check equal for (int i = 1; i < data.Length; i++) { if (data[i] != i) { error++; break; } } //check order //int order = BitConverter.ToInt32(decodedata, 0); //if(order > Frame) { // lost += order - Frame - 1; // Frame = order; //} } } catch (Exception ex) { //Console.WriteLine(ex); } if ((Frame % 100 == 0) && (Frame > 0)) { Console.WriteLine("Frame {0} . Error: {1} . Error bit: Max {2}, Min: {3}", Frame, error, maxError, minError); } } }