Melang

Logo

A script language of preemptive 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.

mln_tcpConnect

create a TCP connection to server.

@mln_tcpConnect(host, service, timeout);

Input:

Return value:

mln_tcpListen

Set up a TCP listen socket.

@mln_tcpListen(host, service); 

Input:

Return value:

mln_tcpAccept

Accept a TCP connection.

@mln_tcpAccept(listenfd, timeout); 

Input:

Return value:

mln_tcpSend

Send ‘data’ via TCP.

@mln_tcpSend(sockfd, data); 

Input:

Return value:

mln_tcpRecv

Receive data from TCP.

@mln_tcpRecv(sockfd, timeout);  

Input:

Return value:

mln_tcpClose

Close a TCP connection.

@mln_tcpClose(sockfd);

Input:

Return value:

mln_tcpShutdown

Shutdown TCP connection.

@mln_tcpShutdown(sockfd, mode); 

Input:

Return value:

mln_udpCreate

Create an UDP socket.

@mln_udpCreate(host, service);

Input:

Return value:

mln_udpClose

Close an UDP socket.

@mln_udpClose(fd);

Input:

Return value:

mln_udpSend

Send data via UDP.

@mln_udpSend(fd, data, host, service); 

Input:

Return value:

mln_udpRecv

Receive data from UDP.

@mln_udpRecv(fd, bufsize, &ip, &port, timeout);

Input:

Return value:

Example
//client.mln
fd = @mln_tcpConnect('127.0.0.1', '1234');
@mln_print(fd);
@mln_tcpSend(fd, 'Hello');
@mln_print(@mln_tcpRecv(fd));
@mln_tcpClose(fd);
//server.mln
listenfd = @mln_tcpListen('127.0.0.1', '1234');
connfd = @mln_tcpAccept(listenfd);
@mln_print(@mln_tcpRecv(connfd));
@mln_tcpSend(connfd, 'Hi');
@mln_tcpClose(connfd);
$ melang server.mln client.mln

The output is:

10
Hello
Hi