0

Im making a lua script running at my pc, and need to connect from my android. So in the lua script I just put this:

local socket = require 'socket'
local server = socket.tcp()
print("createdserver")
server:bind('*',7070)
print("binded")
while 1 do
  server:listen(32)

  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

And at android I got a really simple socket connection:

public Connection(String ip, int _port) {
    //inMessage = new NetworkMessage();
    buffer = new byte[16394];
    try {
        serverAddres = InetAddress.getByName(ip);
        socket = new Socket(serverAddres, _port);
        socketOut = socket.getOutputStream();
    } catch (IOException e) {
        Log.e("Connect", e.getMessage());
    }
}

At android it just stays at "new Socket" and dont connect.

Caio Keto
  • 1,919
  • 4
  • 20
  • 30
  • On Lua side you should call `listen` only once. So move `server:listen(32)` out from loop. But in your code first connection should work. Try connect to server using `telnet`. – moteus Apr 08 '15 at 14:13

2 Answers2

0

Im not familiar with lua but my understanding is that you are writing a new line to the socket and you want to receive on the Android side. Normally if that is the case you need to get the inputStream not the output one since you are waiting for results. Furthermore you need to indefinitely (or till some conditions are met) to listen to the input stream for data on a separate thread (a standard in):

while(true){
    if (inputStreamReader().read() != -1){
          // do you processing
     }
}
eldjon
  • 2,800
  • 2
  • 20
  • 23
0

My notebook was changing its IP address so I couldnt reach it from the android, already solved!

Caio Keto
  • 1,919
  • 4
  • 20
  • 30