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.Threading.Tasks;
- using System.Windows.Forms;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- namespace chat_server
- {
- public partial class Form1 : Form
- {
- private Random rnd = new Random();
- TcpListener lyssnare;
- TcpClient[] client = new TcpClient[24];
- User[] klient = new User[24];
- int port = 12345;
- int antalklienter = 0;
- int antaladmins = 0;
- public Form1()
- {
- InitializeComponent();
- }
- private void btnStart_Click(object sender, EventArgs e)
- {
- try
- {
- lyssnare = new TcpListener(IPAddress.Any, port);
- lyssnare.Start();
- }
- catch (Exception error) { MessageBox.Show(error.Message, Text); return; }
- btnStart.Enabled = false;
- StartaMottagning();
- }
- public async void StartaMottagning()
- {
- try
- {
- Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
- klient[antalklienter] = new StandardUser(antalklienter.ToString(), "123", IPAddress.Any, randomColor);
- klient[antalklienter].client = await lyssnare.AcceptTcpClientAsync();
- }
- catch (Exception error) { MessageBox.Show(error.Message, Text); return; }
- StartaLäsning(klient[antalklienter].getClient());
- tbxLog.AppendText(klient[antalklienter].getClient().Client.RemoteEndPoint.ToString() + " Connected! \n");
- antalklienter++;
- StartaMottagning();
- }
- public async void StartaLäsning(TcpClient k)
- {
- byte[] buffert = new byte[1024];
- int n = 0;
- try
- {
- n = await k.GetStream().ReadAsync(buffert, 0, buffert.Length);
- }
- catch (Exception error) { MessageBox.Show(error.Message, Text); return; }
- if (Encoding.Unicode.GetString(buffert, 0, n)[0] == '4')
- {
- TcpClient tempclient = klient[antalklienter].client;
- klient[antalklienter] = new Admin(klient[antalklienter].getAlias(), klient[antalklienter].getPassword(), klient[antalklienter].getIP(), klient[antalklienter].getColor(), true, true, true);
- klient[antalklienter].client = tempclient;
- }
- string ifadmin = "";
- try
- {
- if (Encoding.Unicode.GetString(buffert, 0, n)[0] == '4')
- {
- ifadmin = "ADMIN :";
- }
- }
- catch (Exception error) { return; }
- int aliasPos = Encoding.Unicode.GetString(buffert, 0, n).IndexOf("1337", 0);
- string alias = Encoding.Unicode.GetString(buffert, 0, n).Substring(1, aliasPos - 1);
- string chatt = File.ReadAllText(@"C:\Users\hamjoh64\Documents\Visual Studio 2015\Projects\Programmering2\chat-server\bin\Debug\log.txt");
- string newLine = ifadmin + alias + ": " + Encoding.Unicode.GetString(buffert, 0, n).Substring(alias.Length + 4) + Environment.NewLine;
- File.AppendAllText(@"C:\Users\hamjoh64\Documents\Visual Studio 2015\Projects\Programmering2\chat-server\bin\Debug\log.txt", newLine);
- chatt = File.ReadAllText(@"C:\Users\hamjoh64\Documents\Visual Studio 2015\Projects\Programmering2\chat-server\bin\Debug\log.txt");
- tbxLog.AppendText(ifadmin + alias + ": ");
- tbxLog.AppendText( Encoding.Unicode.GetString(buffert, 0, n).Substring(alias.Length + 4) + "\n");
- StartaSändning(File.ReadAllText(@"C:\Users\hamjoh64\Documents\Visual Studio 2015\Projects\Programmering2\chat-server\bin\Debug\log.txt"), k, ifadmin);
- StartaLäsning(k);
- }
- public async void StartaSändning(string message, TcpClient k, string ifadmin)
- {
- byte[] utData = Encoding.Unicode.GetBytes(ifadmin + message);
- try
- {
- await k.GetStream().WriteAsync(utData, 0, utData.Length);
- }
- catch (Exception error) { MessageBox.Show(error.Message, Text); return; }
- }
- private void btnFlush_Click(object sender, EventArgs e)
- {
- File.WriteAllText(@"C:\Users\hamjoh64\Documents\Visual Studio 2015\Projects\Programmering2\chat-server\bin\Debug\log.txt", "");
- }
- }
- abstract class User
- {
- protected string name;
- protected string password;
- protected IPAddress ip;
- public TcpClient client;
- protected Color color;
- public User(string name, string password, IPAddress ip, Color color)
- {
- this.name = name;
- this.password = password;
- this.ip = ip;
- this.color = color;
- }
- public IPAddress getIP()
- {
- return ip;
- }
- public TcpClient getClient()
- {
- return client;
- }
- public string getAlias()
- {
- return name;
- }
- public Color getColor()
- {
- return color;
- }
- public string getPassword()
- {
- return password;
- }
- protected void kick()
- {
- }
- protected void ban(string reason)
- {
- kick();
- }
- protected void unBan()
- {
- }
- protected void directMessage(string message)
- {
- }
- }
- class StandardUser : User
- {
- public StandardUser(string name, string password, IPAddress ip, Color color) : base(name, password, ip, color)
- {
- }
- }
- class Admin : User
- {
- protected bool canKick;
- protected bool canBan;
- protected bool canRename;
- public Admin(string name, string password, IPAddress ip, Color color, bool canKick, bool canBan, bool canRename) : base(name, password, ip, color)
- {
- this.canKick = canKick;
- this.canBan = canBan;
- this.canRename = canRename;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement