Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net.Sockets;
- using System.Net;
- using System.IO;
- using System.Threading;
- namespace Tasks
- {
- public partial class Local_Chatroom_Server : Form
- {
- public Local_Chatroom_Server()
- {
- InitializeComponent();
- }
- List<string> user = new List<string>();
- List<string> mac = new List<string>();
- List<string> chat = new List<string>();
- List<string> bestrafung = new List<string>();
- List<string> log = new List<string>();
- Thread th1;
- Thread th2;
- int temp = 0;
- string adminpassword;
- string IP;
- bool macvorhanden = false;
- bool nickvorhanden = false;
- Random random = new Random();
- TcpClient client;
- TcpListener listener;
- NetworkStream nwStream;
- IPAddress localAdd;
- const int PORT_NO = 8080;
- const string SERVER_IP = "127.0.0.1";
- private void Form1_Load(object sender, EventArgs e)
- {
- string HostName = System.Net.Dns.GetHostName();
- System.Net.IPHostEntry hostInfo = System.Net.Dns.GetHostByName(HostName);
- IP = hostInfo.AddressList[0].ToString();
- localAdd = IPAddress.Parse(IP);
- label1.Text = "SERVER IP: " + IP;
- for (int i = 0; i < 5; i++)
- {
- adminpassword += random.Next(0, 10);
- }
- label5.Text = "Adminpasswort: " + adminpassword;
- }
- public void start()
- {
- // Starte Server
- listener = new TcpListener(localAdd, 8080);
- if (temp == 0)
- {
- listener.Start();
- log.Add("LISTENER STARTED");
- temp++;
- }
- log.Add("START COMPLETED");
- chat.Add("Server started at " + DateTime.Now.ToString("T"));
- th2.Start();
- }
- public void accept()
- {
- while (true)
- {
- // Warte auf Verbindung
- client = listener.AcceptTcpClient();
- client.ReceiveTimeout = 2000;
- log.Add("New REQUEST");
- // Akzeptiere Verbindung
- nwStream = client.GetStream();
- nwStream.ReadTimeout = 2000;
- byte[] buffer = new byte[client.ReceiveBufferSize];
- Thread.Sleep(50);
- if (nwStream.DataAvailable == true)
- {
- int readrequest = nwStream.Read(buffer, 0, client.ReceiveBufferSize);
- string request = Encoding.ASCII.GetString(buffer, 0, readrequest);
- log.Add("Request: " + request);
- // Neuer Befehl
- if ("/".Equals(request.Substring(0, 1)) && request.Contains(adminpassword.ToString()))
- {
- if ("freeze".Equals(request.Substring(1, 6)))
- {
- for (int i = 0; i < user.Count; i++)
- {
- if (request.Contains(user[i]))
- {
- bestrafung.Add("freeze" + " " + mac[i]);
- }
- }
- }
- else if ("unfreeze".Equals(request.Substring(1, 8)))
- {
- for (int i = 0; i < bestrafung.Count; i++)
- {
- if (request.Contains(bestrafung[i]))
- {
- bestrafung.RemoveAt(i);
- }
- }
- }
- else if ("shutdown".Equals(request.Substring(1, 8)))
- {
- for (int i = 0; i < user.Count; i++)
- {
- if (request.Contains(user[i]))
- {
- bestrafung.Add("shutdown" + " " + mac[i]);
- }
- }
- }
- else if ("chatbann".Equals(request.Substring(1, 8)))
- {
- for (int i = 0; i < user.Count; i++)
- {
- if (request.Contains(user[i]))
- {
- bestrafung.Add("chatbann" + " " + mac[i]);
- }
- }
- }
- else if ("stopshutdown".Equals(request.Substring(1, 12)))
- {
- for (int i = 0; i < bestrafung.Count; i++)
- {
- if (request.Contains(bestrafung[i]))
- {
- bestrafung.RemoveAt(i);
- }
- }
- }
- else if ("removechatbann".Equals(request.Substring(1, 14)))
- {
- for (int i = 0; i < bestrafung.Count; i++)
- {
- if (request.Contains(bestrafung[i]))
- {
- bestrafung.RemoveAt(i);
- }
- }
- }
- }
- // Neuer Chat
- else if ("newchat".Equals(request.Substring(0, 7)))
- {
- string newchat = request.Remove(0, 7);
- string chatmessage = newchat.Remove(0, 12);
- string macsender = newchat.Substring(0, 12);
- string sendername = "";
- for (int i = 0; i < mac.Count; i++)
- {
- if (macsender == mac[i])
- {
- sendername = user[i];
- }
- }
- string chatadd = (DateTime.Now.ToString("t") + " " + sendername + ": " + chatmessage);
- log.Add("Added '" + chatadd + "' to the Chat");
- chat.Add(chatadd);
- }
- // Disconnect
- else if ("deleteuser".Equals(request.Substring(0, 10)))
- {
- string deluser = request.Remove(0, 10);
- string disconnecteduser = "";
- for (int i = 0; i < mac.Count; i++)
- {
- if (deluser == mac[i])
- {
- disconnecteduser = user[i];
- }
- }
- log.Add("User '" + disconnecteduser + "' Disconnected!");
- chat.Add(disconnecteduser + " Disconnected!");
- }
- else if ("update".Equals(request.Substring(0, 6)))
- {
- string update = request.Remove(0, 6);
- log.Add("Received chat request from: " + update);
- // Sende das Update
- for (int i = 0; i < chat.Count; i++)
- {
- byte[] sendchat = ASCIIEncoding.ASCII.GetBytes(chat[i]);
- Thread.Sleep(50);
- nwStream.Write(sendchat, 0, sendchat.Length);
- log.Add("Sending : " + chat[i]);
- }
- Thread.Sleep(50);
- byte[] sendfinish1 = ASCIIEncoding.ASCII.GetBytes("finish1");
- nwStream.Write(sendfinish1, 0, sendfinish1.Length);
- Thread.Sleep(50);
- // Sende bestrafungsliste
- for (int i = 0; i < bestrafung.Count; i++)
- {
- byte[] sendbestrafung = ASCIIEncoding.ASCII.GetBytes(bestrafung[i]);
- Thread.Sleep(50);
- nwStream.Write(sendbestrafung, 0, sendbestrafung.Length);
- log.Add("Sending : " + bestrafung[i]);
- }
- Thread.Sleep(50);
- byte[] sendfinish2 = ASCIIEncoding.ASCII.GetBytes("finish2");
- nwStream.Write(sendfinish2, 0, sendfinish2.Length);
- log.Add("Sending Complete");
- }
- else if ("newuser".Equals(request.Substring(0, 7)))
- {
- string usermac = request.Remove(0, 7);
- string newusername = usermac.Remove(0, 12);
- string newusermac = usermac.Substring(0, 12);
- //Überprüfe ob user schon Regestriert ist
- for (int i = 0; i < mac.Count; i++)
- {
- if (newusermac == mac[i])
- {
- macvorhanden = true;
- }
- }
- // Überprüfen ob es den gewollten Namen schon gibt
- if (macvorhanden == false)
- {
- for (int i = 0; i < user.Count; i++)
- {
- if (newusername == user[i])
- {
- nickvorhanden = true;
- }
- }
- }
- if (macvorhanden == false)
- {
- mac.Add(newusermac);
- user.Add(newusername);
- log.Add("User '" + newusername + "' joined the Server");
- chat.Add(newusername + " joined the Server");
- }
- if (nickvorhanden == true)
- {
- bool gueltig = false;
- int immernochvorhanden = 0;
- int count = 1;
- while (gueltig == false)
- {
- newusername = newusername + count;
- for (int i = 0; i < user.Count; i++)
- {
- if (newusername == user[i])
- {
- immernochvorhanden++;
- }
- }
- if (immernochvorhanden == 0)
- {
- gueltig = true;
- }
- else
- {
- immernochvorhanden = 0;
- }
- }
- }
- nickvorhanden = false;
- macvorhanden = false;
- }
- }
- else
- {
- log.Add("CONNECTION TIMED OUT");
- }
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- button1.Enabled = false;
- button1.Visible = false;
- label5.Visible = true;
- th1 = new Thread(start);
- th2 = new Thread(accept);
- th1.Start();
- }
- private void updater_Tick(object sender, EventArgs e)
- {
- listBox1.DataSource = null;
- listBox2.DataSource = null;
- listBox3.DataSource = null;
- listBox1.DataSource = user;
- listBox2.DataSource = chat;
- listBox3.DataSource = log;
- listBox1.TopIndex = listBox1.Items.Count - 1;
- listBox2.TopIndex = listBox2.Items.Count - 1;
- listBox3.TopIndex = listBox3.Items.Count - 1;
- }
- private void Local_Chatroom_Server_SizeChanged(object sender, EventArgs e)
- {
- if (this.WindowState == FormWindowState.Maximized)
- {
- this.WindowState = FormWindowState.Normal;
- }
- else if (this.WindowState == FormWindowState.Minimized)
- {
- this.ShowInTaskbar = false;
- }
- }
- private void öffnenToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.WindowState = FormWindowState.Normal;
- this.Show();
- this.BringToFront();
- this.ShowInTaskbar = true;
- }
- private void stoppenToolStripMenuItem_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void Local_Chatroom_Server_FormClosing(object sender, FormClosingEventArgs e)
- {
- label1.Text = "SERVER SHUTDOWN!";
- log.Add("SERVER SHUTDOWN!");
- Thread.Sleep(1000);
- if (button1.Enabled == false)
- {
- listener.Stop();
- th1.Abort();
- th2.Abort();
- }
- label1.Text = "CLOSE SERVER!";
- log.Add("CLOSE SERVER!");
- }
- private void LocalChatRoomServer_MouseClick(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- {
- this.WindowState = FormWindowState.Normal;
- this.Show();
- this.BringToFront();
- this.ShowInTaskbar = true;
- }
- }
- }
- }
- // SERVER
- // Code by Ivo Tofall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement