Skynet基礎入門例子詳解(4)

服務端與客戶端的Socket通信2

在同一個目錄建立4個文件(config,main.lua,socket2.lua,client2.lua) 
config文件參考上一節

main.lua代碼:

local skynet = require "skynet"

-- 啓動服務(啓動函數)
skynet.start(function()
    -- 啓動函數裏調用Skynet API開發各種服務
    print("======Server start=======")

    skynet.newservice("socket2")
    skynet.exit()
end)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

socket2.lua代碼:

local skynet = require "skynet"
local socket = require "socket"

local function echo(id)
    -- 每當 accept 函數獲得一個新的 socket id 後,並不會立即收到這個 socket 上的數據。這是因爲,我們有時會希望把這個 socket 的操作權轉讓給別的服務去處理。
    -- 任何一個服務只有在調用 socket.start(id) 之後,纔可以收到這個 socket 上的數據。
    socket.start(id)

    while true do
        local str = socket.read(id)
        if str then
            print("client say:"..str)
            -- 把一個字符串置入正常的寫隊列,skynet 框架會在 socket 可寫時發送它。
            socket.write(id, str)
        else
            socket.close(id)
            return
        end
    end
end

skynet.start(function()
    print("==========Socket1 Start=========")
    -- 監聽一個端口,返回一個 id ,供 start 使用。
    local id = socket.listen("127.0.0.1", 8888)
    print("Listen socket :", "127.0.0.1", 8888)

    socket.start(id , function(id, addr)
            -- 接收到客戶端連接或發送消息()
            print("connect from " .. addr .. " " .. id)

            -- 處理接收到的消息
            echo(id)
        end)
    --可以爲自己註冊一個別名。(別名必須在 32 個字符以內)
    -- skynet.register "SOCKET2"
end)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

client2.lua代碼:

package.cpath = "luaclib/?.so"
package.path = "lualib/?.lua;myexample/e1/?.lua"

if _VERSION ~= "Lua 5.3" then
    error "Use lua 5.3"
end

local socket = require "clientsocket"

local fd = assert(socket.connect("127.0.0.1", 8888))

socket.send(fd, "Hello world")
while true do
    -- 接收服務器返回消息
    local str   = socket.recv(fd)
    if str~=nil and str~="" then
            print("server says: "..str)
            -- socket.close(fd)
            -- break;
    end

    -- 讀取用戶輸入消息
    local readstr = socket.readstdin()
    if readstr then
        if readstr == "quit" then
            socket.close(fd)
            break;
        else
            -- 把用戶輸入消息發送給服務器
            socket.send(fd, readstr)
        end
    else
        socket.usleep(100)
    end
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

運行服務端:

./skynet ./myexample/e1/config

運行客戶端:

./3rd/lua/lua ./myexample/e1/client2.lua

運行客戶端後,你可以在命令行輸入你想發送的字符。 
服務端接收到消息後會打印消息,並原樣發送給客戶端,客戶端收到消息也打印出來。

服務端

客戶端

通過以上例子,詳細大家已基本理解Skynet的Socket通信原理。 
下一節將講述通信協議的使用。

項目源碼:http://download.csdn.net/detail/uisoul/9789615

Socket API參考文檔:https://github.com/cloudwu/skynet/wiki/Socket

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章