using System.Net.Sockets;
using System.Collections.Generic;
public NetworkStream stream;
public bool PersistantConnect;
public bool DataAvailable;
private Thread CheckConnectionsThread;
private Thread CheckAvailabilityThread;
public bool Connect(string ip, int port)
CheckConnectionsThread = new Thread(new ThreadStart(CheckConnections));
CheckConnectionsThread.IsBackground = true;
CheckConnectionsThread.Start();
CheckAvailabilityThread = new Thread(new ThreadStart(CheckAvailability));
CheckAvailabilityThread.IsBackground = true;
CheckAvailabilityThread.Start();
catch(Exception){return false;}
public void write(string data)
stream.Write(Encoding.UTF8.GetBytes(data), 0, Encoding.UTF8.GetBytes(data).Length);
catch(Exception){Console.WriteLine("Error writing to stream");}
public void write(Dictionary<string, string> dictionary)
stream.Write(Encoding.UTF8.GetBytes(ToString(dictionary)), 0, Encoding.UTF8.GetBytes(ToString(dictionary)).Length);
catch(Exception){Console.WriteLine("Error writing to stream");}
public void CheckAvailability()
if(stream.DataAvailable && !DataAvailable)
else if(!stream.DataAvailable && DataAvailable)
public static Dictionary<string,string> ToDictionary(string data)
Dictionary<string,string> result = new Dictionary<string, string>();
string[] sections = data.Split(',');
foreach(string section in sections)
string[] KeyValue = section.Split(':');
result.Add(KeyValue[0].Replace("$01$", ":").Replace("$02$", ",") , KeyValue[1].Replace("$01$", ":").Replace("$02$", ","));
public static string ToString(Dictionary<string,string> dictionary)
foreach(KeyValuePair<string, string> entry in dictionary)
result += entry.Key.Replace(":", "$01$").Replace(",", "$02$") + ":" + entry.Value.Replace(":", "$01$").Replace(",", "$02$") + ",";
public Dictionary<string, string> read()
string data = ReadToEnd();
return ToDictionary(data);
public string ReadToEnd()
return "THIS IS FOR TEST PURPOSES";
private void CheckConnections()
bool part1 = socket.Poll(1000, SelectMode.SelectRead);
bool part2 = (socket.Available == 0);