Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Net;
  11. using System.Net.Sockets;
  12. using System.IO;
  13.  
  14. namespace chat_server
  15. {
  16. public partial class Form1 : Form
  17. {
  18. private Random rnd = new Random();
  19. TcpListener lyssnare;
  20. TcpClient[] client = new TcpClient[24];
  21. User[] klient = new User[24];
  22. int port = 12345;
  23. int antalklienter = 0;
  24. int antaladmins = 0;
  25.  
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. }
  30.  
  31. private void btnStart_Click(object sender, EventArgs e)
  32. {
  33. try
  34. {
  35. lyssnare = new TcpListener(IPAddress.Any, port);
  36. lyssnare.Start();
  37. }
  38. catch (Exception error) { MessageBox.Show(error.Message, Text); return; }
  39.  
  40. btnStart.Enabled = false;
  41. StartaMottagning();
  42. }
  43.  
  44. public async void StartaMottagning()
  45. {
  46. try
  47. {
  48. Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
  49. klient[antalklienter] = new StandardUser(antalklienter.ToString(), "123", IPAddress.Any, randomColor);
  50. klient[antalklienter].client = await lyssnare.AcceptTcpClientAsync();
  51. }
  52. catch (Exception error) { MessageBox.Show(error.Message, Text); return; }
  53.  
  54. StartaLäsning(klient[antalklienter].getClient());
  55. tbxLog.AppendText(klient[antalklienter].getClient().Client.RemoteEndPoint.ToString() + " Connected! \n");
  56. antalklienter++;
  57. StartaMottagning();
  58. }
  59.  
  60. public async void StartaLäsning(TcpClient k)
  61. {
  62. byte[] buffert = new byte[1024];
  63.  
  64. int n = 0;
  65.  
  66. try
  67. {
  68. n = await k.GetStream().ReadAsync(buffert, 0, buffert.Length);
  69. }
  70. catch (Exception error) { MessageBox.Show(error.Message, Text); return; }
  71. if (Encoding.Unicode.GetString(buffert, 0, n)[0] == '4')
  72. {
  73. TcpClient tempclient = klient[antalklienter].client;
  74. klient[antalklienter] = new Admin(klient[antalklienter].getAlias(), klient[antalklienter].getPassword(), klient[antalklienter].getIP(), klient[antalklienter].getColor(), true, true, true);
  75. klient[antalklienter].client = tempclient;
  76. }
  77.  
  78. string ifadmin = "";
  79.  
  80. try
  81. {
  82. if (Encoding.Unicode.GetString(buffert, 0, n)[0] == '4')
  83. {
  84. ifadmin = "ADMIN :";
  85. }
  86. }
  87. catch (Exception error) { return; }
  88.  
  89. int aliasPos = Encoding.Unicode.GetString(buffert, 0, n).IndexOf("1337", 0);
  90. string alias = Encoding.Unicode.GetString(buffert, 0, n).Substring(1, aliasPos - 1);
  91. string chatt = File.ReadAllText(@"C:\Users\hamjoh64\Documents\Visual Studio 2015\Projects\Programmering2\chat-server\bin\Debug\log.txt");
  92. string newLine = ifadmin + alias + ": " + Encoding.Unicode.GetString(buffert, 0, n).Substring(alias.Length + 4) + Environment.NewLine;
  93. File.AppendAllText(@"C:\Users\hamjoh64\Documents\Visual Studio 2015\Projects\Programmering2\chat-server\bin\Debug\log.txt", newLine);
  94. chatt = File.ReadAllText(@"C:\Users\hamjoh64\Documents\Visual Studio 2015\Projects\Programmering2\chat-server\bin\Debug\log.txt");
  95. tbxLog.AppendText(ifadmin + alias + ": ");
  96. tbxLog.AppendText( Encoding.Unicode.GetString(buffert, 0, n).Substring(alias.Length + 4) + "\n");
  97. StartaSändning(File.ReadAllText(@"C:\Users\hamjoh64\Documents\Visual Studio 2015\Projects\Programmering2\chat-server\bin\Debug\log.txt"), k, ifadmin);
  98. StartaLäsning(k);
  99. }
  100.  
  101. public async void StartaSändning(string message, TcpClient k, string ifadmin)
  102. {
  103. byte[] utData = Encoding.Unicode.GetBytes(ifadmin + message);
  104.  
  105. try
  106. {
  107. await k.GetStream().WriteAsync(utData, 0, utData.Length);
  108. }
  109. catch (Exception error) { MessageBox.Show(error.Message, Text); return; }
  110.  
  111.  
  112. }
  113.  
  114. private void btnFlush_Click(object sender, EventArgs e)
  115. {
  116. File.WriteAllText(@"C:\Users\hamjoh64\Documents\Visual Studio 2015\Projects\Programmering2\chat-server\bin\Debug\log.txt", "");
  117. }
  118. }
  119.  
  120. abstract class User
  121. {
  122. protected string name;
  123. protected string password;
  124. protected IPAddress ip;
  125. public TcpClient client;
  126. protected Color color;
  127.  
  128. public User(string name, string password, IPAddress ip, Color color)
  129. {
  130. this.name = name;
  131. this.password = password;
  132. this.ip = ip;
  133. this.color = color;
  134. }
  135.  
  136. public IPAddress getIP()
  137. {
  138. return ip;
  139. }
  140.  
  141. public TcpClient getClient()
  142. {
  143. return client;
  144. }
  145.  
  146. public string getAlias()
  147. {
  148. return name;
  149. }
  150.  
  151. public Color getColor()
  152. {
  153. return color;
  154. }
  155.  
  156. public string getPassword()
  157. {
  158. return password;
  159. }
  160.  
  161.  
  162. protected void kick()
  163. {
  164.  
  165. }
  166.  
  167. protected void ban(string reason)
  168. {
  169. kick();
  170.  
  171. }
  172.  
  173. protected void unBan()
  174. {
  175.  
  176. }
  177.  
  178. protected void directMessage(string message)
  179. {
  180.  
  181. }
  182. }
  183.  
  184. class StandardUser : User
  185. {
  186.  
  187. public StandardUser(string name, string password, IPAddress ip, Color color) : base(name, password, ip, color)
  188. {
  189.  
  190. }
  191. }
  192.  
  193. class Admin : User
  194. {
  195. protected bool canKick;
  196. protected bool canBan;
  197. protected bool canRename;
  198. public Admin(string name, string password, IPAddress ip, Color color, bool canKick, bool canBan, bool canRename) : base(name, password, ip, color)
  199. {
  200. this.canKick = canKick;
  201. this.canBan = canBan;
  202. this.canRename = canRename;
  203. }
  204. }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement