Skip to main content

Socket Library Reference

Socket programming is the foundation of network communication in virtually every modern application. A socket is an endpoint for sending or receiving data across a network. Under the hood, sockets abstract away the details of TCP/IP or UDP protocols so you can think in terms of “connect, send, receive, close.” Common socket types:

  • TCP (stream) sockets provide reliable, ordered, bidirectional byte streams—perfect for HTTP, SSH, and any protocol where you need guaranteed delivery.
  • UDP (datagram) sockets offer low‑overhead, connectionless message passing—ideal for DNS lookups, real‑time audio/video, or game state updates where speed matters more than reliability.
  • TLS/SSL sockets layer encryption on top of TCP for secure communication (HTTPS, secure WebSocket, etc.).

With sockets you can build everything from simple chat clients and file transfer tools to full‑blown HTTP servers and IoT device interfaces.


Why EasyBite Includes Sockets

EasyBite aims to empower beginners and hobbyists to build real‑world networked applications without wrestling with external libraries or complex setup. By embedding sockets directly into the core language:

  • Zero friction: No need to install or configure additional network modules—just import socket and go.
  • Uniform API: A consistent set of functions for TCP, UDP, and TLS that integrate naturally with EasyBite’s set, show, and repeat while constructs.
  • Rapid prototyping: Spin up clients and servers in minutes, test network protocols, or integrate with web services directly from your script.
  • Learning: Gain hands‑on understanding of networking concepts—connections, binding, listening, and encryption—using familiar syntax.

Benefits for EasyBite Users

  • Simplicity: One module covers all common network use cases—no switching between TCP‑only or UDP‑only libraries.
  • Flexibility: Write clients, servers, DNS utilities, or secure TLS channels with the same API.
  • Portability: Your network scripts run anywhere EasyBite runs—desktop, server, or embedded device—without modification.
  • Integration: Combine socket calls with file I/O, JSON handling, and control structures (repeat while) to build end‑to‑end applications.

Socket Library Reference

The socket library provides low‑level network primitives for TCP, UDP, and TLS. Import with:

import socket

Or selectively:

from socket import tcp_connect, udp_bind, tls_connect

Available Functions

CategoryFunctionParametersDescription
TCPtcp_connect(host, port)host (String), port (Number)Open a TCP connection to host:port, returning a TcpStream object.
tcp_send(stream, data)stream (TcpStream), data (String)Send data over the TCP connection, returning true on success.
tcp_receive(stream)stream (TcpStream)Receive up to 1024 bytes as a String, or null if closed.
tcp_close(stream)stream (TcpStream)Close the TCP connection.
tcp_bind(host, port)host (String), port (Number)Bind a TCP socket on host:port and store it globally.
tcp_listen(backlog)backlog (Number)Listen on the bound socket, returning a TcpListener.
tcp_listen_with_backlog(h,p,b)host, port, backlogBind and listen in one call.
tcp_accept()Accept next connection, returning { "connection": TcpStream, "client_addr": String }.
tcp_accept_by_addr(ip, port)ip (String), port (Number)Only accept a connection matching that client address.
UDPudp_bind(addr)addr (String)Bind a UDP socket on addr, returning a UdpSocket.
udp_send_to(sock, data, tgt)sock (UdpSocket), data (String), tgt (String)Send data to tgt, returning bytes sent.
udp_receive_from(sock)sock (UdpSocket)Receive up to 1024 bytes, returning { "message": String, "address": String }.
TLStls_connect(h, p, domain)host, port, domainOpen a TLS client connection, validating against domain.
tls_listen(h,p,pkcs12,pwd)host, port, pkcs12_path, passwordBind TLS listener with PKCS#12 cert, returning {listener, acceptor}.
tls_accept(info)info (Dict)Accept a TLS connection using {listener, acceptor}, returning a TlsStream.
tls_send(stream, data)stream (TlsStream), data (String)Send over TLS, returning true.
tls_receive(stream)stream (TlsStream)Receive over TLS up to 1024 bytes.
tls_close(stream)stream (TlsStream)Close the TLS connection.
Utilityset_nonblocking(sock, mode)sock (TcpStream/TcpListener/UdpSocket), mode (Bool)Enable or disable non‑blocking mode.
set_nonblocking_global(mode)mode (Bool)Same for the global TCP listener.
gethostbyname(hostname)hostname (String)Resolve to an array of IP address strings.
sendfile(stream, path)stream (TcpStream), path (String)Stream file contents over TCP.
receivefile(stream, path)stream (TcpStream), path (String)Receive from TCP and save to file.

Examples

All examples use repeat while(condition) and end repeat for looping.

import socket

1. Basic TCP Client

set s to socket.tcp_connect("example.com", 80)
set _ to socket.tcp_send(s, "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n")
set response to socket.tcp_receive(s)
show(response)
// Output:
// HTTP/1.0 200 OK
...
set _ to socket.tcp_close(s)

2. TCP Echo Server

// Bind and listen
set _ to socket.tcp_bind("0.0.0.0", 9000)
set listener to socket.tcp_listen(5)

// Loop to accept connections
repeat while(true)
set info to socket.tcp_accept()
// info["connection"], info["client_addr"]
set client to info["connection"]
// Echo until client closes
repeat while(true)
set msg to socket.tcp_receive(client)
if (msg is null) then
exit
end if
set _ to socket.tcp_send(client, "Echo: " + msg)
end repeat
set _ to socket.tcp_close(client)
end repeat

3. UDP Broadcast Listener

set sock to socket.udp_bind("0.0.0.0:8000")
repeat while(true)
set data to socket.udp_receive_from(sock)
show("Got from " + data["address"] + ": " + data["message"])
end repeat

4. Secure TLS Client

set tls to socket.tls_connect("example.com", 443, "example.com")
set _ to socket.tls_send(tls, "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
set reply to socket.tls_receive(tls)
show(reply)
set _ to socket.tls_close(tls)

5. DNS Lookup

set ips to socket.gethostbyname("localhost")
show(ips)
// Output:
// ["127.0.0.1", "::1"]

Conclusion

With the socket library you can build clients, servers, file‑transfer tools, and secure connections—all in EasyBite:

  • Import: import socketsocket.tcp_connect(...)
  • Selective: from socket import tcp_connect, udp_bind → call directly

Use these primitives plus repeat while(condition)...end repeat to create robust networked applications without external dependencies.