Melang

Logo

A script language of time-sharing scheduling coroutine in single thread

View the Project on GitHub Water-Melon/Melang

Network

This document introduces a set of functions about TCP and UDP in Melang.

Import

net = Import('net');
tcp_connect

create a TCP connection to server.

net.tcp_connect(host, service, timeout);

Input:

Return value:

tcp_listen

Set up a TCP listen socket.

net.tcp_listen(host, service);

Input:

Return value:

tcp_accept

Accept a TCP connection.

net.tcp_accept(listenfd, timeout);

Input:

Return value:

tcp_send

Send data via TCP.

net.tcp_send(sockfd, data);

Input:

Return value:

tcp_recv

Receive data from TCP.

net.tcp_recv(sockfd, timeout);

Input:

Return value:

tcp_close

Close a TCP connection.

net.tcp_close(sockfd);

Input:

Return value:

tcp_shutdown

Shutdown TCP connection.

net.tcp_shutdown(sockfd, mode);

Input:

Return value:

net.udp_create

Create an UDP socket.

net.udp_create(host, service);

Input:

Return value:

udp_close

Close an UDP socket.

net.udp_close(fd);

Input:

Return value:

udp_send

Send data via UDP.

net.udp_send(fd, data, host, service);

Input:

Return value:

udp_recv

Receive data from UDP.

net.udp_recv(fd, bufsize, &ip, &port, timeout);

Input:

Return value:

Example
//client.mln
net = Import('net');
sys = Import('sys');

fd = net.tcp_connect('127.0.0.1', '1234');
sys.print(fd);
net.tcp_send(fd, 'Hello');
sys.print(net.tcp_recv(fd));
net.tcp_close(fd);
//server.mln
net = Import('net');
sys = Import('sys');

listenfd = net.tcp_listen('127.0.0.1', '1234');
connfd = net.tcp_accept(listenfd);
sys.print(net.tcp_recv(connfd));
net.tcp_send(connfd, 'Hi');
net.tcp_close(connfd);
$ melang server.mln client.mln

The output is:

10
Hello
Hi