Exemple #1
0
        public static void PersonMessageToXML(PersonMessage obj)
        {
            XmlSerializer xmlSer = new XmlSerializer(typeof(PersonMessage));

            using (var fs = new FileStream("person.txt", FileMode.OpenOrCreate, FileAccess.Write))
            {
                xmlSer.Serialize(fs, obj);
            }
        }
Exemple #2
0
        public static byte[] PersonMessageToXMLByteArray(PersonMessage obj)
        {
            XmlSerializer xmlSer = new XmlSerializer(typeof(PersonMessage));

            using (var ms = new MemoryStream())
            {
                xmlSer.Serialize(ms, obj);
                return(ms.ToArray());
            }
        }
Exemple #3
0
        public static PersonMessage XMLByteArrayToPersonMessage(byte[] arrBytes)
        {
            using (var memStream = new MemoryStream())
            {
                XmlSerializer xmlSer = new XmlSerializer(typeof(PersonMessage));

                memStream.Write(arrBytes, 0, arrBytes.Length);
                memStream.Seek(0, SeekOrigin.Begin);
                PersonMessage person = (PersonMessage)xmlSer.Deserialize(memStream);

                return(person);
            }
        }
Exemple #4
0
        static void Main(string[] args)
        {
            try
            {
                IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(address), port);
                Socket     socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(ipPoint);

                Console.WriteLine("Input message: ");
                string message = Console.ReadLine();
                string name    = "Evgen";
                byte[] foto    = File.ReadAllBytes("pic.jpg");

                PersonMessage p1 = new PersonMessage(1, name, message, new List <string> {
                    "newMessage", "cSharp", "XML", "rulez"
                }, foto);
                Console.WriteLine("\nPackage:\n" + p1);

                byte[] persMess = PersonMessageToXMLByteArray(p1);
                PersonMessageToXML(p1);

                Console.WriteLine("\nDeserialized package:\n\n" + (XMLByteArrayToPersonMessage(persMess).ToString()) + "\n");

                socket.Send(persMess);

                //  GET ANSWER
                byte[]        data    = new byte[256];
                StringBuilder builder = new StringBuilder();
                int           bytes   = 0;

                do
                {
                    bytes = socket.Receive(data, data.Length, 0);
                    builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                } while (socket.Available > 0);

                Console.WriteLine("Server says: " + builder.ToString());

                // CLOSE SOCKET
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }