System.Net.Sockets.TcpClient.Connect C# (CSharp) Méthode

Connect() public méthode

public Connect ( IPAddress address, int port ) : void
address IPAddress
port int
Résultat void
        public void Connect(IPAddress address, int port)
        {
            if (NetEventSource.IsEnabled) NetEventSource.Enter(this, address);
            if (_cleanedUp)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }
            if (!TcpValidationHelpers.ValidatePortNumber(port))
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }

            IPEndPoint remoteEP = new IPEndPoint(address, port);
            Connect(remoteEP);

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

Same methods

TcpClient::Connect ( IPEndPoint remoteEP ) : void
TcpClient::Connect ( string hostname, int port ) : void

Usage Example

		public void WorkerProcessMain(string argument, string passwordBase64)
		{
			int port = int.Parse(argument, CultureInfo.InvariantCulture);
			
			client = new TcpClient();
			client.Connect(new IPEndPoint(IPAddress.Loopback, port));
			Stream stream = client.GetStream();
			receiver = new PacketReceiver();
			sender = new PacketSender(stream);
			shutdownEvent = new ManualResetEvent(false);
			receiver.ConnectionLost += OnConnectionLost;
			receiver.PacketReceived += OnPacketReceived;
			sender.WriteFailed += OnConnectionLost;
			
			// send password
			sender.Send(Convert.FromBase64String(passwordBase64));
			
			receiver.StartReceive(stream);
			while (!shutdownEvent.WaitOne(SendKeepAliveInterval, false)) {
				Program.Log("Sending keep-alive packet");
				sender.Send(new byte[0]);
			}
			
			Program.Log("Closing client (end of WorkerProcessMain)");
			client.Close();
			shutdownEvent.Close();
		}
All Usage Examples Of System.Net.Sockets.TcpClient::Connect