final class · System.Net

TcpClient

public final class TcpClient

One TCP connection.

Kind
final class
Namespace
System.Net
Assembly
System.Core
Access
public, final
Source file
Net.ant

Inheritance

Base classes
Any TcpClient

Constructors

new(i64 handle)
#

Properties

NetworkStream Stream { get; }
#

read-only

Methods

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()
#

Private and internal members (2)

NetworkStream stream
#

private

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

private static

Inherited members (5)

Source

TcpClient as Antril, decompiled from its 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