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_tcp_connect

create a TCP connection to server.

@mln_tcp_connect(host, service, timeout);

Input:

Return value:

mln_tcp_listen

Set up a TCP listen socket.

@mln_tcp_listen(host, service);

Input:

Return value:

mln_tcp_accept

Accept a TCP connection.

@mln_tcp_accept(listenfd, timeout);

Input:

Return value:

mln_tcp_send

Send ‘data’ via TCP.

@mln_tcp_send(sockfd, data);

Input:

Return value:

mln_tcp_recv

Receive data from TCP.

@mln_tcp_recv(sockfd, timeout);

Input:

Return value:

mln_tcp_close

Close a TCP connection.

@mln_tcp_close(sockfd);

Input:

Return value:

mln_tcp_shutdown

Shutdown TCP connection.

@mln_tcp_shutdown(sockfd, mode);

Input:

Return value:

mln_udp_create

Create an UDP socket.

@mln_udp_create(host, service);

Input:

Return value:

mln_udp_close

Close an UDP socket.

@mln_udp_close(fd);

Input:

Return value:

mln_udp_send

Send data via UDP.

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

Input:

Return value:

mln_udp_recv

Receive data from UDP.

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

Input:

Return value:

Example
//client.mln
fd = @mln_tcp_connect('127.0.0.1', '1234');
@mln_print(fd);
@mln_tcp_send(fd, 'Hello');
@mln_print(@mln_tcp_recv(fd));
@mln_tcp_close(fd);
//server.mln
listenfd = @mln_tcp_listen('127.0.0.1', '1234');
connfd = @mln_tcp_accept(listenfd);
@mln_print(@mln_tcp_recv(connfd));
@mln_tcp_send(connfd, 'Hi');
@mln_tcp_close(connfd);
$ melang server.mln client.mln

The output is:

10
Hello
Hi