TCP receive problem w. Java Server

Hi There!

I am developing a Java server to go along with an app, however i have run into the following problem:

I am using TCP to handle the client/server communication but for some reason, i am only able to send data to the Server but not receive a reply. 

This is an example of my code which is super simple but for some reason doesn’t work and I am far beyond the point of staring myself blind, trying to fix it. 

Lua:

function pushToServer(dataToSend)

    local tcp = socket.tcp()

    tcp:settimeout(2)

    tcp:connect(xxxx,  yyyyy);

    tcp:send(dataToSend);

    print(tcp:receive("*l"))

    tcp:close()

end

Java:

// Read incoming

Socket clientSocket = serverSocket.accept();

in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

String jsonString = in.readLine();

// Reply

out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));

out.write(stringToReturn);

I have debugged the code enough to know that the java code is first executed when the Lua client’s connection is closed, and I cant seem to figure out why. The Server succesfully reads the data from the Lua client, but first when the client closes the connection. Then the Server writes a reply but cant, because the connection is closed, and so the client receives NIL.

I am convinced that the problem is probably really simple and i am just making a newbie mistake so i hope some of you ninjas can help. 

Thanks in advance
Jens Finneurp

Hi,

This code is completely out of context as I just copied and pasted some of the relevant portions from an older project, but you basically need to make sure you’re continuously checking the TCP stream.

Again, this code won’t work as-is, it just to illustrate the behavior.

Hope it helps.

local socketlib = require( "socket" ) local json = require( "json" ) self.socket = socketlib.tcp() local success, msg = self.socket:connect( self.host, self.port ) if success then     if self.inLobby == nil then       self.inLobby = true     else       self.inLobby = false --in game     end     self.socket:settimeout( 0 )     self.socket:setoption( "tcp-nodelay", true )     self:start() end -- ... function Game:tick()     local input, \_ = socketlib.select( { self.socket }, nil, 0 )     for \_, client in ipairs( input ) do              local line, err, partial = client:receive('\*l')              if not err then                  local data = json.decode( line )       else         if err == "closed" then           --kill connection           self:stop()           self.socket:close()         end       end     end end function Game:start()   if not self.processTimer then     self.processTimer = timer.performWithDelay( 50, function() self:tick(); end, -1 )   end end  

Hi,

This code is completely out of context as I just copied and pasted some of the relevant portions from an older project, but you basically need to make sure you’re continuously checking the TCP stream.

Again, this code won’t work as-is, it just to illustrate the behavior.

Hope it helps.

local socketlib = require( "socket" ) local json = require( "json" ) self.socket = socketlib.tcp() local success, msg = self.socket:connect( self.host, self.port ) if success then     if self.inLobby == nil then       self.inLobby = true     else       self.inLobby = false --in game     end     self.socket:settimeout( 0 )     self.socket:setoption( "tcp-nodelay", true )     self:start() end -- ... function Game:tick()     local input, \_ = socketlib.select( { self.socket }, nil, 0 )     for \_, client in ipairs( input ) do              local line, err, partial = client:receive('\*l')              if not err then                  local data = json.decode( line )       else         if err == "closed" then           --kill connection           self:stop()           self.socket:close()         end       end     end end function Game:start()   if not self.processTimer then     self.processTimer = timer.performWithDelay( 50, function() self:tick(); end, -1 )   end end