final class · System.Net

TcpClient

public final class TcpClient

One TCP connection.

Tipo
final class
Espacio de nombres
System.Net
Ensamblado
System.Core
Acceso
public, final
Archivo fuente
Net.ant

Herencia

Clases base
Any TcpClient

Constructores

new(i64 handle)
#

Propiedades

NetworkStream Stream { get; }
#

read-only

Métodos

static Connect(str host, i32 port) : TcpClient
#

static

Connects to host (a name or an address) at port.

Write(str text)
#

Writes the text's UTF-8 bytes.

Read(i32 max) : str
#

Reads what has arrived, up to max bytes, as text (empty when the other end closed).

Close()
#

Miembros privados e internos (2)

NetworkStream stream
#

private

static extern("net.connect") ConnectTo(u16[] host, i64 port) : i64
#

private static

Miembros heredados (5)

Código fuente

TcpClient en Antril, descompilado a partir de su Larva HIR.

System.Net.TcpClient.ant
// System.Net.TcpClient — System.Core (decompiled from Larva HIR)

import System;
import System.IO;

into namespace System.Net
{
	/// One TCP connection.
	public final class TcpClient
	{
		NetworkStream stream;

		static extern("net.connect") ConnectTo(u16[] host, i64 port) : i64;

		public:
			new(i64 handle)
			{
				stream = new NetworkStream(handle);
			}

			/// Connects to `host` (a name or an address) at `port`.
			static Connect(str host, i32 port) : TcpClient
			{
				i64 handle = ConnectTo(Unicode.ToUtf16(host), port);
				if (handle < 0)
					throw new IOError("cannot connect to " + host + ":" + port);
				return new TcpClient(handle);
			}

			/// Writes the text's UTF-8 bytes.
			Write(str text)
			{
				u8[] bytes = text.Units();
				stream.Write(bytes, 0, bytes.Length);
			}

			/// Reads what has arrived, up to `max` bytes, as text (empty when the other end closed).
			Read(i32 max) : str
			{
				u8[] buffer = new u8[max];
				i32 n = stream.Read(buffer, 0, max);
				if (n <= 0)
					return "";
				u8[] got = new u8[n];
				Array.Copy(buffer, 0, got, 0, n);
				return new str(got);
			}

			Close()
			{
				stream.Close();
			}

			NetworkStream Stream
			{
				get => stream;
			}
	}
}
Esc