Advertisement
Guest User

Untitled

a guest
Apr 24th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net.Sockets;
  4. using System.Reflection;
  5. using System.Runtime.Serialization;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Xml.Serialization;
  8.  
  9. namespace Client
  10. {
  11.     public class Program
  12.     {
  13.         public static void Main(string[] args)
  14.         {
  15.             while (true)
  16.             {
  17.                 try
  18.                 {
  19.  
  20.                     Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  21.                     Console.WriteLine("Hello! Choose the ip-address to connect to a PC");
  22.                     string? ip = Console.ReadLine();
  23.                    
  24.  
  25.  
  26.  
  27.                     server.Connect(ip, 8080);
  28.  
  29.                     byte[] data = ToByteArray<bool>(true);
  30.  
  31.                     server.Send(data);
  32.                     server.Receive(data);
  33.                     bool initialized = FromByteArray<bool>(data);
  34.                     if (initialized)
  35.                     {
  36.                         // Beide Initialisiert
  37.                         int receivedDataLength = server.Receive(data);
  38.                         string receivedData = System.Text.Encoding.ASCII.GetString(data, 0, receivedDataLength);
  39.                         Console.WriteLine("Received: {0}", receivedData);
  40.  
  41.                         using (NetworkStream stream = new NetworkStream(server, false))
  42.                         {
  43.                             SendToStream<Person>(new Person("Klaus Meinert", 45), stream);
  44.                         }
  45.                     }
  46.                 }
  47.                 catch
  48.                 {
  49.                     throw;
  50.                 }
  51.             }
  52.         }
  53.         [Serializable]
  54.         public class Person
  55.         {
  56.             public string name { get; set; }
  57.             public int age { get; set; }
  58.             public Person(string name, int age)
  59.             {
  60.                 this.name = name;
  61.                 this.age = age;
  62.             }
  63.             public Person() { }
  64.         }
  65.         public static byte[] ToByteArray<T>(T obj)
  66.         {
  67.             if (obj == null)
  68.                 return null;
  69.             XmlSerializer bf = new XmlSerializer(typeof(T));
  70.             using (MemoryStream ms = new MemoryStream())
  71.             {
  72.                 bf.Serialize(ms, obj);
  73.                 return ms.ToArray();
  74.             }
  75.         }
  76.         public static T FromByteArray<T>(byte[] data)
  77.         {
  78.             if (data == null)
  79.                 return default(T);
  80.             XmlSerializer bf = new XmlSerializer(typeof(T));
  81.             using (MemoryStream ms = new MemoryStream(data))
  82.             {
  83.                 object obj = bf.Deserialize(ms);
  84.                 return (T)obj;
  85.             }
  86.         }
  87.         public static T GetStreamFromServer<T>(Stream data)
  88.         {
  89.             if (data == null)
  90.                 return default(T);
  91.             XmlSerializer bf = new XmlSerializer(typeof(T));
  92.             object obj = bf.Deserialize(data);
  93.             return (T)obj;
  94.         }
  95.         public static void SendToStream<T>(T obj, Stream data)
  96.         {
  97.             XmlSerializer bf = new XmlSerializer(typeof(T));
  98.             bf.Serialize(data, obj);
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement