/// <summary> /// Initializes a new server which listens for TCP packets. /// </summary> /// <param name="ip">The IP address to use for the server.</param> /// <param name="port">The port on which the server runs.</param> /// <exception cref="ArgumentException"> /// IP is invalid or no IPv4 Address. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// Port is within an invalid range (0-1024). /// </exception> /// <exception cref="AccessViolationException"> /// Specified port is already in use. /// </exception> /// <exception cref="InvalidOperationException"> /// Server could not be started. /// </exception> public TinyTcpServer(string ip, int port) { int retVal = 0; IPAddress address; if (!(IPUtil.IsValid(ip, out address))) { throw new ArgumentException(SR.ERROR_IPINVALID, nameof(ip)); } retVal = InitServer(address, port); if (1 == retVal) { throw new ArgumentOutOfRangeException( nameof(port), port, SR.ERROR_WELLKNOWNPORT); } else if (2 == retVal) { throw new AccessViolationException(SR.ERROR_USEDPORT); } else if (3 == retVal) { throw new ArgumentException(SR.ERROR_INVALIDPARAM); } else if (4 <= retVal) { throw new InvalidOperationException(SR.ERROR_NOSTART); } }
/// <summary> /// Initializes a new server which listens for TCP packets. /// </summary> /// <param name="endpoint">IPEndPoint containg server address /// and port.</param> /// <exception cref="ArgumentException"> /// IP is invalid or no IPv4 Address. /// </exception> /// <exception cref="ArgumentOutOfRangeException"> /// Port is within an invalid range (0-1024). /// </exception> /// <exception cref="AccessViolationException"> /// Specified port is already in use. /// </exception> /// <exception cref="InvalidOperationException"> /// Server could not be started. /// </exception> public TinyTcpServer(IPEndPoint endpoint) { int retVal = 0; IPAddress address; string ip = endpoint.Address.ToString(); if (!(IPUtil.IsValid(ip, out address))) { throw new ArgumentException( SR.ERROR_IPINVALID, nameof(endpoint)); } // Initialize server and port. retVal = InitServer(address, endpoint.Port); if (1 == retVal) { throw new ArgumentOutOfRangeException( nameof(endpoint), endpoint.Port, SR.ERROR_WELLKNOWNPORT); } else if (2 == retVal) { throw new AccessViolationException(SR.ERROR_USEDPORT); } else if (3 == retVal) { throw new ArgumentException(SR.ERROR_INVALIDPARAM); } else if (4 <= retVal) { throw new InvalidOperationException(SR.ERROR_NOSTART); } }
/// <summary> /// Initializes a new client instance for /// a client-server communication over TCP. /// </summary> /// <param name="serverIp">The IPv4 address of the server.</param> /// <param name="serverPort">The port on which the /// server ca be reached.</param> /// <exception cref="ArgumentException"> /// Invalid IPv4 address</exception> public TinyTcpClient(IPAddress serverIp, int serverPort) { IPAddress address; if (!(IPUtil.IsValid(serverIp.ToString(), out address))) { throw new ArgumentException( SR.ERROR_IPINVALID, nameof(serverIp)); } /* Check if IP is valid and IPv4. */ ServerAddress = address; ServerPort = serverPort; }