TcpConnection
Struct · namespace System.Net · Since 0.30
struct TcpConnection : IDisposableA 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.
Static methods
Section titled “Static methods”| Name | Description |
|---|---|
Connect |
Connects to port of host: a name ("example.com", "localhost") or an IPv4 address ("192.168.1.10"). |
Connect
Section titled “Connect”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
NetError.CannotResolve: the name is unknown (or there is no network).NetError.CannotConnect: nothing listens on the port, or the host cannot be reached.NetError.NotSupported: WebAssembly and AmigaOS.
Panics when port is not in 0..65535.
Methods
Section titled “Methods”| 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. |
IsOpen
Section titled “IsOpen”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.
WaitForData
Section titled “WaitForData”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
NetError.ConnectionLost: the connection is closed, or the other side reset it.
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.ConnectionLost: the connection is closed, or the other side closed or reset it.
WriteText
Section titled “WriteText”NetError<void> WriteText(StringSlice text)Sends text as UTF-8 bytes.
Errors
NetError.ConnectionLost: the connection is closed, or the other side closed or reset it.
void Close()Closes the connection; the copies of it are closed as well. Closing it again does nothing.
Dispose
Section titled “Dispose”void Dispose()Closes the connection (TcpConnection.Close); using calls it.