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.Text;
- using System.Windows.Forms;
- using System.Threading;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- using System.Collections;
- namespace ChatServer
- {
- public partial class Form1 : Form
- {
- private delegate void UpdateStatusCallback(string strMessage);
- bool connected;
- public static int online = 0;
- // This hash table stores users and connections (browsable by user)
- public static Hashtable htUsers;// 30 users at one time limit
- // This hash table stores connections and users (browsable by connection)
- public static Hashtable htConnections; // 30 users at one time limit
- // Will store the IP address passed to it
- private IPAddress ipAddress;
- private TcpClient tcpClient;
- // The event and its argument will notify the form when a user has connected, disconnected, send message, etc.
- public static event StatusChangedEventHandler StatusChanged;
- private static StatusChangedEventArgs e;
- // The thread that will hold the connection listener
- private Thread thrListener;
- // The TCP object that listens for connections
- private TcpListener tlsClient;
- // Will tell the while loop to keep monitoring for connections
- private bool ServRunning = false;
- public Form1()
- {
- InitializeComponent();
- connected = false;
- }
- private void btnListen_Click(object sender, EventArgs e)
- {
- if (ServRunning == false)
- {
- htConnections = new Hashtable(30);
- htUsers = new Hashtable(30);
- // Parse the server's IP address out of the TextBox
- ipAddress = IPAddress.Parse(txtIp.Text);
- // Create a new instance of the ChatServer object
- // Hook the StatusChanged event handler to mainServer_StatusChanged
- StatusChanged += new StatusChangedEventHandler(mainServer_StatusChanged);
- // Start listening for connections
- StartListening();
- connected = true;
- // Show that we started to listen for connections
- txtLog.AppendText("Monitoring for connections...\r\n");
- Thread th = new Thread(updateOnline);
- th.Start();
- btnListen.Text = "Stop Listening";
- }
- else
- {
- ServRunning = false;
- btnListen.Text = "Start Listening";
- htConnections = null;
- htUsers = null;
- }
- }
- public void mainServer_StatusChanged(object sender, StatusChangedEventArgs e)
- {
- // Call the method that updates the form
- this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
- }
- private void UpdateStatus(string strMessage)
- {
- // Updates the log with the message
- txtLog.AppendText(strMessage + "\r\n");
- }
- private void Form1_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (ServRunning==false)
- {
- connected = false;
- MessageBox.Show("asd");
- stopServer();
- Environment.Exit(1);
- }
- else
- {
- Environment.Exit(1);
- }
- }
- public void updateOnline()
- {
- while (ServRunning)
- {
- label2.Text = getOnline().ToString();
- string listOfUsers = getList();
- textBox2.Text = listOfUsers;
- Thread.Sleep(1000);
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- SendAdminMessage(textBox1.Text);
- }
- private void SendAdminMessage(string Message)
- {
- try
- {
- e = new StatusChangedEventArgs(DateTime.Now.ToString()+":"+DateTime.Now.Millisecond + " Administrator: " + Message);
- OnStatusChanged(e);
- //txtLog.AppendText("asd"+ "\r\n");
- //UpdateStatus(Message);
- //txtLog.AppendText(DateTime.Now.ToString() + " Administrator: " + Message + "\r\n");
- StreamWriter swSenderSender;
- // Create an array of TCP clients, the size of the number of users we have
- TcpClient[] tcpClients = new TcpClient[htUsers.Count];
- // Copy the TcpClient objects into the array
- htUsers.Values.CopyTo(tcpClients, 0);
- // Loop through the list of TCP clients
- for (int i = 0; i < tcpClients.Length; i++)
- {
- // Try sending a message to each
- try
- {
- // If the message is blank or the connection is null, break out
- if (Message.Trim() == "" || tcpClients[i] == null)
- {
- continue;
- }
- // Send the message to the current user in the loop
- swSenderSender = new StreamWriter(tcpClients[i].GetStream());
- swSenderSender.WriteLine(DateTime.Now.ToString() + " Administrator: " + Message);
- swSenderSender.Flush();
- swSenderSender = null;
- }
- catch // If there was a problem, the user is not there anymore, remove him
- {
- RemoveUser(tcpClients[i]);
- }
- }
- }
- catch
- {
- MessageBox.Show("asd");
- }
- }
- private void AddUser(TcpClient tcpUser, string strUsername)
- {
- // First add the username and associated connection to both hash tables
- htUsers.Add(strUsername, tcpUser);
- htConnections.Add(tcpUser, strUsername);
- // Tell of the new connection to all other users and to the server form
- SendAdminMessage(htConnections[tcpUser] + " has joined FROYO");
- }
- public static int getOnlineOnline()
- {
- return online;
- }
- // Remove the user from the hash tables
- private void RemoveUser(TcpClient tcpUser)
- {
- // If the user is there
- if (htConnections[tcpUser] != null)
- {
- // First show the information and tell the other users about the disconnection
- SendAdminMessage(htConnections[tcpUser] + " has left FROYO");
- // Remove the user from the hash table
- htUsers.Remove(htConnections[tcpUser]);
- htConnections.Remove(tcpUser);
- }
- }
- public void stopServer()
- {
- ServRunning = false;
- }
- // This is called when we want to raise the StatusChanged event
- public static void OnStatusChanged(StatusChangedEventArgs e)
- {
- StatusChangedEventHandler statusHandler = StatusChanged;
- if (statusHandler != null)
- {
- // Invoke the delegate
- statusHandler(null, e);
- }
- }
- public static int getOnline()
- {
- return htUsers.Count;
- }
- // Send administrative messages
- // Send messages from one user to all the others
- private void SendMessage(string From, string Message)
- {
- StreamWriter swSenderSender;
- // First of all, show in our application who says what
- e = new StatusChangedEventArgs(DateTime.Now.ToString() + " " + From + ": " + Message);
- OnStatusChanged(e);
- // Create an array of TCP clients, the size of the number of users we have
- TcpClient[] tcpClients = new TcpClient[htUsers.Count];
- // Copy the TcpClient objects into the array
- htUsers.Values.CopyTo(tcpClients, 0);
- int count = 0;
- string[] users = new string[htUsers.Count];
- foreach (object key in htUsers.Keys)
- {
- users[count] = key.ToString();
- count++;
- }
- // Loop through the list of TCP clients
- for (int i = 0; i < tcpClients.Length; i++)
- {
- // Try sending a message to each
- try
- {
- // If the message is blank or the connection is null, break out
- if (Message.Trim() == "" || tcpClients[i] == null || users[i] == From)
- {
- continue;
- }
- // Send the message to the current user in the loop
- swSenderSender = new StreamWriter(tcpClients[i].GetStream());
- swSenderSender.WriteLine(DateTime.Now.ToString() + " " + From + ": " + Message);
- swSenderSender.Flush();
- swSenderSender = null;
- }
- catch // If there was a problem, the user is not there anymore, remove him
- {
- RemoveUser(tcpClients[i]);
- }
- }
- }
- public void StartListening()
- {
- int port = 1986;
- // Get the IP of the first network device, however this can prove unreliable on certain configurations
- IPAddress ipaLocal = ipAddress;
- // Create the TCP listener object using the IP of the server and the specified port
- tlsClient = new TcpListener(ipaLocal,port);
- // Start the TCP listener and listen for connections
- tlsClient.Start();
- // The while loop will check for true in this before checking for connections
- ServRunning = true;
- // Start the new tread that hosts the listener
- thrListener = new Thread(KeepListening);
- thrListener.Start();
- }
- private void KeepListening()
- {
- // While the server is running
- while (ServRunning == true)
- {
- // Accept a pending connection
- tcpClient = tlsClient.AcceptTcpClient();
- if (tcpClient != null)
- {
- Connection newConnection = new Connection(tcpClient);
- online++;
- }
- // Create a new instance of Connection
- tcpClient = null;
- Thread.Sleep(100);
- }
- tcpClient = null;
- tlsClient.Stop();
- tlsClient = null;
- }
- public static string getList()
- {
- string users = "";
- foreach (object key in htUsers.Keys)
- {
- users += key.ToString() + "\r\n";
- }
- return users;
- }
- class Connection
- {
- TcpClient tcpClient;
- // The thread that will send information to the client
- private Thread thrSender;
- private StreamReader srReceiver;
- private StreamWriter swSender;
- private string currUser;
- private string strResponse;
- private string KindOf;
- Form1 sa = new Form1();
- // The constructor of the class takes in a TCP connection
- public Connection(TcpClient tcpCon)
- {
- tcpClient = tcpCon;
- // The thread that accepts the client and awaits messages
- thrSender = new Thread(AcceptClient);
- // The thread calls the AcceptClient() method
- thrSender.Start();
- }
- public void SendOnline(string amount)
- {
- StreamWriter swSenderSender;
- // Create an array of TCP clients, the size of the number of users we have
- TcpClient[] tcpClients = new TcpClient[htUsers.Count];
- // Copy the TcpClient objects into the array
- htUsers.Values.CopyTo(tcpClients, 0);
- // Loop through the list of TCP clients
- for (int i = 0; i < tcpClients.Length; i++)
- {
- // Try sending a message to each
- try
- {
- // If the message is blank or the connection is null, break out
- if (amount.Trim() == "" || tcpClients[i] == null)
- {
- continue;
- }
- // Send the message to the current user in the loop
- swSenderSender = new StreamWriter(tcpClients[i].GetStream());
- swSenderSender.WriteLine(amount);
- swSenderSender.Flush();
- swSenderSender = null;
- }
- catch // If there was a problem, the user is not there anymore, remove him
- {
- sa.RemoveUser(tcpClients[i]);
- }
- }
- }
- private void CloseConnection()
- {
- // Close the currently open objects
- KindOf = "close";
- }
- // Occures when a new client is accepted
- private void AcceptClient()
- {
- KindOf = "message";
- srReceiver = new StreamReader(tcpClient.GetStream());
- swSender = new StreamWriter(tcpClient.GetStream());
- // Read the account information from the client
- currUser = srReceiver.ReadLine();
- // We got a response from the client
- if (currUser != "")
- {
- // Store the user name in the hash table
- if (htUsers.Contains(currUser) == true)
- {
- // 0 means not connected
- swSender.WriteLine("0|This username already exists.");
- swSender.Flush();
- CloseConnection();
- return;
- }
- else if (currUser == "Administrator")
- {
- // 0 means not connected
- swSender.WriteLine("0|This username is reserved.");
- swSender.Flush();
- CloseConnection();
- return;
- }
- else
- {
- // 1 means connected successfully
- swSender.WriteLine("1");
- swSender.Flush();
- // Add the user to the hash tables and start listening for messages from him
- sa. AddUser(tcpClient, currUser);
- }
- }
- else
- {
- CloseConnection();
- return;
- }
- SendOnline("♥" + getOnline());
- string users = "";
- foreach (object key in htUsers.Keys)
- {
- users += key.ToString() + "*";
- }
- SendOnline("▲" + users);
- try
- {
- strResponse = getMessage();
- // Keep waiting for a message from the user
- while (strResponse != "close")
- {
- // If it's invalid, remove the user
- if (strResponse == null)
- {
- sa.RemoveUser(tcpClient);
- }
- else
- {
- // Otherwise send the message to all the other users
- sa.SendMessage(currUser, strResponse);
- }
- strResponse = getMessage();
- Thread.Sleep(100);
- }
- tcpClient.Close();
- srReceiver.Close();
- swSender.Close();
- }
- catch
- {
- // If anything went wrong with this user, disconnect him
- sa.RemoveUser(tcpClient);
- SendOnline("♥" + getOnline());
- string users2 = "";
- foreach (object key in htUsers.Keys)
- {
- users2 += key.ToString() + "*";
- }
- SendOnline("▲" + users2);
- }
- }
- private string getMessage()
- {
- if (KindOf == "close")
- {
- return KindOf;
- }
- if (KindOf == "message")
- {
- return srReceiver.ReadLine();
- }
- return "";
- }
- }
- public delegate void StatusChangedEventHandler(object sender, StatusChangedEventArgs e);
- public class StatusChangedEventArgs : EventArgs
- {
- // The argument we're interested in is a message describing the event
- private string EventMsg;
- // Property for retrieving and setting the event message
- public string EventMessage
- {
- get
- {
- return EventMsg;
- }
- set
- {
- EventMsg = value;
- }
- }
- // Constructor for setting the event message
- public StatusChangedEventArgs(string strEventMsg)
- {
- EventMsg = strEventMsg;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement