I wrote simple TCP Client Server application, I was wonder could you please review my code. I would be grateful for any hints and tips regarding to logic of app, coding style or naming convention.
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace TcpConnectionTest
{
class Program
{
private static void handleTcpConnectionEvents(object sender, TcpConnectionEventArgs args)
{
string s = null;
switch (args.EventType)
{
case TcpConnectionEventType.ServerIsListening:
s = "TCP Server listens ...";
break;
case TcpConnectionEventType.ConnectionIsEstablished:
s = "Connection has been established";
break;
case TcpConnectionEventType.ClienetIsDisconnected:
s = "Client has been disconnected";
break;
case TcpConnectionEventType.ServerStoppedListening:
s = "TCP Server stopped listenining";
break;
case TcpConnectionEventType.ByteReceived:
s = String.Format("{0:X2}", args.ByteReceived);
break;
}
Console.WriteLine(s);
}
static void Main(string[] args)
{
Console.WriteLine("Program run in Console mode!");
Console.WriteLine("Press ENTER to to exit ...");
try
{
TcpConnection.Instance.TcpConnectionEvent += new EventHandler<TcpConnectionEventArgs>(handleTcpConnectionEvents);
TcpConnection.Instance.Start(new TcpListener(new IPAddress(new byte[] { 127, 0, 0, 1 }), 3000));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
while (true)
{
ConsoleKeyInfo info = Console.ReadKey();
if (info.Key == ConsoleKey.Enter)
{
TcpConnection.Instance.Stop();
Console.WriteLine("Good Bye!");
Thread.Sleep(2000);
break;
}
}
}
}
}
TcpConnection.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
namespace TcpConnectionTest
{
public enum TcpConnectionEventType
{
ServerIsListening,
ConnectionIsEstablished,
ClienetIsDisconnected,
ServerStoppedListening,
ByteReceived,
}
public class TcpConnectionEventArgs : EventArgs
{
public TcpConnectionEventArgs(TcpConnectionEventType type)
{
EventType = type;
}
public TcpConnectionEventArgs(byte b)
{
EventType = TcpConnectionEventType.ByteReceived;
ByteReceived = b;
}
public TcpConnectionEventType EventType { get; set; }
public byte ByteReceived { get; set; }
}
public class TcpConnection
{
private static TcpConnection instance = null;
private volatile bool _reqStopListen = true;
private static object syncLock = new object();
private readonly object _stopListenLock = new object();
private TcpListener _tcpListener = new TcpListener(new IPAddress(new byte[] { 127, 0, 0, 1 }), 3000);
private TcpClient _tcpClient = null;
private Thread _tcpSlaveThread = null;
TcpListener Listener
{
get { return _tcpListener; }
set { _tcpListener = value; }
}
TcpClient Client
{
get { return _tcpClient; }
set { _tcpClient = value; }
}
Thread TcpSlaveThread
{
get { return _tcpSlaveThread; }
set { _tcpSlaveThread = value; }
}
private bool ReqStopListen
{
get
{
lock (_stopListenLock)
{
return _reqStopListen;
}
}
set
{
lock (_stopListenLock)
{
_reqStopListen = value;
}
}
}
private TcpConnection()
{
}
public static TcpConnection Instance
{
get
{
lock(syncLock)
{
if (TcpConnection.instance == null)
{
TcpConnection.instance = new TcpConnection();
}
return TcpConnection.instance;
}
}
}
private void AcceptCompleted(IAsyncResult ar)
{
Socket socket = null;
try
{
socket = Listener.Server.EndAccept(ar);
Client = new TcpClient { Client = socket };
TcpSlaveThread = new Thread(ThreadTcpSlave);
TcpSlaveThread.Start();
if (TcpConnectionEvent != null)
{
TcpConnectionEvent(this, new TcpConnectionEventArgs(TcpConnectionEventType.ConnectionIsEstablished));
}
}
catch (Exception e)
{
Debug.Print(e.Message);
}
}
public EventHandler<TcpConnectionEventArgs> TcpConnectionEvent;
static void ThreadTcpSlave()
{
while (Instance.ReqStopListen)
{
byte[] data = new byte[1];
try
{
if (Instance.Client.Client.Poll(50, SelectMode.SelectRead))
{
Instance.Client.Client.Receive(data, 0, 1, SocketFlags.None);
if (Instance.TcpConnectionEvent != null)
{
Instance.TcpConnectionEvent(Instance, new TcpConnectionEventArgs(data[0]));
}
}
}
catch (Exception e)
{
Debug.Print(e.Message);
continue;
}
}
}
public void Start(TcpListener listener)
{
Listener = listener;
Listener.Start();
Listener.Server.BeginAccept(AcceptCompleted, Listener);
if (TcpConnectionEvent != null)
{
TcpConnectionEvent(this, new TcpConnectionEventArgs(TcpConnectionEventType.ServerIsListening));
}
}
public void Stop()
{
if (Listener != null)
{
Listener.Server.Close();
if (TcpConnectionEvent != null)
{
TcpConnectionEvent(this, new TcpConnectionEventArgs(TcpConnectionEventType.ServerStoppedListening));
}
Listener = null;
}
if (TcpSlaveThread != null && TcpSlaveThread.IsAlive)
{
ReqStopListen = false;
Instance.TcpSlaveThread.Join(new TimeSpan(0, 0, 0, 100));
}
}
}
}