Skip to content

Commit b26beb2

Browse files
committed
Create server DLL and server
1 parent ddc282e commit b26beb2

9 files changed

Lines changed: 428 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Project_Tracker_Server {
8+
class DataTransferManifest {
9+
public class Rootobject {
10+
public int AmountOpened { get; set; }
11+
public string[] DaysOpened { get; set; }
12+
public string[] TimesOpened { get; set; }
13+
public int ProjectsCount { get; set; }
14+
public string UserID { get; set; }
15+
}
16+
}
17+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Net;
7+
using System.Net.Sockets;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace Project_Tracker_Server {
12+
class Program {
13+
// WARNING: READONLY VALUES. IF YOU CHANGE THESE, CHANGE IN OTHER FILES TOO INCLUDING CLIENT
14+
private static readonly int PORT = 2784;
15+
private static readonly string FILE_SAVE_DIRECTORY =
16+
Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
17+
+ "/Project Tracker Data";
18+
private readonly static int ID_LENGTH = 30;
19+
20+
/// <summary>
21+
/// Creates a new id of numbers with the length of ID_LENGTH.
22+
/// </summary>
23+
/// <returns>Returns the new id.</returns>
24+
private static string CreateNewID() {
25+
string id = "id";
26+
Random r = new Random();
27+
28+
for (int i = 0; i < ID_LENGTH; i++) {
29+
id += r.Next(0, 9).ToString();
30+
}
31+
32+
if (id == "id") {
33+
return "null";
34+
}
35+
else {
36+
return id;
37+
}
38+
}
39+
40+
private static string GetLocalIP() {
41+
IPHostEntry host;
42+
host = Dns.GetHostEntry(Dns.GetHostName());
43+
foreach (IPAddress ip in host.AddressList) {
44+
if (ip.AddressFamily == AddressFamily.InterNetwork) {
45+
return ip.ToString();
46+
}
47+
}
48+
return "";
49+
}
50+
51+
/// <summary>
52+
/// Gets the ID from the data transmitted.
53+
/// </summary>
54+
/// <param name="data">The data that was transmitted.</param>
55+
/// <returns>Returns the username.</returns>
56+
private static string GetUserID(string data) {
57+
string json = data;
58+
DataTransferManifest.Rootobject dataValues =
59+
JsonConvert.DeserializeObject<DataTransferManifest.Rootobject>(json);
60+
61+
return dataValues.UserID;
62+
}
63+
64+
static void Main(string[] args) {
65+
if (!Directory.Exists(FILE_SAVE_DIRECTORY)) {
66+
Directory.CreateDirectory(FILE_SAVE_DIRECTORY);
67+
}
68+
69+
IPAddress ip = IPAddress.Parse(GetLocalIP());
70+
IPEndPoint localEndPoint = new IPEndPoint(ip, PORT);
71+
72+
Socket server = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
73+
server.Bind(localEndPoint);
74+
server.Listen(100);
75+
76+
Console.WriteLine("Created endpoint: " + localEndPoint);
77+
78+
while (true) {
79+
try {
80+
Console.WriteLine("Waiting for a connection...");
81+
82+
Socket client = server.Accept();
83+
84+
// Take up to 5mb of file. Should only be a few kb though
85+
byte[] buffer = new byte[1024 * 10000];
86+
87+
// Receive bytes for file
88+
int bytesReceived = client.Receive(buffer);
89+
90+
char[] chars = new char[bytesReceived];
91+
Decoder decoder = Encoding.UTF8.GetDecoder();
92+
int charLength = decoder.GetChars(buffer, 0, bytesReceived, chars, 0);
93+
string fileData = new string(chars);
94+
95+
string username = GetUserID(fileData);
96+
Console.WriteLine("Received info from " + username);
97+
98+
if (username.Length < ID_LENGTH) {
99+
username = CreateNewID();
100+
}
101+
102+
File.WriteAllText(FILE_SAVE_DIRECTORY + "\\" + username + ".json", fileData);
103+
104+
client.Close();
105+
Console.WriteLine("Closed the server");
106+
}
107+
catch (Exception e) {
108+
Console.WriteLine("We had the following error: " + e);
109+
}
110+
}
111+
}
112+
}
113+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{306CB5A5-9CF8-4F9E-8326-68485A095AEF}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>Project_Tracker_Server</RootNamespace>
10+
<AssemblyName>Project Tracker Server</AssemblyName>
11+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
37+
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
38+
</Reference>
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
<Reference Include="System.Data" />
45+
<Reference Include="System.Net.Http" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="DataTransferManifest.cs" />
50+
<Compile Include="Program.cs" />
51+
<Compile Include="Properties\AssemblyInfo.cs" />
52+
</ItemGroup>
53+
<ItemGroup>
54+
<None Include="App.config" />
55+
<None Include="packages.config" />
56+
</ItemGroup>
57+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
58+
</Project>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" />
4+
</packages>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Net.Sockets;
5+
using System.Text;
6+
7+
namespace Server_Communication_DLL {
8+
public class Connections {
9+
// WARNING: READONLY VALUES. IF YOU CHANGE THESE, CHANGE IN OTHER FILES TOO INCLUDING SERVER
10+
private static readonly int PORT = 2784;
11+
private static readonly string SERVER_IP = "192.168.1.206";
12+
private readonly static string DATA_COLLECTION_FILE =
13+
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
14+
+ "/Project Tracker/data-collection.json";
15+
16+
public static bool CreateConnection() {
17+
// Get IP Address and create endpoint
18+
IPAddress ip = IPAddress.Parse(SERVER_IP);
19+
IPEndPoint serverEndPoint = new IPEndPoint(ip, PORT);
20+
21+
Socket sender = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
22+
23+
try {
24+
FileManager.PrepareDataTransfer();
25+
26+
// Does this get the file or the file title lmao
27+
byte[] dataFile = File.ReadAllBytes(DATA_COLLECTION_FILE);
28+
byte[] userIDByteArray = Encoding.ASCII.GetBytes(FileManager.userID);
29+
30+
sender.Connect(serverEndPoint);
31+
sender.Send(dataFile);
32+
sender.Close();
33+
34+
return true;
35+
}
36+
catch (ArgumentNullException) {
37+
// Sender endpoint is null
38+
Console.WriteLine("null exception");
39+
return false;
40+
}
41+
catch (SocketException e) {
42+
// An error occurred when connecting
43+
Console.WriteLine("sock exception: " + e);
44+
return false;
45+
}
46+
catch (ObjectDisposedException) {
47+
// The socket was closed
48+
Console.WriteLine("closed exception");
49+
return true;
50+
}
51+
}
52+
}
53+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Server_Communication_DLL {
6+
class DataTransferManifest {
7+
8+
public class Rootobject {
9+
public int AmountOpened { get; set; }
10+
public string[] DaysOpened { get; set; }
11+
public string[] TimesOpened { get; set; }
12+
public int ProjectsCount { get; set; }
13+
public string UserID { get; set; }
14+
}
15+
16+
}
17+
}

0 commit comments

Comments
 (0)