Velo by Example: TCP Sockets

The Socket module provides TCP socket communication for both client and server modes.

A TCP client connects to a remote host and exchanges data.

include "lang/socket.vel";

Socket sock = Socket();
sock.connect("127.0.0.1", 9876);

sock.writeLine("Hello!");
str reply = sock.readLine();

sock.close();

A TCP server binds to a port and accepts incoming connections.

include "lang/socket.vel";

Socket srv = Socket();
srv.bind(9876);

Socket client = srv.accept();
str msg = client.readLine();
client.writeLine("Echo: " + msg);

client.close();
srv.close();