2016-11-21 19:24:22 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Net;
|
2016-11-22 22:00:18 +00:00
|
|
|
|
using System.Net.Sockets;
|
2016-11-21 19:24:22 +00:00
|
|
|
|
using System.Text;
|
2016-11-22 22:00:18 +00:00
|
|
|
|
using System.Threading;
|
2016-11-21 19:24:22 +00:00
|
|
|
|
|
|
|
|
|
namespace SMTPServer
|
|
|
|
|
{
|
2016-11-22 22:00:18 +00:00
|
|
|
|
class StartTcpConnection : Socket
|
2016-11-21 19:24:22 +00:00
|
|
|
|
{
|
2016-11-26 11:11:27 +00:00
|
|
|
|
private List<string> Lines = new List<string>();
|
|
|
|
|
private int LinesAvailable { get
|
|
|
|
|
{
|
|
|
|
|
return Lines.Count;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string Others { get; set; }
|
|
|
|
|
private Encoding Encoding { get; set; }
|
2016-11-22 22:00:18 +00:00
|
|
|
|
|
|
|
|
|
public StartTcpConnection(int port, IPAddress destination, Encoding encoding) : base (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
|
|
|
|
|
{
|
|
|
|
|
this.Connect(new IPEndPoint(destination, port));
|
|
|
|
|
Thread readLines = new Thread(new ThreadStart(ReadClientInput));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReadClientInput()
|
2016-11-21 19:24:22 +00:00
|
|
|
|
{
|
2016-11-22 22:00:18 +00:00
|
|
|
|
while (true)
|
|
|
|
|
{
|
2016-11-26 11:11:27 +00:00
|
|
|
|
byte[] buffer = new byte[ReceiveBufferSize];
|
2016-11-22 22:00:18 +00:00
|
|
|
|
Receive(buffer);
|
|
|
|
|
|
2016-11-26 11:11:27 +00:00
|
|
|
|
var str = Encoding.GetString(buffer);
|
|
|
|
|
lock (Others)
|
2016-11-22 22:00:18 +00:00
|
|
|
|
{
|
2016-11-26 11:11:27 +00:00
|
|
|
|
Others += str;
|
2016-11-22 22:00:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CheckLines();
|
|
|
|
|
}
|
2016-11-21 19:24:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-22 22:00:18 +00:00
|
|
|
|
private void CheckLines()
|
|
|
|
|
{
|
2016-11-26 11:11:27 +00:00
|
|
|
|
lock (Others)
|
2016-11-22 22:00:18 +00:00
|
|
|
|
{
|
|
|
|
|
var line = "";
|
2016-11-26 11:11:27 +00:00
|
|
|
|
foreach (char c in Others)
|
2016-11-22 22:00:18 +00:00
|
|
|
|
{
|
|
|
|
|
if (c.Equals('\n'))
|
|
|
|
|
{
|
2016-11-26 11:11:27 +00:00
|
|
|
|
lock (Lines)
|
2016-11-22 22:00:18 +00:00
|
|
|
|
{
|
2016-11-26 11:11:27 +00:00
|
|
|
|
Lines.Add(line);
|
2016-11-22 22:00:18 +00:00
|
|
|
|
line = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else line += c;
|
|
|
|
|
}
|
2016-11-26 11:11:27 +00:00
|
|
|
|
Others = line;
|
2016-11-22 22:00:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetLine()
|
|
|
|
|
{
|
2016-11-26 11:11:27 +00:00
|
|
|
|
lock (Lines)
|
2016-11-22 22:00:18 +00:00
|
|
|
|
{
|
2016-11-26 11:11:27 +00:00
|
|
|
|
return Lines[0];
|
2016-11-22 22:00:18 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-21 19:24:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|