public static PackageData FromBytes(byte[] buffer) { PackageData retVal = new PackageData(); using (MemoryStream ms = new MemoryStream(buffer)) { BinaryReader br = new BinaryReader(ms); retVal.NUMBER = br.ReadInt32(); retVal.DATA = br.ReadString(); retVal.MD5 = br.ReadString(); } return(retVal); }
static void Main(string[] args) { string data; string text = string.Empty; PackageData package = new PackageData(); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); Console.Title = "IdleRQ Secondary"; socket.Bind(ipep); socket.Listen(10); Console.WriteLine("Waiting for client"); Socket client = socket.Accept(); IPEndPoint newClient = (IPEndPoint)client.RemoteEndPoint; Console.WriteLine("Connected with {0} at port {1}", newClient.Address, newClient.Port); NetworkStream ns = new NetworkStream(client); StreamReader sr = new StreamReader(ns); StreamWriter sw = new StreamWriter(ns); sw.WriteLine("Connected success"); sw.Flush(); int start = 0; while (true) { try { data = sr.ReadLine(); if (data != null) { data = Base64Decode(data); Console.WriteLine(data); data = FormatType(data); JPackage frame = JPackage.Deserialize(data); package = frame.Value.ToObject <PackageData>(); start++; } } catch (IOException) { break; } if (start % 3 == 0) { } else { text += package.DATA; sw.WriteLine("ACK"); sw.Flush(); } } ns.Close(); sr.Close(); sw.Close(); Console.WriteLine(); Console.WriteLine("*********************** Text file ************************"); Console.WriteLine(); Console.WriteLine(text); Console.WriteLine(); Console.WriteLine("Disconnected"); }