Guest User

asdasdafds

a guest
Sep 17th, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System.Net;
  2. using System.Net.Sockets;
  3. using System.Text;
  4.  
  5. const string LISTEN_IP_STRING = "127.0.0.1";
  6. const int LISTEN_PORT = 14092;
  7.  
  8. TcpListener server = new(IPAddress.Parse(LISTEN_IP_STRING), LISTEN_PORT);
  9. List<TcpClient> users = new();
  10. List<EndPoint?> usersEndpoints = new();
  11.  
  12. try
  13. {
  14.     Console.WriteLine("Initializing Building Multiplayer server...");
  15.  
  16.     server.Start();
  17.  
  18.     Console.WriteLine("Starting listening at {0}:{1}...", LISTEN_IP_STRING, LISTEN_PORT);
  19.     Console.WriteLine("Press Ctrl+C to stop server");
  20.    
  21.     while (true)
  22.     {
  23.         if (server.Pending())
  24.         {
  25.             TcpClient user = server.AcceptTcpClient();
  26.             user.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  27.             users.Add(user);
  28.             usersEndpoints.Add(user.Client.LocalEndPoint);
  29.             Console.WriteLine("{0} have been connected!", user.Client.RemoteEndPoint);
  30.         }
  31.        
  32.         foreach (var user in users)
  33.         {
  34.             NetworkStream str = null;
  35.  
  36.             //try
  37.             //{
  38.                 str = user.GetStream();
  39.                
  40.                 byte[] bytes = new byte[256];
  41.                 str.Read(bytes, 0, bytes.Length);
  42.                 string dat = Encoding.UTF8.GetString(bytes);
  43.                 if (dat.StartsWith("set"))
  44.                     Console.WriteLine("{0} changed block", user.Client.LocalEndPoint);
  45.  
  46.                 foreach (var user2 in users)
  47.                 {
  48.                     NetworkStream str2 = user2.GetStream();
  49.                     str2.Write(bytes, 0, bytes.Length);
  50.                     str2.Close();
  51.                 }
  52.  
  53.                 str.Close();
  54.             //}
  55.             //catch (ObjectDisposedException)
  56.             //{
  57.             //    Console.WriteLine("{0} have been disconnected!", usersEndpoints[users.IndexOf(user)]);
  58.             //    usersEndpoints.RemoveAt(users.IndexOf(user));
  59.             //    users.Remove(user);
  60.             //    break;
  61.             //}
  62.             //catch (InvalidOperationException)
  63.             //{
  64.             //    Console.WriteLine("{0} have been disconnected!", usersEndpoints[users.IndexOf(user)]);
  65.             //    usersEndpoints.RemoveAt(users.IndexOf(user));
  66.             //    users.Remove(user);
  67.             //    break;
  68.             //}
  69.             //finally
  70.             //{
  71.             //    if (str != null)
  72.             //        str.Close();
  73.             //}
  74.         }
  75.     }
  76.  
  77.    
  78.  
  79. }
  80. catch (Exception ex)
  81. {
  82.     Console.WriteLine(ex.ToString());
  83. }
  84. finally
  85. {
  86.     Console.WriteLine("Stopping server...");
  87.     server.Stop();
  88. }
Advertisement
Add Comment
Please, Sign In to add comment