python:

server.py

if __name__ == '__main__':
    import socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 8001))
    sock.listen(5)
    while True:
        connection,address = sock.accept()
        try:
            connection.settimeout(5)
            buf = connection.recv(1024)
            if buf == '1':
                connection.send('welcome to server!')
            else:
                connection.send('please go out!')
        except socket.timeout:
            print 'time out'
        connection.close()

client.py

if __name__ == '__main__':
    import socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('localhost', 8001))
    import time
    time.sleep(2)
    sock.send('1')
    print sock.recv(1024)
    sock.close()

lua:

server.lua

socket = require("socket");
host = host or "127.0.0.1";
port = port or "8383";
server = assert(socket.bind(host, port));
ack = "ack\n";
while 1 do
    print("server: waiting for client connection...");
    control = assert(server:accept());
    while 1 do 
        command,status = control:receive();
  if status == "closed" then break end
        print(command);
        control:send(ack);
    end
end

client.lua

local socket = require("socket")

host = "127.0.0.1"
port = 8383

--打开一个TCP连接
c = assert (socket.connect (host, port))

c:send ("GET \n")
while (true) do
 local s, status, partial = c:receive ()
 print(s)
 if status == "closed" then break end
 c:send ("GET \n")
end

c:close ()


Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐