feat: add Std::Net — TCP sockets module
- C runtime: socket(), bind(), listen(), accept(), connect(), send(), recv(), close() - library/std/Net.bux: Net_Create, Net_Bind, Net_Listen, Net_Accept, Net_Connect, Net_Send, Net_Recv, Net_Close, Net_SetReuse, Net_LastError - examples/tcp_server.bux: echo server on 127.0.0.1:9000 - examples/tcp_client.bux: client that sends 'Hello from Bux!' and prints reply
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
// TCP Client — connects to 127.0.0.1:9000, sends a message, prints the reply
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
import Std::Net::{Net_Create, Net_Connect, Net_Send, Net_Recv, Net_Close};
|
||||
|
||||
func Main() -> int {
|
||||
let fd: int = Net_Create();
|
||||
if fd < 0 {
|
||||
PrintLine("Failed to create socket");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if Net_Connect(fd, "127.0.0.1", 9000) == false {
|
||||
PrintLine("Failed to connect");
|
||||
Net_Close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
PrintLine("Connected to 127.0.0.1:9000");
|
||||
|
||||
let msg: String = "Hello from Bux!";
|
||||
let sent: int = Net_Send(fd, msg);
|
||||
if sent < 0 {
|
||||
PrintLine("Send failed");
|
||||
Net_Close(fd);
|
||||
return 1;
|
||||
}
|
||||
PrintLine("Message sent");
|
||||
|
||||
let reply: String = Net_Recv(fd, 1024);
|
||||
PrintLine("Server reply:");
|
||||
PrintLine(reply);
|
||||
|
||||
Net_Close(fd);
|
||||
PrintLine("Disconnected");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// TCP Echo Server — binds to 127.0.0.1:9000 and echoes back received data
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
import Std::Net::{Net_Create, Net_SetReuse, Net_Bind, Net_Listen, Net_Accept, Net_Send, Net_Recv, Net_Close};
|
||||
import Std::String::{String_Eq};
|
||||
|
||||
func Main() -> int {
|
||||
let fd: int = Net_Create();
|
||||
if fd < 0 {
|
||||
PrintLine("Failed to create socket");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if !Net_SetReuse(fd) {
|
||||
PrintLine("Warning: failed to set SO_REUSEADDR");
|
||||
}
|
||||
|
||||
if Net_Bind(fd, "127.0.0.1", 9000) == false {
|
||||
PrintLine("Failed to bind");
|
||||
Net_Close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if Net_Listen(fd, 5) == false {
|
||||
PrintLine("Failed to listen");
|
||||
Net_Close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
PrintLine("Echo server listening on 127.0.0.1:9000");
|
||||
|
||||
let client: int = Net_Accept(fd);
|
||||
if client < 0 {
|
||||
PrintLine("Failed to accept");
|
||||
Net_Close(fd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
PrintLine("Client connected");
|
||||
|
||||
var running: bool = true;
|
||||
while running {
|
||||
let msg: String = Net_Recv(client, 1024);
|
||||
if String_Eq(msg, "") {
|
||||
running = false;
|
||||
} else {
|
||||
PrintLine("Received:");
|
||||
PrintLine(msg);
|
||||
let sent: int = Net_Send(client, msg);
|
||||
if sent < 0 {
|
||||
PrintLine("Send failed");
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Net_Close(client);
|
||||
Net_Close(fd);
|
||||
PrintLine("Server shut down");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user