tcp

package
v0.1.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2021 License: BSD-3-Clause Imports: 4 Imported by: 0

README

tcp GoDoc

Usage

local tcp = require("tcp")

-- http request
local conn, err = tcp.open("google.com:80")
err = conn:write("GET /\n\n")
if err then error(err) end
local result, err = conn:read(64*1024)
print(result)

-- ping pong game
local conn, err = tcp.open(":12345")
if err then error(err) end

err = conn:write("ping")
if err then error(err) end

local result, err = conn:read()
if err then error(err) end
if (result == "pong") then error("must be pong message") end

Documentation

Overview

Package tcp implements tcp client lib for lua.

Example (Full)

tcp.open(), tcp_client_ud:write(), tcp_client_ud:read()

state := lua.NewState()
Preload(state)
go runPingPongServer(":12346")
time.Sleep(time.Second)
source := `
        local tcp = require("tcp")

        local conn, err = tcp.open(":12346")
        if err then error(err) end

        -- send ping, read "pong\n"
        local err = conn:write("ping")
        if err then error(err) end
        local result, err = conn:read()
        if err then error(err) end
        print(result)

        -- send ping, read by byte
        local err = conn:write("ping")
        if err then error(err) end
        for i = 1, 5 do
            local result, err = conn:read(1)
            if err then error(err) end
            print(result)
        end

        conn:close()
`
if err := state.DoString(source); err != nil {
	log.Fatal(err.Error())
}
Output:

pong

p
o
n
g

Index

Examples

Constants

View Source
const (
	// timeout for dial
	DefaultDialTimeout = 5 * time.Second
	// timeout for write
	DefaultWriteTimeout = time.Second
	// timeout for read
	DefaultReadTimeout = time.Second
	// timeout for close
	DefaultCloseTimeout = time.Second
)

Variables

This section is empty.

Functions

func Close

func Close(L *lua.LState) int

Close lua tcp_client_ud:close()

func Loader

func Loader(L *lua.LState) int

Loader is the module loader function.

func Open

func Open(L *lua.LState) int

Open lua tcp.open(string) returns (tcp_client_ud, err)

func Preload

func Preload(L *lua.LState)

Preload adds tcp to the given Lua state's package.preload table. After it has been preloaded, it can be loaded using require:

local tcp = require("tcp")

func Read

func Read(L *lua.LState) int

Read lua tcp_client_ud:read(max_size_int) returns (string, err)

func Write

func Write(L *lua.LState) int

Write lua tcp_client_ud:write() returns err

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL