Skip to content
CShift0.32

TcpConnection

Struct · namespace System.Net · Since 0.30

struct TcpConnection : IDisposable

A TCP connection: bytes in both directions, to a server (TcpConnection.Connect) or from a client (TcpListener.Accept).

using var connection = try TcpConnection.Connect("example.com", 80);
try connection.WriteText("GET / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n");
var buffer = new uint8[4096];
while (true)
{
int got = try connection.Read(buffer, 0, buffer.Length);
if (got == 0)
break; // the server closed the connection
Console.Write(try Encoding.UTF8().GetString(buffer, 0, got));
}

Copies of a connection share it, and TcpConnection.Close of one closes it for all of them; keep one owner. The zero value (TcpConnection { }) is a closed connection. Only IPv4.

Name Description
Connect Connects to port of host: a name ("example.com", "localhost") or an IPv4 address ("192.168.1.10").
static NetError<TcpConnection> Connect(StringSlice host, int port)

Connects to port of host: a name ("example.com", "localhost") or an IPv4 address ("192.168.1.10").

Errors

Panics when port is not in 0..65535.

Name Description
IsOpen Whether the connection is open (not closed yet by TcpConnection.Close).
WaitForData Waits up to milliseconds until TcpConnection.Read can return without waiting: data arrived, or the other side closed the connection (0: does not wait; -1: waits as long as it takes).
Read Reads what has arrived, up to count bytes, into buffer[offset..]; waits until something arrives.
Write Sends all of bytes: an array, a part of one, or the bytes of a text (text.AsBytes()).
WriteText Sends text as UTF-8 bytes.
Close Closes the connection; the copies of it are closed as well.
Dispose Closes the connection (TcpConnection.Close); using calls it.
bool IsOpen()

Whether the connection is open (not closed yet by TcpConnection.Close). That the other side closed it shows when TcpConnection.Read returns 0.

bool WaitForData(int milliseconds)

Waits up to milliseconds until TcpConnection.Read can return without waiting: data arrived, or the other side closed the connection (0: does not wait; -1: waits as long as it takes).

Returns whether TcpConnection.Read would return at once; false when the time is up (or the connection is closed).

NetError<int> Read(uint8[] buffer, int offset, int count)

Reads what has arrived, up to count bytes, into buffer[offset..]; waits until something arrives.

Returns the number of bytes; 0 when the other side closed the connection and everything was read.

Errors

Panics when offset..offset + count is not inside of buffer.

NetError<void> Write(ReadOnlySlice<uint8> bytes)

Sends all of bytes: an array, a part of one, or the bytes of a text (text.AsBytes()).

Errors

NetError<void> WriteText(StringSlice text)

Sends text as UTF-8 bytes.

Errors

void Close()

Closes the connection; the copies of it are closed as well. Closing it again does nothing.

void Dispose()

Closes the connection (TcpConnection.Close); using calls it.