143 lines
4.5 KiB
C#
143 lines
4.5 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.Threading;
|
|||
|
using System.Net;
|
|||
|
|
|||
|
namespace SafeMobileLib
|
|||
|
{
|
|||
|
public class TCPServer
|
|||
|
{
|
|||
|
private bool isRunning = false;
|
|||
|
private TcpListener tcpListener;
|
|||
|
private Thread listenThread;
|
|||
|
|
|||
|
public TCPServer()
|
|||
|
{
|
|||
|
Console.WriteLine("TCPServer Constructor");
|
|||
|
}
|
|||
|
|
|||
|
public void Start(int portNr)
|
|||
|
{
|
|||
|
isRunning = true;
|
|||
|
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()
|
|||
|
{
|
|||
|
isRunning = false;
|
|||
|
|
|||
|
if (tcpListener != null)
|
|||
|
{
|
|||
|
tcpListener.Stop();
|
|||
|
}
|
|||
|
if (listenThread.IsAlive)
|
|||
|
{
|
|||
|
listenThread.Abort();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void ListenForClients()
|
|||
|
{
|
|||
|
tcpListener.Start();
|
|||
|
|
|||
|
while (isRunning)
|
|||
|
{
|
|||
|
Console.WriteLine("Waiting for client....");
|
|||
|
//blocks until a client has connected to the server
|
|||
|
TcpClient client = tcpListener.AcceptTcpClient();
|
|||
|
Console.WriteLine("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 = "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 (isRunning)
|
|||
|
{
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
//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("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 void Send(byte[] data, int len, NetworkStream ns)
|
|||
|
{
|
|||
|
if (ns != null)
|
|||
|
{
|
|||
|
ns.Write(data, 0, len);
|
|||
|
ns.Flush();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|