1

Luasocket の Web サイトにある例を使って試してみました。私の目標は、ソケットと通信するフラッシュ ゲームを作成することでした。

サーバーを実行し、最初にtelnetを使用して接続しましたが、機能しました。送信したすべてのメッセージがコンソールに表示されたので、次のステップに進み、AS 3を介して接続しましたが、接続しましたが、サーバーは受信しません私は常に write() を行っていますが、どんなメッセージでも構いません。

actionscript アプリケーションが lua ソケット サーバーと通信できないようにするために不足しているものはありますか?

コード

-- load namespace
local socket = require("socket")
-- create a TCP socket and bind it to the local host, at any port
local server = assert(socket.bind("*", 0))
-- find out which port the OS chose for us
local ip, port = server:getsockname()
-- print a message informing what's up
print("Please telnet to localhost on port " .. port)
print("After connecting, you have 10s to enter a line to be echoed")
-- loop forever waiting for clients
while 1 do
  -- wait for a connection from any client
  local client = server:accept()
  -- make sure we don't block waiting for this client's line
  client:settimeout(10)
  -- receive the line
  local line, err = client:receive()
  -- if there was no error, send it back to the client
  if not err then client:send(line .. "\n") end
  -- done with client, close the object
  client:close()
end

アクションスクリプト:

var sock:Socket = new Socket();
sock.connect("127.0.0.1",3335);
stage.addEventListener(Event.ENTER_FRAME,test);
public function test(e:Event):void{
    sock.writeUTF("Hello world");
}
4

1 に答える 1

1

client:receive() メソッドの標準操作モードは "*l" で、入力ストリームの改行文字が返されるのを待ちます。http://w3.impa.br/~diego/software/luasocket/tcp.html#receive

これを修正するには、"Hello world\n" を送信するか (これが actionscript の正しいエスケープ文字であると仮定します)、または receive() で別のパラメーターを使用します。

于 2013-11-21T19:19:32.043 に答える