Some Basic stuff
This commit is contained in:
parent
42497809af
commit
23836945c9
@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.25914.0
|
|||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SMTPServer", "SMTPServer\SMTPServer.csproj", "{ABB6A3E6-38B6-4D02-AC9C-91FA69CF03BE}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SMTPServer", "SMTPServer\SMTPServer.csproj", "{ABB6A3E6-38B6-4D02-AC9C-91FA69CF03BE}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D43013F8-933F-4ADC-8943-08E91662A070}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
22
SMTPServer/SMTPServer/Exceptions/PortUsedException.cs
Normal file
22
SMTPServer/SMTPServer/Exceptions/PortUsedException.cs
Normal 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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
64
SMTPServer/SMTPServer/PortListener.cs
Normal file
64
SMTPServer/SMTPServer/PortListener.cs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,9 +1,20 @@
|
|||||||
using System;
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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" />
|
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" />
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>netcoreapp1.0</TargetFramework>
|
<TargetFramework>netcoreapp1.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="**\*.cs" />
|
<Compile Include="**\*.cs" />
|
||||||
<EmbeddedResource Include="**\*.resx" />
|
<EmbeddedResource Include="**\*.resx" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="PortListener.cs" />
|
||||||
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.NETCore.App">
|
<PackageReference Include="Microsoft.NETCore.App">
|
||||||
<Version>1.0.1</Version>
|
<Version>1.1.0</Version>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
<PackageReference Include="Microsoft.NET.Sdk">
|
<PackageReference Include="Microsoft.NET.Sdk">
|
||||||
<Version>1.0.0-alpha-20161104-2</Version>
|
<Version>1.0.0-alpha-20161104-2</Version>
|
||||||
<PrivateAssets>All</PrivateAssets>
|
<PrivateAssets>All</PrivateAssets>
|
||||||
</PackageReference>
|
</PackageReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||||
</Project>
|
</Project>
|
16
SMTPServer/SMTPServer/StartTcpConnection.cs
Normal file
16
SMTPServer/SMTPServer/StartTcpConnection.cs
Normal 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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
64
SMTPServer/SMTPServer/TCPListener.cs
Normal file
64
SMTPServer/SMTPServer/TCPListener.cs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
SMTPServer/TCPListener.cs
Normal file
8
SMTPServer/TCPListener.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
public class Class1
|
||||||
|
{
|
||||||
|
public Class1()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user