This repo is archived. You can view files and clone it, but cannot push or open issues or pull requests.

121 lines
4.4 KiB
C#
Raw Normal View History

2021-10-06 14:30:41 +13:00
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;
}
}
}
}