A script language of time-sharing scheduling coroutine in single thread
This document introduces a set of functions about TCP and UDP in Melang.
net = Import('net');
create a TCP connection to server.
net.tcp_connect(host, service, timeout);
Input:
Return value:
Set up a TCP listen socket.
net.tcp_listen(host, service);
Input:
Return value:
Accept a TCP connection.
net.tcp_accept(listenfd, timeout);
Input:
Return value:
Send ‘data’ via TCP.
net.tcp_send(sockfd, data);
Input:
Return value:
Receive data from TCP.
net.tcp_recv(sockfd, timeout);
Input:
Return value:
Close a TCP connection.
net.tcp_close(sockfd);
Input:
Return value:
Shutdown TCP connection.
net.tcp_shutdown(sockfd, mode);
Input:
Return value:
Create an UDP socket.
net.udp_create(host, service);
Input:
Return value:
Close an UDP socket.
net.udp_close(fd);
Input:
Return value:
Send data via UDP.
net.udp_send(fd, data, host, service);
Input:
Return value:
Receive data from UDP.
net.udp_recv(fd, bufsize, &ip, &port, timeout);
Input:
Return value:
//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