diff --git a/App.config b/App.config new file mode 100644 index 0000000000000000000000000000000000000000..193aecc675e7d4600fde2d37cf350bce7a056948 --- /dev/null +++ b/App.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8" ?> +<configuration> + <startup> + <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" /> + </startup> +</configuration> \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..c726dbc18b63e5300f0b03a3d968d3e60324baa2 --- /dev/null +++ b/Program.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TcpServerTest +{ + class Program + { + static void Main(string[] args) + { + + if (args.Length > 0) + { + if (args[0] == "server") + { + TCPServer.StartServer(); + } + + if (args[0] == "client") + { + TCPClient.connect(args[1]); + } + } + else + { + TCPServer.StartServer(); + Console.ReadLine(); + } + + + + } + } +} diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..9fdb095efc8c07a96640813128987a5c5e37901c --- /dev/null +++ b/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("TcpServerTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TcpServerTest")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("4155c8df-efa9-4d9d-a42b-ff684fd5a316")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, +// indem Sie "*" wie unten gezeigt eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/TCPClient.cs b/TCPClient.cs new file mode 100644 index 0000000000000000000000000000000000000000..53ac817db06936f1945874be384dcb4dd7b94d10 --- /dev/null +++ b/TCPClient.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Sockets; +using System.Text; +using System.Threading.Tasks; + +namespace TcpServerTest +{ + class TCPClient + { + public static void connect(string filename) + { + try + { + // Set the IP address and port number of the server to connect + string serverIP = "172.29.28.63"; + int serverPort = 12345; + long offset = 0; + byte[] receivedData = new byte[32768]; + byte[] nullbuffer = new byte[3]; + + + nullbuffer[0] = 0x4b; + nullbuffer[1] = 0x4d; + nullbuffer[2] = 0x54; + + Console.WriteLine("TCPclient started."); + + + + // Create a TcpClient to connect to the server + using (TcpClient tcpClient = new TcpClient(serverIP, serverPort)) + { + Console.WriteLine($"Connected to {serverIP}:{serverPort}"); + + var start = DateTime.Now; + + // Get the network stream for reading and writing data + using (NetworkStream clientStream = tcpClient.GetStream()) + { + // Example: Send data to the server + + + + using (FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read)) + { + var bits = new byte[32768]; + int bytesRead; + + // Loop to read the file in chunks + while ((bytesRead = fileStream.Read(bits, 0, 32768)) > 0) + { + // Process the chunk of data (you can modify this part) + clientStream.Write(bits, 0, bytesRead); + offset = offset + bytesRead; + } + } + + Console.Write("Write END"); + clientStream.Write(nullbuffer, 0, 3); + + + // wait for answer from Server + while (true) + { + Console.Write("."); + int bytesRead = clientStream.Read(receivedData, 0, 32768); + if (bytesRead != 0) break; + } + + + + } + var end = DateTime.Now; + float speed = (float)((offset / (1024 * (end - start).TotalMilliseconds)) * 1000); + Console.WriteLine(); + Console.WriteLine("done in " + ((end - start).TotalMilliseconds).ToString()); + Console.WriteLine("Transferred with " + speed.ToString() + " kB/s"); + + } + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + + } + + + + + } +} diff --git a/TCPServer.cs b/TCPServer.cs new file mode 100644 index 0000000000000000000000000000000000000000..fc028f8f61c177ee7ff1407fbde22423a0569fab --- /dev/null +++ b/TCPServer.cs @@ -0,0 +1,155 @@ +using System; +using System.IO; +using System.Net; +using System.Net.Sockets; +using System.Text; +using System.Threading; + +class TCPServer +{ + + static byte[] receivedData = new byte[32768]; + static byte[] nullbuffer = new byte[3]; + + + + public static void StartServer() + { + nullbuffer[0] = 0x45; + nullbuffer[1] = 0x4e; + nullbuffer[2] = 0x44; + + TcpListener tcpListener = null; + try + { + // Set the IP address and port number on which the server will listen + IPAddress ipAddress = IPAddress.Parse("192.168.178.20"); + int port = 12345; + + // Create the TcpListener + tcpListener = new TcpListener(ipAddress, port); + + // Start listening for client requests + tcpListener.Start(); + + Console.WriteLine($"Server is listening on {ipAddress}:{port}"); + + while (true) + { + // Accept a pending connection request from a client + TcpClient tcpClient = tcpListener.AcceptTcpClient(); + + // Create a separate thread to handle the client + Thread clientThread = new Thread(HandleClient); + clientThread.Start(tcpClient); + } + } + catch (Exception ex) + { + Console.WriteLine($"Error: {ex.Message}"); + } + finally + { + // Stop listening for new clients if an exception occurs + tcpListener?.Stop(); + } + } + + + static void write_file(Stream clientStream, string filename) + { + int bytesRead; + long offset = 0; + var start = DateTime.Now; + var bits = new byte[32768]; + + using (FileStream fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\" + filename, FileMode.Open, FileAccess.Read)) + { + + // Loop to read the file in chunks + bytesRead = 1; + while (bytesRead > 0) + { + bytesRead = fileStream.Read(bits, 0, 32768); + clientStream.Write(bits, 0, bytesRead); + offset = offset + bytesRead; + //Console.WriteLine($"{offset}-{bytesRead}"); + } + } + + Console.Write("Write END to client"); + clientStream.Write(nullbuffer, 0, 3); + clientStream.Flush(); + + // wait for answer from Client + while (true) + { + Console.Write(":"); + bytesRead = clientStream.Read(receivedData, 0, 32768); + if (bytesRead == 0) break; + } + + + var end = DateTime.Now; + float speed = (float)((offset / (1024 * (end - start).TotalMilliseconds)) * 1000); + Console.WriteLine(); + Console.WriteLine("done in " + ((end - start).TotalMilliseconds).ToString() + "ms"); + Console.WriteLine("Transferred with " + speed.ToString() + " kB/s"); + } + + // ***************************************************************************** + // Client Thread + // ***************************************************************************** + static void HandleClient(object obj) + { + TcpClient tcpClient = (TcpClient)obj; + NetworkStream clientStream = tcpClient.GetStream(); + string command = ""; + + + byte[] message = new byte[32768]; + byte[] response = new byte[1]; + response[0] = 0x41; + + + int bytesRead; + + try + { + + bytesRead = clientStream.Read(message, 0, 32768); + if (bytesRead>4) + { + command = Encoding.UTF8.GetString(message, 0, 5); + Console.WriteLine(Encoding.UTF8.GetString(message,0,bytesRead)); + } + + if (command=="GET |") + { + string filename = Encoding.UTF8.GetString(message, 5, bytesRead - 5); + Console.WriteLine($"writing {filename} to client"); + write_file(clientStream, filename); + } + + if (command=="UPDA|") + { + Console.WriteLine("Writing updated files in response to UPDA command."); + } + + if (command=="CHKS|") + { + Console.WriteLine("Getting MD5 checksum of file..."); + } + + + + } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally + { + tcpClient.Close(); + } + + Console.WriteLine("close stream"); + + } +} + \ No newline at end of file diff --git a/TcpServerTest.csproj b/TcpServerTest.csproj new file mode 100644 index 0000000000000000000000000000000000000000..502d11a2da4e9a985baf5a5d5f4a473f3ff9adf9 --- /dev/null +++ b/TcpServerTest.csproj @@ -0,0 +1,55 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="15.0" 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>{4155C8DF-EFA9-4D9D-A42B-FF684FD5A316}</ProjectGuid> + <OutputType>Exe</OutputType> + <RootNamespace>TcpServerTest</RootNamespace> + <AssemblyName>TcpServerTest</AssemblyName> + <TargetFrameworkVersion>v4.8</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> + <Deterministic>true</Deterministic> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <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' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <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.Net.Http" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Program.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="TCPClient.cs" /> + <Compile Include="TCPServer.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="App.config" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> +</Project> \ No newline at end of file diff --git a/TcpServerTest.sln b/TcpServerTest.sln new file mode 100644 index 0000000000000000000000000000000000000000..6240181104c3aa838a1f1e0eb7eaf24ed844c689 --- /dev/null +++ b/TcpServerTest.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.33801.447 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TcpServerTest", "TcpServerTest.csproj", "{4155C8DF-EFA9-4D9D-A42B-FF684FD5A316}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4155C8DF-EFA9-4D9D-A42B-FF684FD5A316}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4155C8DF-EFA9-4D9D-A42B-FF684FD5A316}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4155C8DF-EFA9-4D9D-A42B-FF684FD5A316}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4155C8DF-EFA9-4D9D-A42B-FF684FD5A316}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {108FD5E9-E903-4498-B2DF-C49CFF05E101} + EndGlobalSection +EndGlobal