From 45165b0192325867771acc0e27a443100b700b3e Mon Sep 17 00:00:00 2001 From: Zuhaitz Méndez Fernández de Aránguiz Date: Sat, 31 Jan 2026 22:35:26 +0000 Subject: Improved networking example --- examples/networking/echo_server.zc | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'examples/networking') diff --git a/examples/networking/echo_server.zc b/examples/networking/echo_server.zc index 2934923..64c60da 100644 --- a/examples/networking/echo_server.zc +++ b/examples/networking/echo_server.zc @@ -4,6 +4,7 @@ import "std/net.zc" def SIZE = 1024; fn main() { + raw { setbuf(stdout, NULL); } "Starting Echo Server on 127.0.0.1:8080..."; let listener_res = TcpListener::bind("127.0.0.1", 8080); @@ -13,24 +14,37 @@ fn main() { } let listener = listener_res.unwrap(); - defer listener.close(); loop { let client_res = listener.accept(); if client_res.is_ok() { let stream = client_res.unwrap(); - defer stream.close(); - let buf = (char*)malloc(SIZE); - defer free(buf); - let read_res = stream.read(buf, SIZE); + "New Connection!"; - if read_res.is_ok() { + let buf: char[SIZE]; + + while true { + let read_res = stream.read(&buf[0], SIZE); + + if read_res.is_err() { + !"Read error: {read_res.err}"; + break; + } + let bytes = read_res.unwrap(); - if bytes > 0 { - stream.write(buf, bytes); - "Echoed {bytes} bytes."; + if bytes == 0 { + "Client disconnected."; + break; } + + let write_res = stream.write(&buf[0], bytes); + if write_res.is_err() { + !"Write error: {write_res.err}"; + break; + } + + "Echoed {bytes} bytes."; } } } -- cgit v1.2.3 From aafd7e8739b14dd89b2e81148f2b07710f3c2c42 Mon Sep 17 00:00:00 2001 From: Zuhaitz Méndez Fernández de Aránguiz Date: Sat, 31 Jan 2026 22:53:52 +0000 Subject: Automatic versioning --- README.md | 2 +- README_ES.md | 2 +- README_IT.md | 2 +- README_ZH_CN.md | 2 +- README_ZH_TW.md | 2 +- examples/networking/echo_server.zc | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) (limited to 'examples/networking') diff --git a/README.md b/README.md index 786e32c..2f0832f 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ [![Build Status](https://img.shields.io/badge/build-passing-brightgreen)]() [![License](https://img.shields.io/badge/license-MIT-blue)]() -[![Version](https://img.shields.io/badge/version-0.1.0-orange)]() +[![Version](https://img.shields.io/github/v/release/z-libs/Zen-C?label=version&color=orange)]() [![Platform](https://img.shields.io/badge/platform-linux-lightgrey)]() *Write like a high-level language, run like C.* diff --git a/README_ES.md b/README_ES.md index 4f1e4be..2feb762 100644 --- a/README_ES.md +++ b/README_ES.md @@ -13,7 +13,7 @@ [![Estado de la Construcción](https://img.shields.io/badge/build-passing-brightgreen)]() [![Licencia](https://img.shields.io/badge/license-MIT-blue)]() -[![Versión](https://img.shields.io/badge/version-0.1.0-orange)]() +[![Versión](https://img.shields.io/github/v/release/z-libs/Zen-C?label=version&color=orange)]() [![Plataforma](https://img.shields.io/badge/platform-linux-lightgrey)]() *Escribe como un lenguaje de alto nivel, ejecuta como C.* diff --git a/README_IT.md b/README_IT.md index 7d14165..ba49e46 100644 --- a/README_IT.md +++ b/README_IT.md @@ -13,7 +13,7 @@ [![Stato Build](https://img.shields.io/badge/build-passing-brightgreen)]() [![Licenza](https://img.shields.io/badge/license-MIT-blue)]() -[![Versione](https://img.shields.io/badge/version-0.1.0-orange)]() +[![Versione](https://img.shields.io/github/v/release/z-libs/Zen-C?label=version&color=orange)]() [![Piattaforma](https://img.shields.io/badge/platform-linux-lightgrey)]() *Comodità di un linguaggio ad alto livello, veloce come il C* diff --git a/README_ZH_CN.md b/README_ZH_CN.md index 09b9bc3..796eb66 100644 --- a/README_ZH_CN.md +++ b/README_ZH_CN.md @@ -13,7 +13,7 @@ [![构建状态](https://img.shields.io/badge/build-passing-brightgreen)]() [![许可证](https://img.shields.io/badge/license-MIT-blue)]() -[![版本](https://img.shields.io/badge/version-0.1.0-orange)]() +[![版本](https://img.shields.io/github/v/release/z-libs/Zen-C?label=version&color=orange)]() [![平台](https://img.shields.io/badge/platform-linux-lightgrey)]() *像高级语言一样编写,像 C 一样运行。* diff --git a/README_ZH_TW.md b/README_ZH_TW.md index b0a98b8..5b4d484 100644 --- a/README_ZH_TW.md +++ b/README_ZH_TW.md @@ -13,7 +13,7 @@ [![構建狀態](https://img.shields.io/badge/build-passing-brightgreen)]() [![許可證](https://img.shields.io/badge/license-MIT-blue)]() -[![版本](https://img.shields.io/badge/version-0.1.0-orange)]() +[![版本](https://img.shields.io/github/v/release/z-libs/Zen-C?label=version&color=orange)]() [![平台](https://img.shields.io/badge/platform-linux-lightgrey)]() *像高級語言一樣編寫,像 C 一樣運行。* diff --git a/examples/networking/echo_server.zc b/examples/networking/echo_server.zc index 64c60da..82ecd82 100644 --- a/examples/networking/echo_server.zc +++ b/examples/networking/echo_server.zc @@ -4,7 +4,7 @@ import "std/net.zc" def SIZE = 1024; fn main() { - raw { setbuf(stdout, NULL); } + setbuf(stdout, NULL); "Starting Echo Server on 127.0.0.1:8080..."; let listener_res = TcpListener::bind("127.0.0.1", 8080); -- cgit v1.2.3