About
Networking in the Mys programming language.
Examples
TCP client
from net.tcp.client import Client
func main():
client = Client()
client.connect("localhost", 5858)
print("Connected!")
client.write(b"Hi!")
data = client.read(3)
print(f"Got: {data}")
TCP server
from net.tcp.server import Server
func main():
server = Server()
server.listen(5858)
client = server.accept()
print("Connected!")
data = client.read(3)
print(f"Got: {data}")
client.write(data)
API
TCP client
class Client(Reader):
func __init__(self):
func is_connected(self) -> bool:
Returns true if conencted to the server, false otherwise.
func connect(self, host: string, port: i64):
Connect to a server using given `host` and `port`. Reconnects
if already connected.
func disconnect(self):
Disconnect from the server.
func write(self, data: bytes):
Write data to the server. Never blocks. Raises an error if disconnected.
func read(self, size: i64) -> bytes:
Read data from the server. Always returns size number of bytes,
unless the connection was closed, in which case the remaining
data is returned.
TCP server
class Client(Reader):
func __init__(self):
func is_connected(self) -> bool:
Returns true if conencted to the server, false otherwise.
func disconnect(self):
Disconnect from the server.
func write(self, data: bytes):
Write given data to the server. Never blocks. Raises an error if
disconnected.
func read(self, size: i64) -> bytes:
Read data from the server. Always returns size number of bytes,
unless the connection was closed, in which case the remaining
data is returned.
func try_read_into(self, data: bytes, offset: i64, size: i64) -> i64:
Try to read data from the server into given buffer. Returns number
of read bytes, which is at least one and at most given size
bytes, unless the connection was closed, in which case the
remaining number of bytes is returned.
class Server:
A TCP server, listening for clients to connect.
func __init__(self):
func listen(self, port: i64):
Start listening for clients to connect to given `port` on any
interface.
func accept(self) -> Client:
Wait for a client to connect and return it.
UDP socket
Warning
UDP sockets are not yet implemented!
class Socket:
func __init__(self):
func bind(self, port: i64):
Bind the socket to given local port. Used both when sending and
receiving packtes.
func connect(self, host: string, port: i64):
Connect the socket to given remote host and port. Sent packages are
sent to this address by default.
func send(self, data: bytes):
func send_to(self, data: bytes, host: string, port: i64):
Send given data to given address.
func receive(self) -> bytes:
func receive_from(self) -> (bytes, string, i64):
Secure TCP client
Warning
Not yet implemented!
class Client(Reader):
A secure TCP client using SSL/TLS.
func __init__(self, context: SecureContext = SecureContext()):
func is_connected(self) -> bool:
Returns true if conencted to the server, false otherwise.
func connect(self, host: string, port: i64):
Connect to a server using given `host` and `port`.
func disconnect(self):
Disconnect from the server.
func write(self, data: bytes):
Write data to the server.
func read(self, size: i64) -> bytes:
Read data from the server. Always returns size number of bytes,
unless the connection was closed, in which case the remaining
data is returned.