/// <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> /// 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; }
/// <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) {} }