138 lines
4.5 KiB
C#
138 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Net.Sockets;
|
|
using System.Net;
|
|
using System.Threading;
|
|
|
|
namespace Simulator
|
|
{
|
|
public class TCPServer
|
|
{
|
|
private TcpListener tcpListener;
|
|
private Thread listenThread;
|
|
|
|
public TCPServer()
|
|
{
|
|
Console.WriteLine("TCPServer Constructor");
|
|
}
|
|
|
|
public void Start(int portNr)
|
|
{
|
|
Console.WriteLine("TCPServer Started on port " + portNr);
|
|
tcpListener = new TcpListener(IPAddress.Any, portNr);
|
|
listenThread = new Thread(new ThreadStart(ListenForClients));
|
|
listenThread.IsBackground = true;
|
|
listenThread.Start();
|
|
Console.WriteLine("===================================================");
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
if (tcpListener != null)
|
|
{
|
|
tcpListener.Stop();
|
|
}
|
|
if (listenThread.IsAlive)
|
|
{
|
|
listenThread.Abort();
|
|
}
|
|
}
|
|
|
|
private void ListenForClients()
|
|
{
|
|
tcpListener.Start();
|
|
|
|
while (true)
|
|
{
|
|
Console.WriteLine(" Debug Waiting for client....");
|
|
//blocks until a client has connected to the server
|
|
TcpClient client = tcpListener.AcceptTcpClient();
|
|
Console.WriteLine("Debug Client connected" + ((IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
|
|
//create a thread to handle communication
|
|
//with connected client
|
|
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
|
|
clientThread.IsBackground = true;
|
|
clientThread.Start(client);
|
|
}
|
|
}
|
|
|
|
private void HandleClientComm(object client)
|
|
{
|
|
TcpClient tcpClient = (TcpClient)client;
|
|
NetworkStream clientStream = tcpClient.GetStream();
|
|
|
|
string clientIPAddress = "Debug Client Ip Address is: " + IPAddress.Parse(((
|
|
IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString());
|
|
if (this.OnConnectionRecv != null)
|
|
{
|
|
this.OnConnectionRecv(clientStream, clientIPAddress);
|
|
}
|
|
byte[] message = new byte[4096];
|
|
int bytesRead;
|
|
|
|
while (true)
|
|
{
|
|
bytesRead = 0;
|
|
try
|
|
{
|
|
//blocks until client sends a message
|
|
bytesRead = clientStream.Read(message, 0, 4096);
|
|
}
|
|
catch
|
|
{
|
|
//a socket error has occured
|
|
break;
|
|
}
|
|
|
|
if (bytesRead == 0)
|
|
{
|
|
//the client has disconnected from the server
|
|
break;
|
|
}
|
|
//Console.WriteLine("Message received");
|
|
//message has successfully been received
|
|
//ASCIIEncoding encoder = new ASCIIEncoding();
|
|
//System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
|
|
if (this.OnMessageRecv != null)
|
|
{
|
|
this.OnMessageRecv(message, bytesRead, clientStream);
|
|
}
|
|
if (this.OnMessageRecvWithIdent != null)
|
|
{
|
|
this.OnMessageRecvWithIdent(message, bytesRead, clientIPAddress);
|
|
}
|
|
|
|
}
|
|
|
|
tcpClient.Close();
|
|
if (this.OnConnectionEnded != null)
|
|
{
|
|
this.OnConnectionEnded(clientIPAddress);
|
|
}
|
|
Console.WriteLine(" Debug Client disconnected");
|
|
}
|
|
|
|
public delegate void MessageRecv(byte[] data, int recv, NetworkStream ns);
|
|
public event MessageRecv OnMessageRecv;
|
|
|
|
public delegate void MessageRecvWithIdent(byte[] data, int recv, string ip);
|
|
public event MessageRecvWithIdent OnMessageRecvWithIdent;
|
|
|
|
public delegate void ConnectionRecv(NetworkStream ns, string IP);
|
|
public event ConnectionRecv OnConnectionRecv;
|
|
|
|
public delegate void ConnectionEnded(string IP);
|
|
public event ConnectionEnded OnConnectionEnded;
|
|
|
|
public static void Send(byte[] data, int len, NetworkStream ns)
|
|
{
|
|
if (ns != null)
|
|
{
|
|
ns.Write(data, 0, len);
|
|
ns.Flush();
|
|
}
|
|
}
|
|
}
|
|
}
|