Advanced

Networking (TCP, UDP, WebSocket)

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
2dekho("Starting TCP server on port 8080...");
3
4kaj 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
14tcp_server_chalu(8080, handleConnection);
15dekho("Server running!");
16
17// Keep server alive
18jotokkhon (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
1proyash 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
19tcpClient();

TCP Functions

FunctionParametersDescription
tcp_server_chaluport, handlerStart TCP server with callback for each connection
tcp_juktohost, portConnect to TCP server (async, returns promise)
tcp_pathaoconnection, dataSend data on TCP connection
tcp_lekhoconnection, dataWrite data to TCP connection (alias for tcp_pathao)
tcp_shunoconnectionRead data from TCP connection (async, returns promise)
tcp_bondhoconnectionClose 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.

UDP Server

udp_server.bangbanglacode
1// UDP Server
2dekho("Starting UDP server on port 9000...");
3
4kaj handlePacket(packet) {
5 dekho("Received from:", packet["remote_addr"]);
6 dekho("Data:", packet["data"]);
7
8 // Send response back to client
9 udp_uttor(packet, "Received: " + packet["data"]);
10}
11
12udp_server_chalu(9000, handlePacket);
13dekho("UDP server listening!");
14
15jotokkhon (sotti) {
16 ghumaao(1000);
17}

UDP Client

udp_client.bangbanglacode
1proyash kaj udpClient() {
2 dekho("Sending UDP packet...");
3
4 // Send UDP packet (async)
5 opekha udp_pathao("localhost", 9000, "Hello UDP!");
6
7 dekho("Packet sent!");
8}
9
10udpClient();

UDP Functions

FunctionParametersDescription
udp_server_chaluport, handlerStart UDP server with callback for each packet
udp_pathaohost, port, dataSend UDP packet (async, returns promise)
udp_uttorpacket, dataSend UDP response to client
udp_shunoport, handlerListen for UDP packets (alias for udp_server_chalu)
udp_bondhoconnectionClose 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
2dekho("Starting WebSocket chat server on port 3000...");
3
4bishwo clients = [];
5
6kaj 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) {
15 websocket_pathao(clients[i], "Broadcast: " + message);
16 }
17}
18
19websocket_server_chalu(3000, handleWebSocket);
20dekho("WebSocket server running on ws://localhost:3000");
21
22jotokkhon (sotti) {
23 ghumaao(1000);
24}

WebSocket Client

websocket_client.bangbanglacode
1proyash kaj wsClient() {
2 dekho("Connecting to WebSocket...");
3
4 // Connect to WebSocket server (async)
5 dhoro ws = opekha websocket_jukto("ws://localhost:3000");
6 dekho("Connected!");
7
8 // Send messages
9 websocket_pathao(ws, "Hello from BanglaCode!");
10 websocket_pathao(ws, "This is message 2");
11
12 // Wait a bit
13 ghumaao(2000);
14
15 // Close connection
16 websocket_bondho(ws);
17 dekho("Disconnected");
18}
19
20wsClient();

WebSocket Functions

FunctionParametersDescription
websocket_server_chaluport, handlerStart WebSocket server with callback for messages
websocket_juktourlConnect to WebSocket server (async, returns promise)
websocket_pathaoconnection, messageSend WebSocket message
websocket_bondhoconnectionClose WebSocket connection

Connection Objects

All networking functions use connection objects (maps) to represent connections. These objects contain useful information about the connection:

TCP Connection Object

1{
2 id: "tcp_conn_1", // Unique connection ID
3 host: "localhost", // Host (client only)
4 port: 8080, // Port (client only)
5 remote_addr: "127.0.0.1:12345", // Remote address
6 local_addr: "127.0.0.1:8080", // Local address
7 data: "received data" // Received data (server only)
8}

UDP Packet Object

1{
2 id: "udp_conn_1", // Unique packet ID
3 data: "packet data", // Packet data
4 remote_addr: "127.0.0.1:12345", // Sender address
5 local_addr: "127.0.0.1:9000" // Receiver address
6}

WebSocket Connection Object

1{
2 id: "ws_conn_1", // Unique connection ID
3 url: "ws://localhost:3000", // WebSocket URL (client only)
4 connected: sotti, // Connection status
5 remote_addr: "127.0.0.1:12345", // Remote address
6 local_addr: "127.0.0.1:3000", // Local address
7 message: "received message", // Received message (server only)
8 type: "text" // Message type (text/binary/close)
9}

Function Name Meanings

FunctionBengaliMeaning
chaluচালুstart/run
juktoযুক্তconnect/join
pathaoপাঠাওsend
lekhoলেখোwrite
shunoশোনোlisten/hear
bondhoবন্ধclose
uttorউত্তরreply/response

Best Practices

  • Use TCP when you need reliable, ordered delivery (HTTP APIs, file transfers, chat)
  • Use UDP when speed matters more than reliability (gaming, video streaming, IoT sensors)
  • Use WebSocket for real-time bidirectional communication (live chat, notifications, collaborative editing)
  • Always use proyash/opekha for async operations
  • Remember to close connections with tcp_bondho, udp_bondho, or websocket_bondho
  • Use error handling with chesta/dhoro_bhul for network operations

Complete Examples

Check out the examples/ folder in the BanglaCode repository for complete working examples:

  • tcp_server.bang - TCP echo server
  • tcp_client.bang - TCP client with async/await
  • udp_server.bang - UDP server with responses
  • udp_client.bang - UDP client
  • websocket_chat.bang - WebSocket chat server
  • websocket_client.bang - WebSocket client

Next Steps

Learn more about related topics: