/// <summary> /// Constructor for the TCP Listener(Server) Socket class :: Creates and starts the TCP listener /// </summary> /// <param name="myIPAddress">IP Address to host the listener on.</param> /// <param name="myPort">Port to host the listener on.</param> /// <param name="myMaxClients">Maximum number of clients allowed to connect.</param> /// <param name="myBufferRSize">Default read buffer size for every client.</param> /// <param name="myBufferWSize">Default write buffer size for every client.</param> /// <param name="myBufferAlign">Default buffer alignment for every client.</param> /// <param name="myHeader">Universal packet ID for received/sent packets.</param> public TcpListenerSocket(string myIPAddress, int myPort, int myMaxClients, int myBufferRSize, int myBufferWSize, int myBufferAlign, int myHeader) { // Attempt to process the code, if not successful, throw an exception. try { // Initiate the TCP socket ID and set status to online. SocketId = SocketSystem.BindSocket(); Status = true; // Setup server properties. MaxClients = myMaxClients; PacketHeader = ( byte )myHeader; AddressInfo = new IPEndPoint(IPAddress.Any /*IPAddress.Parse(IPAddress.Any)*/, myPort); // Setup default buffer sizes for incoming packets. BufferReadSize = myBufferRSize; BufferWriteSize = myBufferWSize; BufferAlignment = myBufferAlign; // Create a new TCP Socket class and setup the client list. ServerHandle = new TcpListener(AddressInfo); ClientList = new Dictionary <long, TcpClientSocket>(MaxClients); ServerHandle.Start(); // Call the TCP client connection accept, function is asynchronous, so TcpAccept() will still exit as expected. //System.Threading.Tasks.Task.Run( () => TcpAccept() ); ThreadPool.QueueUserWorkItem(myThread => TcpAccept()); Console.WriteLine("TCP Connection Listener Established (" + myIPAddress + " :: " + myPort + ")"); Console.WriteLine(" The server is set to have a maximum of " + MaxClients + " connections."); } catch (Exception e) { Console.WriteLine("ERROR: TCP SERVER FAILURE"); Console.WriteLine(e.Message); } }
/// <summary> /// Destructor for the TCP client socket class :: Frees up the client's memory and closes the connection to the client. /// </summary> public void TcpClientClose() { // When the client disconnects unbind it's socket ID and queue the socket ID for use again with another socket. SocketSystem.UnbindSocket(SocketId); DataStream.Dispose(); ClientHandle.Close(); }
/// <summary> /// Constructor for the TCP Client Socket class :: Sets up the client on the server. /// </summary> /// <param name="myBufferReadSize">Read buffer size for the client.</param> /// <param name="myBufferWriteSize">Write buffer size for the client.</param> /// <param name="myAlignment">Alignment(in bytes) for the client.</param> /// <param name="myPacketHeader">Server/client packet header ID.</param> public TcpClientSocket(int myBufferReadSize, int myBufferWriteSize, int myAlignment, byte myPacketHeader) { // Creates the socket ID, packet ID and read and write buffers upon constructing the class. ReadBuffer = new ByteBuffer(myBufferReadSize, myAlignment); WriteBuffer = new ByteBuffer(myBufferWriteSize, myAlignment); SocketId = SocketSystem.BindSocket(); HeaderId = myPacketHeader; DCTimer = new System.Timers.Timer(30000); DCTimer.Elapsed += ForceDisconnect; }
public int MaxClients = 0; // Maximum number of clients that can connect to the server. /// <summary> /// Destructor for the TCP Listener(Server) Socket class :: Handles freeing up any left over data in the class. /// </summary> public void TcpListenerClose() { // If the server was succesfully initiated and assigned a socket ID, free it's memory. // NOTE: The server has no memory created if it fails to bind to a socket. if (SocketId >= 0) { ServerHandle.Stop(); Status = false; SocketSystem.UnbindSocket(SocketId); foreach (KeyValuePair <long, TcpClientSocket> ClientSocket in ClientList) { ClientSocket.Value.ClientHandle.GetStream().Close(); ClientSocket.Value.ClientHandle.Close(); ClientSocket.Value.Connected = false; } } }
/// <summary> /// Constructor for the UDP Server Socket class :: Creates and starts the UDP client socket. /// </summary> /// <param name="myPort"></param> /// <param name="myBufferRSize"></param> /// <param name="myBufferWSize"></param> /// <param name="myBufferAlign"></param> /// <param name="myHeader"></param> public UdpServerSocket(int myPort, int myBufferRSize, int myBufferWSize, int myBufferAlign, int myHeader) { // Attempt to process the code, if not successful, throw an exception. try { // Initiate the UDP socket ID and set status to online. SocketId = SocketSystem.BindSocket(); Status = true; // Set the indicated port and header IDs. Port = myPort; HeaderId = ( byte )myHeader; // Create a new UDP Socket class and allow support for NAT Traversal. ServerHandle = new UdpClient(myPort); ServerHandle.AllowNatTraversal(true); // Create new read/write buffers of the indicated total byte size and byte alignment. ReadBuffer = new ByteBuffer(myBufferRSize, myBufferAlign); WriteBuffer = new ByteBuffer(myBufferWSize, myBufferAlign); // Call the UDP Handle, function is asynchronous, so UdpStart() will still exit as expected. ThreadPool.QueueUserWorkItem(myThread => UdpHandle()); } catch (Exception) {} }
public IPEndPoint SenderInfo; // The IP and Port Info of whomever sent data. /// <summary> /// Destructor for the UDP Server Socket class :: Unbinds the UDP socket ID and closes the socket. /// </summary> public void UdpServerClose() { SocketSystem.UnbindSocket(SocketId); ServerHandle.Close(); }