Archived
Added solution files
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")]
|
||||
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>739200ea-9900-4dcc-8a72-bbadc9e73a47</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>TCPServerLWCore</RootNamespace>
|
||||
<AssemblyName>TCPServerLWCore</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System"/>
|
||||
|
||||
<Reference Include="System.Core"/>
|
||||
<Reference Include="System.Xml.Linq"/>
|
||||
<Reference Include="System.Data.DataSetExtensions"/>
|
||||
|
||||
|
||||
<Reference Include="Microsoft.CSharp"/>
|
||||
|
||||
<Reference Include="System.Data"/>
|
||||
|
||||
<Reference Include="System.Xml"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Class1.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user