System.Net.Sockets.TcpClient.BeginConnect C# (CSharp) Method

BeginConnect() public method

public BeginConnect ( IPAddress address, int port, AsyncCallback requestCallback, object state ) : IAsyncResult
address IPAddress
port int
requestCallback AsyncCallback
state object
return IAsyncResult
        public IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback requestCallback, object state)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, address);

            IAsyncResult result = BeginConnectCore(address, port, requestCallback, state);

            if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
            return result;
        }

Same methods

TcpClient::BeginConnect ( string host, int port, AsyncCallback requestCallback, object state ) : IAsyncResult

Usage Example

Exemplo n.º 1
0
        public Boolean Connect(IPAddress hostAddr, Int32 hostPort, Int32 timeout)
        {
            // Create new instance of TCP client
            _Client = new TcpClient();
            var result = _Client.BeginConnect(hostAddr, hostPort, null, null);

            _TransmitThread = null;

            result.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(timeout));
            if (!_Client.Connected)
            {
                return false;
            }

            // We have connected
            _Client.EndConnect(result);
            EventHandler handler = OnConnected;
            if(handler != null)
            {
                handler(this, EventArgs.Empty);
            }

            // Now we are connected --> start async read operation.
            NetworkStream networkStream = _Client.GetStream();
            byte[] buffer = new byte[_Client.ReceiveBufferSize];
            networkStream.BeginRead(buffer, 0, buffer.Length, OnDataReceivedHandler, buffer);

            // Start thread to manage transmission of messages
            _TransmitThreadEnd = false;
            _TransmitThread = new Thread(TransmitThread);
            _TransmitThread.Start();

            return true;
        }
All Usage Examples Of System.Net.Sockets.TcpClient::BeginConnect