Some Basic stuff

This commit is contained in:
Fabian Stamm 2016-11-21 20:24:22 +01:00
parent 42497809af
commit 23836945c9
8 changed files with 195 additions and 9 deletions

View File

@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.25914.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SMTPServer", "SMTPServer\SMTPServer.csproj", "{ABB6A3E6-38B6-4D02-AC9C-91FA69CF03BE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D43013F8-933F-4ADC-8943-08E91662A070}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU

View File

@ -0,0 +1,22 @@
using System;
namespace SMTPServer.Exceptions
{
public class PortUsedException : Exception
{
public PortUsedException() : base()
{
}
public PortUsedException(string message) : base(message)
{
}
public PortUsedException(string message, Exception inner) : base(message, inner)
{
}
}
}

View File

@ -0,0 +1,64 @@
using SMTPServer.Exceptions;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SMTPServer
{
public delegate void ConnectedEventHandler(object source, ConnectedEventArgs e);
public class ConnectedEventArgs : EventArgs
{
private string EventInfo;
public Socket Client { get; private set; }
public ConnectedEventArgs(Socket client)
{
Client = client;
}
}
public class PortListener
{
static List<int> _Ports = null;
TcpListener _Listener;
public event ConnectedEventHandler OnConnected;
public PortListener(int port)
{
if(_Ports == null)
{
_Ports = new List<int>();
}
foreach(var i in _Ports)
{
if (i == port)
{
throw new PortUsedException();
}
}
_Ports.Add(port);
StartListening(new TcpListener(IPAddress.Any, port));
}
private void StartListening(TcpListener listener)
{
listener.Start();
_Listener = listener;
Thread thread = new Thread(new ThreadStart(ListenerAsync));
}
private async void ListenerAsync()
{
var client = await _Listener.AcceptSocketAsync();
OnConnected(this, new ConnectedEventArgs(client));
}
}
}

View File

@ -1,9 +1,20 @@
using System;
class Program
namespace SMTPServer
{
static void Main(string[] args)
class Program
{
Console.WriteLine("Hello World!");
static void Main(string[] args)
{
Console.WriteLine("####################");
var listener = new PortListener(5122);
listener.OnConnected += Listener_OnConnected;
}
private static void Listener_OnConnected(object source, ConnectedEventArgs e)
{
throw new NotImplementedException();
}
}
}

View File

@ -1,25 +1,24 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="**\*.cs" />
<EmbeddedResource Include="**\*.resx" />
</ItemGroup>
<ItemGroup>
<Compile Include="PortListener.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.App">
<Version>1.0.1</Version>
<Version>1.1.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.NET.Sdk">
<Version>1.0.0-alpha-20161104-2</Version>
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace SMTPServer
{
class StartTcpConnection
{
public StartTcpConnection(int port, IPAddress destination)
{
}
}
}

View File

@ -0,0 +1,64 @@
using SMTPServer.Exceptions;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace SMTPServer
{
public delegate void ConnectedEventHandler(object source, ConnectedEventArgs e);
public class ConnectedEventArgs : EventArgs
{
private string EventInfo;
public Socket Client { get; private set; }
public ConnectedEventArgs(Socket client)
{
Client = client;
}
}
public class TCPListener
{
static List<int> _Ports = null;
TcpListener _Listener;
public event ConnectedEventHandler OnConnected;
public TCPListener(int port)
{
if(_Ports == null)
{
_Ports = new List<int>();
}
foreach(var i in _Ports)
{
if (i == port)
{
throw new PortUsedException();
}
}
_Ports.Add(port);
StartListening(new TcpListener(IPAddress.Any, port));
}
private void StartListening(TcpListener listener)
{
listener.Start();
_Listener = listener;
Thread thread = new Thread(new ThreadStart(ListenerAsync));
}
private async void ListenerAsync()
{
var client = await _Listener.AcceptSocketAsync();
OnConnected(this, new ConnectedEventArgs(client));
}
}
}

View File

@ -0,0 +1,8 @@
using System;
public class Class1
{
public Class1()
{
}
}