Exemple #1
0
        public void StartClient(string serverIp, int port)
        {
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse(serverIp), port);

            _listener.Blocking = false;
            _listener.BeginConnect(serverEndPoint, new AsyncCallback(OnConnect), _listener);
        }
Exemple #2
0
        private bool Connect(ISocket socket, string hostname, UInt16 port)
        {
            System.Net.IPEndPoint ipEnd = Helpers.GetEndpoint(hostname, port);
            if (ipEnd == null)
            {
                return(false);
            }

            try
            {
                IAsyncResult result     = socket.BeginConnect(ipEnd, null, null);
                bool         waitResult = result.AsyncWaitHandle.WaitOne(2000);

                if (waitResult && socket.Connected)
                {
                    _socket = socket;

                    BeginReceive();

                    return(true);
                }
            }
            catch (SocketException ex)
            {
                Diagnostics.Trace("Exception during Connect: {0}", ex.Message);
                Close();
            }

            return(false);
        }
Exemple #3
0
        public bool Connect(string host, int port)
        {
            if (State == ConnectionState.Closed)
            {
                try
                {
                    connectEvent.Reset();
                    IAsyncResult ar = socket.BeginConnect(host, port, new AsyncCallback(ConnectCallback), socket);

                    if (connectEvent.WaitOne(ConnectTimeout, true))
                    {
                        // Success
                        socket.EndConnect(ar);
                        Logger.GetLog().Debug("接続しました: " + host + ":" + port);

                        BeginReceive();
                        return(true);
                    }
                    else
                    {
                        // Fail
                        Logger.GetLog().Debug("タイムアウト: " + host + ":" + port);
                        return(false);
                    }
                }
                catch (SocketException se)
                {
                    Logger.GetLog().Info("接続エラー: " + host + ":" + port, se);

                    return(false);
                }
            }
            else
            {
                Logger.GetLog().Debug("接続要求を受け入れ可能な状態ではありません。");

                return(false);
            }
        }