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:
host
- a string value. It can be an IP or domain name.service
- a string value. It can be a port number or protocol name, like: http
, 80
.timeout
- a millisecond timer. It’s an optional argument. It must be an positive integer or zero (timeout immediately). Omit means never timeout.Return value:
nil
on timed outfalse
returnedSet up a TCP listen socket.
net.tcp_listen(host, service);
Input:
host
- a string value. It can be an IP or domain name.service
- a string value. It can be a port number or protocol name, like: http
, 80
.Return value:
false
returned.Accept a TCP connection.
net.tcp_accept(listenfd, timeout);
Input:
listened
- an integer value generated by tcp_listen
.timeout
- a millisecond timer. It’s an optional argument. It should be an positive integer or zero (timeout immediately). Omit means never timeout.Return value:
timeout >= 0
and no TCP arrived, nil
will be returned.false
returned.Send data via TCP.
net.tcp_send(sockfd, data);
Input:
socked
- a socket file descriptor which is an integer.data
- a string value.Return value:
true
on success, otherwise false
returned.Receive data from TCP.
net.tcp_recv(sockfd, timeout);
Input:
sockfd
- a socket file descriptor which is an integer.timeout
- a millisecond timer. It’s an optional argument. It must be an positive integer or zero (timeout immediately). Omit means never timeout.Return value:
timeout >= 0
and no data arrived, nil
will be returned.true
will be returned.false
returned.Close a TCP connection.
net.tcp_close(sockfd);
Input:
sockfd
- a socket file descriptor which is an integer.Return value:
nil
.Shutdown TCP connection.
net.tcp_shutdown(sockfd, mode);
Input:
sockfd
- a socket file descriptor which is an integer.mode
- a string and only has two values: send
and recv
.Return value:
nil
.Create an UDP socket.
net.udp_create(host, service);
Input:
host
- a string value. It can be an IP or domain name.service
- a string value. It can be a port number or protocol name, like: http
, 80
.Return value:
false
returned.Close an UDP socket.
net.udp_close(fd);
Input:
fd
- an UDP socket file descriptor generated by udp_create
.Return value:
nil
.Send data via UDP.
net.udp_send(fd, data, host, service);
Input:
fd
. - an UDP socket file descriptor generated by udp_create
.data
- a string value.host
- a string value. It can be an IP or domain name. This is the destination host.service
- a string value. It can be a port number or protocol name, like: http
, 80
. This is the destination service name.Return value:
true
will be returned on success, otherwise false
returned.Receive data from UDP.
net.udp_recv(fd, bufsize, &ip, &port, timeout);
Input:
fd
- an UDP socket file descriptor generated by udp_create
.bufsize
- an integer indicates the UDP receiving buffer size.ip
- sender’s IP. This is an output argument.port
- sender’s port. This is an output argument.timeout
- a millisecond timer. It’s an optional argument. It should be an positive integer or zero (timeout immediately). Omit means never timeout.Return value:
timeout >= 0
and no data arrived, nil
will be returned.false
returned.//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