| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 
 | using System;
using System.IO;
using System.Text;
using System.Globalization;
using System.Net.Mail;
using System.Configuration;
using System.Net;
using System.Net.Sockets;
using System.Xml.Serialization;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            HelloWorld hello = new HelloWorld();
            hello.Birth = new DateTime(1972, 8, 27);
            hello.Name = "Immobilis";
            hello.Height = 1.72;
 
            Connect("127.0.0.1", hello);
            Console.ReadLine();
        }
 
        static void Connect(String server, HelloWorld hello)
        {
            try
            {
                // Create a TcpClient.
                // Note, for this client to work you need to have a TcpServer 
                // connected to the same address as specified by the server, port
                // combination.
                Int32 port = 13000;
                TcpClient client = new TcpClient(server, port);
 
                // Translate the passed message into ASCII and store it as a Byte array.
                XmlSerializer serializer = new XmlSerializer(typeof(HelloWorld));
                MemoryStream mems = new System.IO.MemoryStream();
                serializer.Serialize(mems, hello);
 
                string _str = Encoding.UTF8.GetString(mems.ToArray());
 
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(_str);
 
                // Get a client stream for reading and writing.
                //  Stream stream = client.GetStream();
 
                using (NetworkStream stream = client.GetStream())
                {
                    // Send the message to the connected TcpServer. 
                    stream.Write(data, 0, data.Length);
 
                    Console.WriteLine("Sent: {0}", hello.ToString());
 
                    // Receive the TcpServer.response.
 
                    // Buffer to store the response bytes.
                    data = new Byte[256];
 
                    // String to store the response ASCII representation.
                    String responseData = String.Empty;
 
                    // Read the first batch of the TcpServer response bytes.
                    Int32 bytes = stream.Read(data, 0, data.Length);
                    responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                    Console.WriteLine("Received: {0}", responseData);
                }
                // Close everything.
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
 
            Console.WriteLine("\n Press Enter to continue...");
            Console.Read();
        }
    }
} | 
Partager