|  public class Server{
 public List<IPAddress> mailing;
 public TcpClient client;
 public TcpListener listener;
 public int Get_port;
 public int Send_port;
 public bool contiuneing;
 public List<string> ToSend;
 
 public Server(int get_port, int send_port)
 {
 Get_port = get_port;
 Send_port = send_port;
 
 client = new TcpClient();
 listener = new TcpListener(Get_port);
 
 contiuneing = true;
 ToSend = new List<string>();
 mailing = new List<IPAddress>();
 }
 
 public void start()
 {
 listener = new TcpListener(new IPEndPoint(IPAddress.Any, Get_port));
 listener.Start();
 
 contiuneing = true;
 
 Thread recivethread = new Thread(getdata);
 recivethread.IsBackground = true;
 recivethread.Start();
 }
 
 public void getdata()
 {
 while (contiuneing)
 {
 try
 {
 using (TcpClient client = listener.AcceptTcpClient())
 {
 NetworkStream stream = client.GetStream();
 using (BinaryWriter w = new BinaryWriter(stream))
 {
 using (BinaryReader r = new BinaryReader(stream))
 {
 string read = r.ReadString();
 ToSend.Add(read);
 Console.WriteLine(read);
 }
 }
 }
 sendata();
 }
 catch (Exception ex)
 {
 System.Diagnostics.Debug.WriteLine(ex.ToString());
 }
 finally
 {
 listener.Stop();
 }
 }
 }
 public void sendata()
 {
 if (ToSend.Count != 0)
 {
 foreach (IPAddress i in mailing)
 {
 try
 {
 client.Connect(i, Send_port);
 
 NetworkStream stream = client.GetStream();
 
 using (BinaryWriter w = new BinaryWriter(stream))
 {
 using (BinaryReader r = new BinaryReader(stream))
 {
 foreach (string s in ToSend)
 {
 w.Write(s);
 }
 }
 }
 }
 catch (Exception err)
 {
 System.Diagnostics.Debug.WriteLine(err.ToString());
 }
 finally
 {
 client.Close();
 }
 }
 }
 }
 }
 
 class Program
 {
 static bool iserver;
 static Server server;
 static IPAddress serverIP;
 static IPAddress yourIP;
 static void Main(string[] args)
 {
 Console.WriteLine("are you the server?[y/n]");
 
 if (Console.ReadLine().ToLower() == "y")
 {
 server = new Server(8010, 8015);
 iserver = true;
 server.contiuneing = true;
 
 server.start();
 Console.WriteLine("you are the server");
 }
 else
 {
 Console.WriteLine("IP of server:?");
 serverIP = IPAddress.Parse(Console.ReadLine());
 Console.WriteLine("your IP:?");
 yourIP = IPAddress.Parse(Console.ReadLine());
 NetworkManager.SendData(serverIP, 8015, "I" + yourIP.ToString());
 }
 Thread getthread = new Thread(getdata);
 getthread.IsBackground = true;
 getthread.Start();
 Console.WriteLine("hum...click...hum...click...click");
 Console.ReadLine();
 if (!iserver)
 {
 TcpClient client = new TcpClient();
 client.Connect(serverIP, 8010);
 
 NetworkStream stream = client.GetStream();
 
 using (BinaryWriter w = new BinaryWriter(stream))
 {
 using (BinaryReader r = new BinaryReader(stream))
 {
 while (true)
 {
 string read = Console.ReadLine();
 if (read != "QUIT")
 {
 w.Write(read);
 }
 }
 }
 }
 }
 
 }
 private static void getdata()
 {
 while (true)
 {
 TcpListener listener;
 if (!iserver)
 {
 listener = new TcpListener(new IPEndPoint(IPAddress.Any, 8015));
 }
 else
 {
 listener = new TcpListener(new IPEndPoint(IPAddress.Any, 8005));
 }
 try
 {
 using (TcpClient client = listener.AcceptTcpClient())
 {
 NetworkStream stream = client.GetStream();
 using (BinaryWriter w = new BinaryWriter(stream))
 {
 using (BinaryReader r = new BinaryReader(stream))
 {
 string read = r.ReadString();
 if (!iserver)
 {
 Console.WriteLine(read);
 }
 else
 {
 Console.WriteLine(read);
 if (read[0] == "I".ToCharArray()[0])
 {
 read.Remove(0, 1);
 server.mailing.Add( IPAddress.Parse(read));
 }
 }
 }
 }
 }
 }
 catch (Exception ex)
 {
 System.Diagnostics.Debug.WriteLine(ex.ToString());
 }
 finally
 {
 listener.Stop();
 }
 }
 }
 }
 public class NetworkManager
 {
 public static void SendData(IPAddress address, int port, string message)
 {
 TcpClient client = new TcpClient();
 
 try
 {
 client.Connect(address, port);
 
 NetworkStream stream = client.GetStream();
 
 using (BinaryWriter w = new BinaryWriter(stream))
 {
 using (BinaryReader r = new BinaryReader(stream))
 {
 w.Write(message);
 }
 }
 }
 catch (Exception err)
 {
 System.Diagnostics.Debug.WriteLine(err.ToString());
 }
 finally
 {
 client.Close();
 }
 }
 
 public static string GetData(int port)
 {
 TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Any,port));
 listener.Start();
 
 try
 {
 using (TcpClient client = listener.AcceptTcpClient())
 {
 NetworkStream stream = client.GetStream();
 using (BinaryWriter w = new BinaryWriter(stream))
 {
 using (BinaryReader r = new BinaryReader(stream))
 {
 return r.ReadString();
 }
 }
 }
 }
 catch (Exception ex)
 {
 System.Diagnostics.Debug.WriteLine(ex.ToString());
 return null;
 }
 finally
 {
 listener.Stop();
 }
 }
 
 /// <summary>
 /// Waits Untill data is found
 /// </summary>
 /// <param name="address"></param>
 /// <param name="port"></param>
 /// <returns></returns>
 public static string GetData(int port,IPAddress address)
 {
 TcpListener listener = new TcpListener(new IPEndPoint(address, port));
 listener.Start();
 
 try
 {
 using (TcpClient client = listener.AcceptTcpClient())
 {
 NetworkStream stream = client.GetStream();
 using (BinaryWriter w = new BinaryWriter(stream))
 {
 using (BinaryReader r = new BinaryReader(stream))
 {
 return r.ReadString();
 }
 }
 }
 }
 catch (Exception ex)
 {
 System.Diagnostics.Debug.WriteLine(ex.ToString());
 return null;
 }
 finally
 {
 listener.Stop();
 }
 }
 }
 
 |