Example #1
0
        /// <summary>
        /// Init TCP server, start listen task on argPort at the same time.
        /// </summary>
        /// <param name="argPort"></param>
        /// <param name="argListenDelegate"></param>
        public void InitTcpServer(int argPort, ListenTaskDelegate argListenDelegate)
        {
            if (_hostName == null || _hostIpAddress == null)
            {
                InitHost();
            }
            _hostServerEndPoint       = null;
            _hostTcpServer            = null;
            _hostServerEndPoint       = new IPEndPoint(_hostIpAddress, argPort);
            _hostTcpServer            = new TcpListener(_hostServerEndPoint);
            _tcpServerReceiveDelegate = argListenDelegate;

            _hostTcpServer.Start(MaxClient);
            TcpServerStartListenTask();
        }
Example #2
0
        /// <summary>
        /// Init TCP client and connect to a server, occupy a port at the same time.
        /// </summary>
        /// <param name="argRemoteIpEndPoint"></param>
        /// <param name="argListenDelegate"></param>
        /// <param name="argPort"></param>
        public void InitTcpClient(IPEndPoint argRemoteIpEndPoint, ListenTaskDelegate argListenDelegate, int argPort)
        {
            if (_hostName == null || _hostIpAddress == null)
            {
                InitHost();
            }
            _hostClientEndPoint = null;
            _hostClientEndPoint = new IPEndPoint(_hostIpAddress, argPort);
            _hostTcpClient      = null;
            _hostTcpClient      = new TcpClient(_hostClientEndPoint);

            _hostTcpClient.Connect(argRemoteIpEndPoint);

            _tcpClientReceiveDelegate = argListenDelegate;
            TcpClientStartListenTask();
        }
Example #3
0
        /// <summary>
        /// Init local UDP socket
        /// </summary>
        /// <param name="argPort"></param>
        /// <param name="argListenTask"></param>
        public void InitUdp(int argPort, ListenTaskDelegate argListenTask)
        {
            if (_hostName == null || _hostIpAddress == null)
            {
                InitHost();
            }

            _hostIpEndPoint  = new IPEndPoint(_hostIpAddress, argPort);
            _hostUdpClient   = new UdpClient(_hostIpEndPoint);
            _receiveDelegate = argListenTask;

            //Add listen thread
            Thread listenThread = new Thread(Listen)
            {
                IsBackground = true
            };

            listenThread.Start();
        }
Example #4
0
        /// <summary>
        /// Init TCP client, this method use a random port.
        /// </summary>
        /// <param name="argRemoteIpEndPoint"></param>
        /// <param name="argListenDelegate"></param>
        public void InitTcpClient(IPEndPoint argRemoteIpEndPoint, ListenTaskDelegate argListenDelegate)
        {
            Random randomGenerator = new Random();

            while (true)
            {
                int port = randomGenerator.Next(10000, 65535);
                try
                {
                    InitTcpClient(argRemoteIpEndPoint, argListenDelegate, port);
                    break;
                }
                catch (SocketException e)
                {
                    if (e.SocketErrorCode == SocketError.ConnectionRefused)
                    {
                        throw;
                    }
                }
            }
        }