Advanced Examples
Chat Server
Section titled “Chat Server”A simple TCP chat server that echoes messages back to the client.
print("Starting server...");
// Create server socket listening on 127.0.0.1:8080int sockfd = socket_server("127.0.0.1", 8080);if (sockfd == -1) { print("Failed to start server"); return;}
print("Server listening on 127.0.0.1:8080");
// Accept a connectionint client_sock = socket_accept(sockfd);if (client_sock == -1) { print("Accept failed"); socket_close(sockfd); return;}
print("Client connected");
// Receive messagestr msg = socket_receive(client_sock, 1024);print("Received: " + msg);
// Send responsesocket_send(client_sock, "Hello from Tulpar Server!");
// Close connectionssocket_close(client_sock);socket_close(sockfd);print("Server closed");Database Management
Section titled “Database Management”An example of creating a table, inserting data, and querying it using SQLite.
print("Opening database...");int db = db_open("query_test.db");
if (db != 0) { print("Creating table..."); db_query(db, "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT);");
print("Inserting data..."); db_query(db, "INSERT INTO users (name) VALUES ('Tulpar');");
print("Querying data..."); array results = db_query(db, "SELECT * FROM users;");
print("Results:"); print(results);
print("Closing database..."); db_close(db);}File Operations
Section titled “File Operations”Reading, writing, and modifying files.
str filename = "test_file.txt";
// Write to filewrite_file(filename, "Merhaba Tulpar!\nBu bir test dosyasıdır.");
// Check if existsif (file_exists(filename)) { // Read content str content = read_file(filename); print("Content:", content);
// Append data append_file(filename, "\nNew line added.");}