This repository has been archived by the owner on Jan 3, 2018. It is now read-only.
Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
couv/examples/tcp-echo-server.lua
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
35 lines (28 sloc)
719 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local uv = require 'couv' | |
| local TEST_PORT = 9123 | |
| coroutine.wrap(function() | |
| local handle = uv.Tcp.new() | |
| handle:bind(uv.SockAddrV4.new('0.0.0.0', TEST_PORT)) | |
| handle:listen(128, function(server) | |
| coroutine.wrap(function() | |
| local stream = uv.Tcp.new() | |
| server:accept(stream) | |
| stream:startRead() | |
| local nread, buf | |
| repeat | |
| nread, buf = stream:read() | |
| if nread and nread > 0 then | |
| local msg = buf:toString(1, nread) | |
| if msg == 'QUIT' then | |
| server:close() | |
| break | |
| end | |
| stream:write({msg}) | |
| end | |
| until nread and nread == 0 | |
| stream:stopRead() | |
| stream:close() | |
| end)() | |
| end) | |
| end)() | |
| uv.run() |