BanglaCode provides comprehensive networking capabilities for building servers and clients using TCP, UDP, and WebSocket protocols - as easy to use as JavaScript/Node.js.
TCP (Transmission Control Protocol)
TCP provides reliable, ordered, and error-checked delivery of data. Perfect for applications that need guaranteed message delivery like chat servers, file transfers, and API clients.
TCP Server
Use tcp_server_chalu(port, handler) to create a TCP server. The handler function receives a connection object for each client connection.
tcp_server.bangbanglacode
1
// TCP Echo Server
2
dekho("Starting TCP server on port 8080...");
3
4
kaj handleConnection(conn) {
5
dekho("Client connected:", conn["remote_addr"]);
6
7
// Echo back the received data
8
dhoro data = conn["data"];
9
tcp_pathao(conn, "Echo: "+ data);
10
11
dekho("Sent response");
12
}
13
14
tcp_server_chalu(8080, handleConnection);
15
dekho("Server running!");
16
17
// Keep server alive
18
jotokkhon (sotti) {
19
ghumaao(1000);
20
}
TCP Client
Use tcp_jukto(host, port) to connect to a TCP server. This function is async and returns a promise.
tcp_client.bangbanglacode
1
proyash kaj tcpClient() {
2
dekho("Connecting to TCP server...");
3
4
// Connect to server (async)
5
dhoro conn = opekha tcp_jukto("localhost", 8080);
6
dekho("Connected!");
7
8
// Send message
9
tcp_lekho(conn, "Hello from BanglaCode!");
10
11
// Read response (async)
12
dhoro response = opekha tcp_shuno(conn);
13
dekho("Server replied:", response);
14
15
// Close connection
16
tcp_bondho(conn);
17
}
18
19
tcpClient();
TCP Functions
Function
Parameters
Description
tcp_server_chalu
port, handler
Start TCP server with callback for each connection
tcp_jukto
host, port
Connect to TCP server (async, returns promise)
tcp_pathao
connection, data
Send data on TCP connection
tcp_lekho
connection, data
Write data to TCP connection (alias for tcp_pathao)
tcp_shuno
connection
Read data from TCP connection (async, returns promise)
tcp_bondho
connection
Close TCP connection
UDP (User Datagram Protocol)
UDP is a connectionless protocol that's faster but doesn't guarantee delivery. Perfect for real-time applications like gaming, video streaming, and IoT where speed matters more than perfect reliability.
Listen for UDP packets (alias for udp_server_chalu)
udp_bondho
connection
Close UDP connection
WebSocket
WebSocket provides full-duplex communication channels over a single TCP connection. Perfect for real-time applications like chat, live updates, and collaborative tools.
WebSocket Server
websocket_chat.bangbanglacode
1
// WebSocket Chat Server
2
dekho("Starting WebSocket chat server on port 3000...");
3
4
bishwo clients = [];
5
6
kaj handleWebSocket(conn) {
7
dekho("Client connected:", conn["remote_addr"]);
8
9
// Add to clients list
10
dhokao(clients, conn);
11
12
// Broadcast messages to all clients
13
dhoro message = conn["message"];
14
ghuriye (dhoro i =0; i <dorghyo(clients); i = i +1) {