C# Multi Thread TcpServer
public class TcpServer
{
private static TcpServer _instance;
public static TcpServer Instance { get { return _instance; } }
private TcpListener m_TcpListener;
private int _nPort = 13000;
private string _sIPAddress;
private List<DeviceTcpClient> m_Clients;
private Thread m_ListenThread;
public TcpServer()
{
if (_instance == null)
{
_instance = this;
}
this.m_Clients = new List<DeviceTcpClient>();
}
public void ServerStart()
{
m_ListenThread = new Thread(StartServer);
m_ListenThread.Start();
}
private void StartServer()
{
try
{
this.m_TcpListener = new TcpListener(IPAddress.Any, this._nPort);
this.m_TcpListener.Start();
Console.WriteLine("TV server has started.");
while (true)
{
TcpClient client = this.m_TcpListener.AcceptTcpClient();
NetworkStream clientStream = client.GetStream();
byte[] message = new byte[client.ReceiveBufferSize];
int byteRead;
byteRead = clientStream.Read(message, 0, client.ReceiveBufferSize);
string receiveMessage = System.Text.ASCIIEncoding.GetEncoding("euc-kr").GetString(message, 0, byteRead);
DeviceTcpClient deviceClient = new DeviceTcpClient { Client = client, DeviceID = receiveMessage };
this.m_Clients.Add(deviceClient);
//Console.WriteLine(String.Format("TcpClient Count - {0}", this.m_Clients.Count));
Console.WriteLine(String.Format("[{0}] is logged in. Connected device count is {1}.", receiveMessage, this.m_Clients.Count));
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClient));
clientThread.Start(client);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
this.m_TcpListener.Stop();
Console.WriteLine("TV server has ended.");
}
}
private void HandleClient(object c)
{
TcpClient client = c as TcpClient;
NetworkStream clientStream = client.GetStream();
byte[] message = new byte[client.ReceiveBufferSize];
int byteRead;
while (true)
{
byteRead = 0;
try
{
byteRead = clientStream.Read(message, 0, client.ReceiveBufferSize);
}
catch
{
break;
}
if (byteRead == 0)
{
break;
}
string receiveMessage = System.Text.ASCIIEncoding.GetEncoding("euc-kr").GetString(message, 0, byteRead);
Console.WriteLine(receiveMessage);
}
client.Close();
DeviceTcpClient deleteItem = null;
foreach (var item in m_Clients)
{
if (item.Client == client)
{
deleteItem = item;
break;
}
}
if (deleteItem != null)
{
Console.WriteLine(String.Format("[{0}] is logged out. Connected device count is {1}.", deleteItem.DeviceID, this.m_Clients.Count-1));
m_Clients.Remove(deleteItem);
}
//Console.WriteLine(String.Format("TcpClient Count - {0}", this.m_Clients.Count));
}
public TcpClient FindDevice(string id)
{
TcpClient result = null;
foreach (var item in m_Clients)
{
if (item.DeviceID == id)
{
result = item.Client;
break;
}
}
return result;
}
public void SendMessageToClient(TcpClient c, string msg)
{
try
{
TcpClient client = (TcpClient)c;
NetworkStream clientStream = client.GetStream();
byte[] buffer = System.Text.ASCIIEncoding.UTF8.GetBytes(msg);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
public void ServerClose()
{
if (m_Clients != null)
{
for (int i = 0; i < m_Clients.Count; i++)
{
TcpClient client = m_Clients[i].Client;
client.Close();
}
m_Clients.Clear();
}
if (m_TcpListener != null)
{
m_TcpListener.Stop();
}
if (m_ListenThread != null)
{
m_ListenThread.Abort();
}
}
}
'C# > Network' 카테고리의 다른 글
C# Http Post File Upload (0) | 2020.08.14 |
---|---|
C# TCP Client (0) | 2020.08.14 |
C# TCPServer with WebSocket (0) | 2020.08.13 |
C# UDP (0) | 2020.08.13 |
C# WOL 컴퓨터 원격 온오프 (0) | 2020.08.07 |