EDIT Notice, I needed to make the reverse changes of this https://github.com/akka/akka/commit/ce014ece3568938b2036c4ccfd21b92faba69607#L13L6 to make the accepted answer work with AKKA 2.1 which is the stable distribution found on akkas homepage!
I have read all the tutorials I could find on AKKA, but nothing I found works "out of box".
Using eclipse, I want to create 2 programs.
Program1:
starts actor "joe" and somehow makes it available on 127.0.0.1:some_port
Program2:
gets a reference to actor "joe" at 127.0.0.1:some_port. Sends a hello message to "joe".
Program 1 should print something when the message is received. I want to run this example in eclipse using AKKA 2.1. Can someone list 2 programs, (program1 and program2) together with a working application.conf file that does this and nothing else?
edit>
let me show you what I got so far:
actor
case class Greeting(who: String) extends Serializable
class GreetingActor extends Actor with ActorLogging {
def receive = {
case Greeting(who) ⇒ println("Hello " + who);log.info("Hello " + who)
}
}
Program1:
package test
import akka.actor.ActorSystem
object Machine1 {
def main(args: Array[String]): Unit = {
val system = ActorSystem("MySystem")
}
}
Program2
package test
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.actorRef2Scala
object Machine2 {
def main(args: Array[String]): Unit = {
val system = ActorSystem("MySystem")
val greeter = system.actorOf(Props[GreetingActor], name = "greeter")
greeter ! Greeting("Felix")
}
}
application.conf
akka {
actor {
deployment {
/greeter {
remote = "akka://MySystem#127.0.0.1:2553"
}
}
}
}
However, this program works when I start only Program2 and it outputs:
Hello Felix
[INFO] [02/18/2013 12:27:29.999] [MySystem-akka.actor.default-dispatcher-2] [akka://MySystem/user/greeter] Hello Felix
It seems that it is not picking up my application.conf. I tried placing it both in the ./src/ and ./ folder of my eclipse project. No difference. Also, I know this is really demote deployment, but I need just a hello world program to work using AKKA. I spent so much time on this without getting a simple working application.
Update for Akka 2.2.3
A minimal remote application can be created as follows:
Create 2 Projects in Eclipse: Client and Server
Server:
The code for the server is
package server
import akka.actor.Actor
import akka.actor.ActorLogging
import akka.actor.ActorSystem
import akka.actor.Props
class Joe extends Actor {
def receive = {
case msg: String => println("joe received " + msg + " from " + sender)
case _ => println("Received unknown msg ")
}
}
object Server extends App {
val system = ActorSystem("GreetingSystem")
val joe = system.actorOf(Props[Joe], name = "joe")
println(joe.path)
joe ! "local msg!"
println("Server ready")
}
The applincation.conf for the server is
akka {
loglevel = "DEBUG"
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
port = 2552
}
log-sent-messages = on
log-received-messages = on
}
}
Client:
The Client-code is
package client
import akka.actor._
import akka.actor.ActorDSL._
object Greet_Sender extends App {
println("STARTING")
implicit val system = ActorSystem("GreetingSystem-1")
val joe = system.actorSelection("akka.tcp://GreetingSystem#127.0.0.1:2552/user/joe")
println("That 's Joe:" + joe)
val a = actor(new Act {
whenStarting { joe ! "Hello Joe from remote" }
})
joe ! "Hello"
println("Client has sent Hello to joe")
}
The client application.conf is:
akka {
#log-config-on-start = on
stdout-loglevel = "DEBUG"
loglevel = "DEBUG"
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
log-sent-messages = on
log-received-messages = on
netty.tcp {
hostname = "127.0.0.1"
port = 0
}
}
}
Configurations have to be placed in two files called application.conf, both within the bin directory of the two projects.
As korefn mentioned, the remote documentation explains it's workings in detail. It also links to an example application. That example should give you everything you need to get started.
Edit
To get the sample application running perform the following steps:
Clone from GitHub
eecolor#BLACK:~/GihHub$ git clone https://github.com/akka/akka.git
Go into the akka directory and run sbt
eecolor#BLACK:~/GihHub/akka$ sbt
Switch to the akka-sample-project
akka > project akka-sample-remote
Call run on the project and select the CalcApp
Multiple main classes detected, select one to run:
[1] sample.remote.calculator.java.JCreationApp
[2] sample.remote.calculator.LookupApp
[3] sample.remote.calculator.CalcApp
[4] sample.remote.calculator.java.JLookupApp
[5] sample.remote.calculator.CreationApp
[6] sample.remote.calculator.java.JCalcApp
Enter number: 3
[info] Running sample.remote.calculator.CalcApp
[INFO] [02/19/2013 19:22:09.055] [run-main] [Remoting] Starting remoting
[INFO] [02/19/2013 19:22:09.230] [run-main] [Remoting] Remoting started; listening on addresses :[akka.tcp://CalculatorApplication#127.0.0.1:2552]
Started Calculator Application - waiting for messages
Switch to another console and repeat the first few steps
eecolor#BLACK:~/GihHub/akka$ sbt
akka > project akka-sample-remote
Call run and select the LookupApp
akka-sample-remote > run
Multiple main classes detected, select one to run:
[1] sample.remote.calculator.java.JCreationApp
[2] sample.remote.calculator.LookupApp
[3] sample.remote.calculator.CalcApp
[4] sample.remote.calculator.java.JLookupApp
[5] sample.remote.calculator.CreationApp
[6] sample.remote.calculator.java.JCalcApp
Enter number: 2
[info] Running sample.remote.calculator.LookupApp
[INFO] [02/19/2013 19:23:39.358] [run-main] [Remoting] Starting remoting
[INFO] [02/19/2013 19:23:39.564] [run-main] [Remoting] Remoting started; listening on addresses :[akka.tcp://LookupApplication#127.0.0.1:2553]
Started Lookup Application
Sub result: 14 - 16 = -2
Sub result: 13 - 22 = -9
Add result: 56 + 93 = 149
Add result: 18 + 19 = 37
Switch back to the other console and you should see something like this:
Calculating 14 - 16
Calculating 13 - 22
Calculating 56 + 93
Calculating 18 + 19
Well, in your example, the Client code never references the config file and it won't work.
akka by default will use the application.conf file - so it does not need to be explicitly selected.
if one does want to then the code would be (taking the code above as an exmaple):
val configFile = getClass.getClassLoader.getResource("akka_local_application.conf").getFile
val config = ConfigFactory.parseFile(new File(configFile))
val system = ActorSystem("GreetingSystem",config)
val joe = system.actorOf(Props[Joe], name = "joe")
Related
i am new for Akka, i am using Akka 2.3.3 version for creating actors. I am going to create remote actor and trying to access with client. Whenever i am going to run test-case, the following exception will throw:
[INFO] [04/27/2016 07:51:23.727] [Localsystem-akka.actor.default-dispatcher-3] [akka://Localsystem/deadLetters] Message [com.harmeetsingh13.chapter2.messages.SetRequest] from Actor[akka://Localsystem/temp/$a] to Actor[akka://Localsystem/deadLetters] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [04/27/2016 07:51:23.745] [Localsystem-akka.actor.default-dispatcher-3] [akka://Localsystem/deadLetters] Message [com.harmeetsingh13.chapter2.messages.GetRequest] from Actor[akka://Localsystem/temp/$b] to Actor[akka://Localsystem/deadLetters] was not delivered. [2] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
Futures timed out after [10 seconds]
java.util.concurrent.TimeoutException: Futures timed out after [10 seconds]
at scala.concurrent.impl.Promise$DefaultPromise.ready(Promise.scala:219)
at scala.concurrent.impl.Promise$DefaultPromise.result(Promise.scala:223)
at scala.concurrent.Await$$anonfun$result$1.apply(package.scala:190)
at scala.concurrent.BlockContext$DefaultBlockContext$.blockOn(BlockContext.scala:53)
at scala.concurrent.Await$.result(package.scala:190)
at com.harmeetsingh13.chapter2.SClientIntegrationSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(SClientIntegrationSpec.scala:18)
at com.harmeetsingh13.chapter2.SClientIntegrationSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(SClientIntegrationSpec.scala:15)
at com.harmeetsingh13.chapter2.SClientIntegrationSpec$$anonfun$1$$anonfun$apply$mcV$sp$1.apply(SClientIntegrationSpec.scala:15)
at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
at org.scalatest.Transformer.apply(Transformer.scala:22)
at org.scalatest.Transformer.apply(Transformer.scala:20)
at org.scalatest.FunSpecLike$$anon$1.apply(FunSpecLike.scala:422)
at org.scalatest.Suite$class.withFixture(Suite.scala:1122)
at com.harmeetsingh13.chapter2.SClientIntegrationSpec.withFixture(SClientIntegrationSpec.scala:11)
at org.scalatest.FunSpecLike$class.invokeWithFixture$1(FunSpecLike.scala:419)
at org.scalatest.FunSpecLike$$anonfun$runTest$1.apply(FunSpecLike.scala:431)
at org.scalatest.FunSpecLike$$anonfun$runTest$1.apply(FunSpecLike.scala:431)
at org.scalatest.SuperEngine.runTestImpl(Engine.scala:306)
at org.scalatest.FunSpecLike$class.runTest(FunSpecLike.scala:431)
at com.harmeetsingh13.chapter2.SClientIntegrationSpec.runTest(SClientIntegrationSpec.scala:11)
at org.scalatest.FunSpecLike$$anonfun$runTests$1.apply(FunSpecLike.scala:464)
at org.scalatest.FunSpecLike$$anonfun$runTests$1.apply(FunSpecLike.scala:464)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:413)
at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:401)
............
My Server code as below:
Main.scala
object Main extends App{
private val configFile = getClass.getClassLoader.getResource("application.conf").getFile;
private val config = ConfigFactory.parseFile(new File(configFile ))
val system = ActorSystem("SimpleClientServer", config)
system.actorOf(Props[AkkadmeyDB], name = "akkademy-db")
}
application.conf:
akka{
actor{
provider = "akka.remote.RemoteActorRefProvider"
}
remote{
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
port = 2552
}
log-sent-messages = on
log-received-messages = on
}
}
AkkadmeyDB.scala Actor class:
class AkkadmeyDB extends Actor{
val map = new HashMap[String, Object]
val log = Logging(context.system, this)
override def receive: Receive = {
case SetRequest(key, value) =>
log.info("received SetRequest - key: {} value: {}", key, value)
map.put(key, value)
sender() ! Status.Success
case GetRequest(key) =>
log.info("received GetRequest - key: {}", key)
val response = map.get(key)
response match{
case Some(x) => sender() ! x
case None => Status.Failure(new KeyNotFoundException(key))
}
case o => Status.Failure(new ClassNotFoundException())
}
}
Client Code as below:
SClient.scala
class SClient(remoteIp: String) {
private implicit val timeout = Timeout(10 seconds)
private implicit val system = ActorSystem("Localsystem")
private val remoteAddress = s"akka.tcp://SimpleClientServer#$remoteIp/user/akkademy-db";
private val remoteDb = system.actorSelection(remoteAddress)
def set(key: String, value: Object) = {
remoteDb ? SetRequest(key, value)
}
def get(key: String) = {
remoteDb ? GetRequest(key)
}
}
SClientIntegrationSpec.scala Test case:
class SClientIntegrationSpec extends FunSpecLike with Matchers {
val client = new SClient("127.0.0.1:2552")
describe("akkadment-db-client"){
it("should set a value"){
client.set("jame", new Integer(1313))
val futureResult = client.get("james")
val result = Await.result(futureResult, 10 seconds)
result should equal (1313)
}
}
}
When i see the logs of my remote application, this seems like, the request hit doesn't go to the server. what is the problem in my sample code running?
For solving above problem, we need to follow two steps that are metnion below:
When i am creating a server code, i am excluding application.conf from my server application, that's why, client application not able to connect with server. The code are using in built.sbt is as below:
mappings in (Compile, packageBin) ~= { _.filterNot { case (_, name) =>
Seq("application.conf").contains(name)
}}
After commenting above code, the client see the server successfully.
In Learning Scala chapter 2 jasongoodwin explain the code of client and server actor system. But there are some Errata in book and missing application.conf configuration for client. Because when we run both code in same PC, we are facing already port bind exception because by default actors are using 2552 port for accessing and we already define this port for our server application. So, application.conf also need for client as below:
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
enabled-transports = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "127.0.0.1"
port = 0
}
log-sent-messages = on
log-received-messages = on
}
}
Here Port 0 means any free port.
After that, above code are running successfully.
There is an application.conf file in the client project as well which is not mentioned in the book.
Make sure you create that file under the resources folder with the following content:
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
}
See the official github repo
I wrote this code to create a remote actor
object Main extends App {
val system = ActorSystem("keyvalue")
system.actorOf(Props[KeyValueActor], name = "keyvalue-db")
}
class KeyValueActor extends Actor {
val map = new util.HashMap[String, Object]
val log = Logging(context.system, this)
override def receive = {
case SetRequest(key, value) => {
log.info(s"received set request key ${key} value ${value}")
map.put(key, value)
sender() ! Status.Success
}
case GetRequest(key) => log.info(s"recieved get request ${key}")
sender() ! KeyValue(map.get(key))
case _=> log.info("unknown message")
}
}
I started my server using activator run and this printed the message
[info] Running com.abhi.akka.Main
[INFO] [01/10/2016 20:21:52.461] [run-main-0] [Remoting] Starting remoting
[INFO] [01/10/2016 20:21:52.617] [run-main-0] [Remoting] Remoting started;
listening on addresses :[akka.tcp://keyvalue#127.0.0.1:2552]
[INFO] [01/10/2016 20:21:52.619] [run-main-0] [Remoting]
Remoting now listens on addresses: [akka.tcp://keyvalue#127.0.0.1:2552]
but now when i try to call my remote actor using this client code
object KeyValueClient {
def main(args: Array[String]) : Unit = {
implicit val system = ActorSystem("LocalFileSystem")
implicit val timeout = Timeout(2 seconds)
val keyValueActorRef = system.actorSelection("akka.tcp://keyvalue#127.0.0.1:2552/user/keyvalue-db")
keyValueActorRef ! SetRequest("foo", "bar")
(keyValueActorRef ? GetRequest("foo")).onSuccess({
case x : KeyValue => println(s"got value ${x.value}")
})
}
}
it throws an error message
[INFO] [01/10/2016 20:25:33.345] [LocalFileSystem-akka.actor.default-dispatcher-2] [akka://LocalFileSystem/deadLetters] Message [com.abhi.akka.messages.SetRequest] from Actor[akka://LocalFileSystem/deadLetters] to Actor[akka://LocalFileSystem/deadLetters] was not delivered. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
[INFO] [01/10/2016 20:25:33.346] [LocalFileSystem-akka.actor.default-dispatcher-2] [akka://LocalFileSystem/deadLetters] Message [com.abhi.akka.messages.GetRequest] from Actor[akka://LocalFileSystem/temp/$a] to Actor[akka://LocalFileSystem/deadLetters] was not delivered. [2] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
My Full code is available at
https://github.com/abhitechdojo/keyvaluedb.git
https://github.com/abhitechdojo/keyvalueclient.git
EDIT: I solved the problem based on the suggestion given by Mustafa Simov. The client side needed this configuration file
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
deployment {
/keyvalue-db {
remote = "akka.tcp://keyvalue#127.0.0.1:2552"
}
}
}
}
Then you create the actor using
val actor = system.actorOf(Props[KeyValueActor], "keyvalue-db")
I have looked at your client code and I think it is just a configuration problem. In order to use akka-remoting, you must configure your client too. So you must create an application.conf for client ActorSystem as you created for server.
Akka remoting is peer-to-peer and not really a good fit for client-server
Because of this both sides in remoting must be able to connect to the other side, as either of them can initiate communication. Both of your applications therefore needs to configure a remoting actor-ref provider and host+port pair that the other node can connect through. You can read details about how to do this in the akka docs here.
HI I am new to akka dispatchers i took help from the akka documentation
I want to check either i tuned up the dispatcher correctly or not
here is my applica.conf
include "DirectUserWriteMongoActor"
akka {
loggers = ["akka.event.slf4j.Slf4jLogger"]
loglevel = "DEBUG"
}
here is my DirectUserWriteMongoActor.conf
akka {
actor{
############################### Setting for a Dispatcher #####################################
directUserWriteMongoActor-dispatcher {
type = Dispatcher
executor = "fork-join-executor"
fork-join-executor {
parallelism-min = 2
parallelism-factor = 2.0
parallelism-max = 10
}
throughput = 10
} #end default-dispatcher
############################### Setting for a Router #####################################
deployment{
/directUserWritwMongoActorRouter{
router = round-robin
nr-of-instances = 5
}
}#end deployment
} #end Actor
} #end Akka
And here is my code
object TestActor extends App{
val config = ConfigFactory.load().getConfig("akka.actor")
val system = ActorSystem("TestActorSystem",config)
val DirectUserWriteMongoActor = system.actorOf(Props[DirectUserWriteMongoActor].withDispatcher("directUserWriteMongoActor-dispatcher"), name = "directwritemongoactor")
class DirectUserWriteMongoActor extends Actor {
def receive =
{
case _ =>
}
}
when i run it the code compiles but i am wondering how do i get to know whether akka dispatcher is working or not please help
You can always print the thread name from inside an Actor, since thread pools (used by dispatchers) will have proper names you should be able to identify if it's running on the dispatcher you expected it to run.
class CheckingThread extends Actor {
def receive = {
case _ =>
println(s"Using thread: ${Thread.currentThread().getName}")
// or reply with it: sender() ! Thread.currentThread().getName
}
}
You can also just investigate the running threads by opening visual vm or any jvm profiler that you like / have available.
You can use the system object to get the current dispatcher:
scala> val config = ConfigFactory.load().getConfig("akka.actor")
config: com.typesafe.config.Config = Config(SimpleConfigObject(... "default-dispatcher" ... )
scala> val system = ActorSystem("ArteciateActorSystem", config)
system: akka.actor.ActorSystem = akka://ArteciateActorSystem
scala> system.dispatcher.toString
res7: String = Dispatcher[akka.actor.default-dispatcher]
Note that I've cut the code in Config to the relevant part, what you get there is much more verbose.
I am trying to run remote actors using AKKA, on my localhost, but each time I get this error. It says dead letters encountered. I searched on internet and found out that this error comes when actors receive a message after its thread is stopped. So I am looking for a way to keep the actors alive on remote machines. I am using akka actors and not the scala actors.
[INFO] [09/16/2013 18:44:51.426] [run-main] [Remoting] Starting remoting
[INFO] [09/16/2013 18:44:51.688] [run-main] [Remoting] Remoting started; listening on addresses :[akka.tcp://actorSystem1#localhost:2209]
[INFO] [09/16/2013 18:44:51.759] [actorSystem2-akka.actor.default-dispatcher-5] [akka://actorSystem2/deadLetters] Message [java.lang.String] from
Actor[akka://actorSystem2/deadLetters] to Actor[akka://actorSystem2/deadLetters] was not delivered. [1] **dead letters encountered**. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.
Following is the code.
import akka.actor.{Actor, Props, ActorSystem}
import com.typesafe.config.ConfigFactory
import akka.remote._
object MyApp extends App {
val actorSystem1 = ActorSystem("actorSystem1", ConfigFactory.parseString("""
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = ["akka.remote.netty.tcp"]
netty.tcp {
hostname = "localhost"
port = 2209
}
}
}
"""))
val actorSystem2 = ActorSystem("actorSystem2")
actorSystem1.actorOf(Props(new Actor {
def receive = {
case x: String =>
Thread.sleep(1000)
println("RECEIVED MESSAGE: " + x)
} }), "simplisticActor")
val remoteActor = actorSystem2.actorFor("akka://actorSystem1#localhost:2209/user/simplisticActor")
remoteActor ! "TEST 1"
remoteActor ! "TEST 2"
Thread.sleep(1000)
actorSystem1.shutdown()
actorSystem2.shutdown()
}
Thanks in advance.
I think I see a few issues with your code that might be leading to deadletters. First, if your intention is to lookup an actor on a remote system from actorSystem2 into actorSystem1, then you will still need to set up remoting properties for actorSystem1, most specifically, you need to make sure it's using the RemoteActorRefProvider. If you don't do this, system 2 will not be able to lookup a remote actor in system 1. Once you make this change, I would also change your remote actor lookup from:
val remoteActor = actorSystem2.actorFor("akka://actorSystem1#localhost:2209/user/simplisticActor")
to:
val remoteActor = actorSystem2.actorSelection("akka.tcp://actorSystem1#localhost:2209/user/simplisticActor")
The actorFor method has been deprecated and also I think you left off the .tcp part of the akka protocol for looking up the remote actor.
Make these changes and then see if things work for you.
I am trying to set up a simple server/client akka (using Akka 2.0.3) application, but it failed to connect. Beforehand here is the basic code:
import com.typesafe.config.ConfigFactory
import akka.actor.Actor
import akka.actor.ActorSystem
import akka.actor.Props
class Server extends Actor {
def receive = {
case s: String => println("Got " + s)
}
}
val serverSystem = ActorSystem("server", ConfigFactory.load(ConfigFactory.parseString("""
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
remote {
transport = "akka.remote.netty.NettyRemoteTransport"
netty {
hostname = "localhost"
port = 5678
}
}
}
""")))
val server = serverSystem.actorOf(Props[Server], name = "server")
Thread.sleep(500)
println("started")
Thread.sleep(500)
val clientSystem = ActorSystem("client", ConfigFactory.load(ConfigFactory.parseString("""
akka {
actor {
provider = "akka.remote.RemoteActorRefProvider"
}
}
""")))
val remoteServer = clientSystem.actorFor("akka://server#XXX:5678/user/server")
remoteServer ! "HEY"
Thread.sleep(3000)
clientSystem.shutdown
serverSystem.shutdown
I know that the configurations should be placed in external files.
If you replace XXX with localhost it works:
started
Got HEY
But if I used my external (resolved) IP (PC behind home router) for XXX the HEY message never arrives. I thought it is due to some firewall problem and forwarded the related TCP and UDP ports at my router and also opened/allowed them at my Windows firewall. So after that the following code worked (also XXX replaced with my external IP). A started ServerTest can be connected by a ClientTest:
import java.net.ServerSocket
object ServerTest extends App {
println("START server")
val ss = new ServerSocket(5678)
val s = ss.accept()
println(s)
println("END")
}
import java.net.Socket
object ClientTest extends App {
println("START client")
val s = new Socket("XXX", 5678)
println(s)
println("END")
}
So it´s not a port/firewall problem, isn´t it?! So where is the problem???
localhost usually means 127.0.0.1, which is only one of the possibly many interfaces (cards) in a computer. The server binding to localhost won't receive connections connecting to the other interfaces (including the one with the external address).
You should either specify the external address in the server, or 0.0.0.0 which means "bind to all interfaces".