From 212d99d36c107ede3c0ffc4f124c9ea5fff6fdac Mon Sep 17 00:00:00 2001 From: Brychan Dempsey Date: Wed, 6 Oct 2021 14:30:41 +1300 Subject: [PATCH] Added solution files --- TCPServerLWCore/Class1.cs | 120 ++++++++++++++++++++ TCPServerLWCore/Properties/AssemblyInfo.cs | 36 ++++++ TCPServerLWCore/TCPServerLWCore.csproj | 59 ++++++++++ ThermalWebServer.sln | 28 +++++ ThermalWebServer/Program.cs | 19 ++++ ThermalWebServer/Properties/AssemblyInfo.cs | 36 ++++++ ThermalWebServer/ThermalWebServer.csproj | 61 ++++++++++ 7 files changed, 359 insertions(+) create mode 100644 TCPServerLWCore/Class1.cs create mode 100644 TCPServerLWCore/Properties/AssemblyInfo.cs create mode 100644 TCPServerLWCore/TCPServerLWCore.csproj create mode 100644 ThermalWebServer.sln create mode 100644 ThermalWebServer/Program.cs create mode 100644 ThermalWebServer/Properties/AssemblyInfo.cs create mode 100644 ThermalWebServer/ThermalWebServer.csproj diff --git a/TCPServerLWCore/Class1.cs b/TCPServerLWCore/Class1.cs new file mode 100644 index 0000000..c4f688f --- /dev/null +++ b/TCPServerLWCore/Class1.cs @@ -0,0 +1,120 @@ +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; + +namespace TCPServerLWCore +{ + public class Server + { + public IPAddress[] ipAddress; + public IPEndPoint ipEnd; + public Socket sock; + public TcpListener tcpHandler; + public bool isActive = true; + public int portNumber = 65535; + + public void initServer() + { + ipAddress = new IPAddress[1]; + ipAddress[0] = IPAddress.Parse("192.168.1.22"); + tcpHandler = new TcpListener(ipAddress[0], portNumber); + tcpHandler.Start(); + while (isActive) + { + TcpClient client = tcpHandler.AcceptTcpClient(); + ServerObjectHandler handler = new ServerObjectHandler(client, this); + Thread thread = new Thread(new ThreadStart(handler.process)); + thread.Start(); + Thread.Sleep(1); + } + } + class ServerObjectHandler + { + public string filePath = "\\log\\log.csv"; + public Stream inputStream; + public TcpClient socket; + public Server srv; + public ServerObjectHandler(TcpClient s, Server srv) + { + this.socket = s; + this.srv = srv; + } + public void process() // Recieves an input stream. Data from the stream is sorted by bytes and needs to occur + { + Console.WriteLine("Connection Recieved"); + inputStream = new BufferedStream(socket.GetStream()); + StreamReader reader = new StreamReader(inputStream); + MemoryStream ReadStream = new MemoryStream(); + byte[] buffer = new byte[1]; + int i = 0; + int dataRead; + char[] streamArray = new char[1]; + try + { + while((dataRead = inputStream.Read(buffer,0,buffer.Length)) > 0) + { + ReadStream.Write(buffer, 0, dataRead); + } + byte[] result = ReadStream.ToArray(); + streamArray = Encoding.ASCII.GetChars(result); + } + catch (IOException) + { + Console.WriteLine("End of stream"); + } + Console.Write(String.Concat(streamArray) + Environment.NewLine); + if(String.Concat(streamArray) == "") + { + + } + string[] tempSegments = string.Concat(streamArray).Split('/'); + Array.Resize(ref tempSegments, tempSegments.Length - 1); + string finalString = ""; + foreach(string s in tempSegments) + { + string temp = (s.Split('|')[2]).Replace("/", ""); + string humidty = s.Split('|')[1]; + finalString += temp + "," + humidty + Environment.NewLine; + } + + Directory.CreateDirectory(Path.Combine(Directory.GetCurrentDirectory(), "\\log\\")); + bool writeSuccess = false; + int loopInc = 0; + while(!writeSuccess) + { + try + { + if (!File.Exists(Path.Combine(Directory.GetCurrentDirectory(), filePath))) + { + File.AppendAllText(Path.Combine(Directory.GetCurrentDirectory(), filePath), "Temperature,Humidity" + Environment.NewLine); + Thread.Sleep(500); + } + File.AppendAllText(Path.Combine(Directory.GetCurrentDirectory(), filePath), finalString); + writeSuccess = true; + } + catch (IOException) + { + Console.WriteLine("File in use"); + if(loopInc >10) + { + throw new IOException("File is in use and could not be unlocked"); + } + Thread.Sleep(500); + loopInc++; + } + } + + } + public bool IsWebRequest() + { + return true; + } + } + } +} + diff --git a/TCPServerLWCore/Properties/AssemblyInfo.cs b/TCPServerLWCore/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7215730 --- /dev/null +++ b/TCPServerLWCore/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TCPServerLWCore")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TCPServerLWCore")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("739200ea-9900-4dcc-8a72-bbadc9e73a47")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TCPServerLWCore/TCPServerLWCore.csproj b/TCPServerLWCore/TCPServerLWCore.csproj new file mode 100644 index 0000000..caa333c --- /dev/null +++ b/TCPServerLWCore/TCPServerLWCore.csproj @@ -0,0 +1,59 @@ + + + + + Debug + AnyCPU + 739200ea-9900-4dcc-8a72-bbadc9e73a47 + Library + Properties + TCPServerLWCore + TCPServerLWCore + v4.0 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + + + + diff --git a/ThermalWebServer.sln b/ThermalWebServer.sln new file mode 100644 index 0000000..97f9d5c --- /dev/null +++ b/ThermalWebServer.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.24720.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ThermalWebServer", "ThermalWebServer\ThermalWebServer.csproj", "{D8A53F10-F395-4FD7-B626-8AEC0B68DA17}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TCPServerLWCore", "TCPServerLWCore\TCPServerLWCore.csproj", "{739200EA-9900-4DCC-8A72-BBADC9E73A47}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D8A53F10-F395-4FD7-B626-8AEC0B68DA17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D8A53F10-F395-4FD7-B626-8AEC0B68DA17}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D8A53F10-F395-4FD7-B626-8AEC0B68DA17}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D8A53F10-F395-4FD7-B626-8AEC0B68DA17}.Release|Any CPU.Build.0 = Release|Any CPU + {739200EA-9900-4DCC-8A72-BBADC9E73A47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {739200EA-9900-4DCC-8A72-BBADC9E73A47}.Debug|Any CPU.Build.0 = Debug|Any CPU + {739200EA-9900-4DCC-8A72-BBADC9E73A47}.Release|Any CPU.ActiveCfg = Release|Any CPU + {739200EA-9900-4DCC-8A72-BBADC9E73A47}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/ThermalWebServer/Program.cs b/ThermalWebServer/Program.cs new file mode 100644 index 0000000..2156510 --- /dev/null +++ b/ThermalWebServer/Program.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Net.Sockets; +using TCPServerLWCore; + +namespace ThermalWebServer +{ + class Program + { + static void Main(string[] args) + { + Server tcpServer = new Server(); + tcpServer.initServer(); + Console.ReadKey(); + } + } +} diff --git a/ThermalWebServer/Properties/AssemblyInfo.cs b/ThermalWebServer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..a224ef6 --- /dev/null +++ b/ThermalWebServer/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("ThermalWebServer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("ThermalWebServer")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("d8a53f10-f395-4fd7-b626-8aec0b68da17")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/ThermalWebServer/ThermalWebServer.csproj b/ThermalWebServer/ThermalWebServer.csproj new file mode 100644 index 0000000..7dedc97 --- /dev/null +++ b/ThermalWebServer/ThermalWebServer.csproj @@ -0,0 +1,61 @@ + + + + + Debug + AnyCPU + {D8A53F10-F395-4FD7-B626-8AEC0B68DA17} + Exe + Properties + ThermalWebServer + ThermalWebServer + v4.0 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + {739200ea-9900-4dcc-8a72-bbadc9e73a47} + TCPServerLWCore + + + + + \ No newline at end of file