Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.45 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.Windows.Forms;
  9. using System.Net.Sockets;
  10. using System.Net;
  11. using System.IO;
  12. using System.Threading;
  13.  
  14. namespace Tasks
  15. {
  16. public partial class Local_Chatroom_Server : Form
  17. {
  18. public Local_Chatroom_Server()
  19. {
  20. InitializeComponent();
  21. }
  22.  
  23. List<string> user = new List<string>();
  24. List<string> mac = new List<string>();
  25. List<string> chat = new List<string>();
  26. List<string> bestrafung = new List<string>();
  27.  
  28. List<string> log = new List<string>();
  29.  
  30. Thread th1;
  31. Thread th2;
  32.  
  33. int temp = 0;
  34. string adminpassword;
  35.  
  36. string IP;
  37.  
  38. bool macvorhanden = false;
  39. bool nickvorhanden = false;
  40.  
  41. Random random = new Random();
  42.  
  43. TcpClient client;
  44. TcpListener listener;
  45. NetworkStream nwStream;
  46. IPAddress localAdd;
  47.  
  48. const int PORT_NO = 8080;
  49. const string SERVER_IP = "127.0.0.1";
  50.  
  51. private void Form1_Load(object sender, EventArgs e)
  52. {
  53. string HostName = System.Net.Dns.GetHostName();
  54. System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostByName(HostName);
  55. IP = hostInfo.AddressList[0].ToString();
  56. localAdd = IPAddress.Parse(IP);
  57. label1.Text = "SERVER IP: " + IP;
  58. for (int i = 0; i < 5; i++)
  59. {
  60. adminpassword += random.Next(0, 10);
  61. }
  62. label5.Text = "Adminpasswort: " + adminpassword;
  63. }
  64.  
  65. public void start()
  66. {
  67. // Starte Server
  68. listener = new TcpListener(localAdd, 8080);
  69. if (temp == 0)
  70. {
  71. listener.Start();
  72. log.Add("LISTENER STARTED");
  73. temp++;
  74. }
  75. log.Add("START COMPLETED");
  76. chat.Add("Server started at " + DateTime.Now.ToString("T"));
  77. th2.Start();
  78. }
  79.  
  80. public void accept()
  81. {
  82. while (true)
  83. {
  84. // Warte auf Verbindung
  85. client = listener.AcceptTcpClient();
  86. client.ReceiveTimeout = 2000;
  87. log.Add("New REQUEST");
  88. // Akzeptiere Verbindung
  89. nwStream = client.GetStream();
  90. nwStream.ReadTimeout = 2000;
  91. byte[] buffer = new byte[client.ReceiveBufferSize];
  92. Thread.Sleep(50);
  93. if (nwStream.DataAvailable == true)
  94. {
  95. int readrequest = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
  96. string request = Encoding.ASCII.GetString(buffer, 0, readrequest);
  97. log.Add("Request: " + request);
  98. // Neuer Befehl
  99.  
  100. if ("/".Equals(request.Substring(0, 1)) && request.Contains(adminpassword.ToString()))
  101. {
  102. if ("freeze".Equals(request.Substring(1, 6)))
  103. {
  104. for (int i = 0; i < user.Count; i++)
  105. {
  106. if (request.Contains(user[i]))
  107. {
  108. bestrafung.Add("freeze" + " " + mac[i]);
  109. }
  110. }
  111. }
  112. else if ("unfreeze".Equals(request.Substring(1, 8)))
  113. {
  114. for (int i = 0; i < bestrafung.Count; i++)
  115. {
  116. if (request.Contains(bestrafung[i]))
  117. {
  118. bestrafung.RemoveAt(i);
  119. }
  120. }
  121. }
  122. else if ("shutdown".Equals(request.Substring(1, 8)))
  123. {
  124. for (int i = 0; i < user.Count; i++)
  125. {
  126. if (request.Contains(user[i]))
  127. {
  128. bestrafung.Add("shutdown" + " " + mac[i]);
  129. }
  130. }
  131. }
  132. else if ("chatbann".Equals(request.Substring(1, 8)))
  133. {
  134. for (int i = 0; i < user.Count; i++)
  135. {
  136. if (request.Contains(user[i]))
  137. {
  138. bestrafung.Add("chatbann" + " " + mac[i]);
  139. }
  140. }
  141. }
  142. else if ("stopshutdown".Equals(request.Substring(1, 12)))
  143. {
  144. for (int i = 0; i < bestrafung.Count; i++)
  145. {
  146. if (request.Contains(bestrafung[i]))
  147. {
  148. bestrafung.RemoveAt(i);
  149. }
  150. }
  151. }
  152. else if ("removechatbann".Equals(request.Substring(1, 14)))
  153. {
  154. for (int i = 0; i < bestrafung.Count; i++)
  155. {
  156. if (request.Contains(bestrafung[i]))
  157. {
  158. bestrafung.RemoveAt(i);
  159. }
  160. }
  161. }
  162. }
  163.  
  164. // Neuer Chat
  165. else if ("newchat".Equals(request.Substring(0, 7)))
  166. {
  167. string newchat = request.Remove(0, 7);
  168. string chatmessage = newchat.Remove(0, 12);
  169. string macsender = newchat.Substring(0, 12);
  170. string sendername = "";
  171. for (int i = 0; i < mac.Count; i++)
  172. {
  173. if (macsender == mac[i])
  174. {
  175. sendername = user[i];
  176. }
  177. }
  178. string chatadd = (DateTime.Now.ToString("t") + " " + sendername + ": " + chatmessage);
  179. log.Add("Added '" + chatadd + "' to the Chat");
  180. chat.Add(chatadd);
  181. }
  182.  
  183. // Disconnect
  184. else if ("deleteuser".Equals(request.Substring(0, 10)))
  185. {
  186. string deluser = request.Remove(0, 10);
  187. string disconnecteduser = "";
  188. for (int i = 0; i < mac.Count; i++)
  189. {
  190. if (deluser == mac[i])
  191. {
  192. disconnecteduser = user[i];
  193. }
  194. }
  195. log.Add("User '" + disconnecteduser + "' Disconnected!");
  196. chat.Add(disconnecteduser + " Disconnected!");
  197. }
  198.  
  199. else if ("update".Equals(request.Substring(0, 6)))
  200. {
  201. string update = request.Remove(0, 6);
  202. log.Add("Received chat request from: " + update);
  203.  
  204. // Sende das Update
  205. for (int i = 0; i < chat.Count; i++)
  206. {
  207. byte[] sendchat = ASCIIEncoding.ASCII.GetBytes(chat[i]);
  208. Thread.Sleep(50);
  209. nwStream.Write(sendchat, 0, sendchat.Length);
  210. log.Add("Sending : " + chat[i]);
  211. }
  212. Thread.Sleep(50);
  213. byte[] sendfinish1 = ASCIIEncoding.ASCII.GetBytes("finish1");
  214. nwStream.Write(sendfinish1, 0, sendfinish1.Length);
  215.  
  216. Thread.Sleep(50);
  217.  
  218. // Sende bestrafungsliste
  219. for (int i = 0; i < bestrafung.Count; i++)
  220. {
  221. byte[] sendbestrafung = ASCIIEncoding.ASCII.GetBytes(bestrafung[i]);
  222. Thread.Sleep(50);
  223. nwStream.Write(sendbestrafung, 0, sendbestrafung.Length);
  224. log.Add("Sending : " + bestrafung[i]);
  225. }
  226. Thread.Sleep(50);
  227. byte[] sendfinish2 = ASCIIEncoding.ASCII.GetBytes("finish2");
  228. nwStream.Write(sendfinish2, 0, sendfinish2.Length);
  229.  
  230. log.Add("Sending Complete");
  231. }
  232.  
  233. else if ("newuser".Equals(request.Substring(0, 7)))
  234. {
  235. string usermac = request.Remove(0, 7);
  236. string newusername = usermac.Remove(0, 12);
  237. string newusermac = usermac.Substring(0, 12);
  238.  
  239. //Überprüfe ob user schon Regestriert ist
  240. for (int i = 0; i < mac.Count; i++)
  241. {
  242. if (newusermac == mac[i])
  243. {
  244. macvorhanden = true;
  245. }
  246. }
  247. // Überprüfen ob es den gewollten Namen schon gibt
  248. if (macvorhanden == false)
  249. {
  250. for (int i = 0; i < user.Count; i++)
  251. {
  252. if (newusername == user[i])
  253. {
  254. nickvorhanden = true;
  255. }
  256. }
  257. }
  258.  
  259. if (macvorhanden == false)
  260. {
  261. mac.Add(newusermac);
  262. user.Add(newusername);
  263. log.Add("User '" + newusername + "' joined the Server");
  264. chat.Add(newusername + " joined the Server");
  265. }
  266.  
  267. if (nickvorhanden == true)
  268. {
  269. bool gueltig = false;
  270. int immernochvorhanden = 0;
  271. int count = 1;
  272. while (gueltig == false)
  273. {
  274. newusername = newusername + count;
  275. for (int i = 0; i < user.Count; i++)
  276. {
  277. if (newusername == user[i])
  278. {
  279. immernochvorhanden++;
  280. }
  281. }
  282. if (immernochvorhanden == 0)
  283. {
  284. gueltig = true;
  285. }
  286. else
  287. {
  288. immernochvorhanden = 0;
  289. }
  290. }
  291. }
  292. nickvorhanden = false;
  293. macvorhanden = false;
  294. }
  295. }
  296. else
  297. {
  298. log.Add("CONNECTION TIMED OUT");
  299. }
  300. }
  301. }
  302.  
  303. private void button1_Click(object sender, EventArgs e)
  304. {
  305. button1.Enabled = false;
  306. button1.Visible = false;
  307. label5.Visible = true;
  308.  
  309. th1 = new Thread(start);
  310. th2 = new Thread(accept);
  311. th1.Start();
  312. }
  313.  
  314. private void updater_Tick(object sender, EventArgs e)
  315. {
  316. listBox1.DataSource = null;
  317. listBox2.DataSource = null;
  318. listBox3.DataSource = null;
  319.  
  320. listBox1.DataSource = user;
  321. listBox2.DataSource = chat;
  322. listBox3.DataSource = log;
  323.  
  324. listBox1.TopIndex = listBox1.Items.Count - 1;
  325. listBox2.TopIndex = listBox2.Items.Count - 1;
  326. listBox3.TopIndex = listBox3.Items.Count - 1;
  327. }
  328.  
  329. private void Local_Chatroom_Server_SizeChanged(object sender, EventArgs e)
  330. {
  331. if (this.WindowState == FormWindowState.Maximized)
  332. {
  333. this.WindowState = FormWindowState.Normal;
  334. }
  335. else if (this.WindowState == FormWindowState.Minimized)
  336. {
  337. this.ShowInTaskbar = false;
  338. }
  339. }
  340.  
  341. private void öffnenToolStripMenuItem_Click(object sender, EventArgs e)
  342. {
  343. this.WindowState = FormWindowState.Normal;
  344. this.Show();
  345. this.BringToFront();
  346. this.ShowInTaskbar = true;
  347. }
  348.  
  349. private void stoppenToolStripMenuItem_Click(object sender, EventArgs e)
  350. {
  351. this.Close();
  352. }
  353.  
  354. private void Local_Chatroom_Server_FormClosing(object sender, FormClosingEventArgs e)
  355. {
  356. label1.Text = "SERVER SHUTDOWN!";
  357. log.Add("SERVER SHUTDOWN!");
  358. Thread.Sleep(1000);
  359. if (button1.Enabled == false)
  360. {
  361. listener.Stop();
  362. th1.Abort();
  363. th2.Abort();
  364. }
  365. label1.Text = "CLOSE SERVER!";
  366. log.Add("CLOSE SERVER!");
  367. }
  368.  
  369. private void LocalChatRoomServer_MouseClick(object sender, MouseEventArgs e)
  370. {
  371. if (e.Button == MouseButtons.Left)
  372. {
  373. this.WindowState = FormWindowState.Normal;
  374. this.Show();
  375. this.BringToFront();
  376. this.ShowInTaskbar = true;
  377. }
  378. }
  379. }
  380. }
  381.  
  382. // SERVER
  383. // Code by Ivo Tofall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement