Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8807427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:21:59+00:00 2026-06-14T02:21:59+00:00

The cases in the act method of the main actor are never matched in

  • 0

The cases in the act method of the main actor are never matched in this code, so my wrapUp method is never called.

import java.net.Socket
import scala.actors._
import Actor._
import scala.collection.mutable.ArrayBuffer

object LowPortScanner {

  var lastPort = 0
  var openPorts = ArrayBuffer[Int]()
  var longestRunTime = 00.00
  var results = List[Tuple3[Int, Range, Long]]()

  val host = "localhost"
  val numProcs = 1 to Runtime.getRuntime().availableProcessors()
  val portsPerProc = 1024 / numProcs.size
  val caller = self
  val procPortRanges = numProcs.foldLeft(List[Tuple2[Int, Range]]()) { (portRanges, proc) =>
    val tuple2 = (proc.toInt, (lastPort + 1) to (lastPort + portsPerProc))
    lastPort += portsPerProc
    tuple2 :: portRanges
  }

  def main(args: Array[String]): Unit = {

    //spawn an actor for each processor that scans a given port range
    procPortRanges.foreach { proc =>
      actor {
        caller ! scan(proc._1, proc._2)
      } //end inner actors
    } //end numProcs.foreach  

    //catch results from the processor actors above
    def act {
      loop {
        reactWithin(100) {
          //update the list of results returned from scan
          case scanResult: Tuple3[Int, Range, Long] =>
            println("Processor " + scanResult._1 + " completed scan of ports " + scanResult._2.first + " through " + scanResult._2.last)
            results = results ::: List(scanResult)

          //check if results have been returned for each actor
          case TIMEOUT =>
            println("Main actor timed out")
            if (results.size == numProcs.size) wrapUp

          case _ =>
            println("got back something weird from one of the port scan actors!")
            wrapUp
        }
      }
    }    

    //Attempt to open a socket on each port in the given range
    //returns a Tuple3[procID: Int, ports: Range, time: Long
    def scan(proc: Int, ports: Range) = {
      val startTime = System.nanoTime()
      ports.foreach { n =>
        try {
          //println("Processor " + proc + " is checking port " + n)
          print(".")

          val socket = new Socket(host, n)

          //println("Found open port: " + n)
          openPorts += n

          socket.close
        } catch {
          case e: Exception =>
          //println("While scanning port " + n + " caught Exception: " + e)
        }
      }
      (proc, ports, startTime - System.nanoTime())
    }

    //output results and kill the main actor
    def wrapUp {
      println("These are the open ports in the range 1-1024:")
      openPorts.foreach { port => println(port) }
      results.foreach { result => if (result._3 > longestRunTime) { longestRunTime = result._3 } }
      println("Time to scan ports 1 through 1024 is: %3.3f".format(longestRunTime / 1000))
      caller ! exit
    }
  }
}

The code that spawns off the actors for each processor works fine, the scan method is invoked correctly for each range of ports, and I have confirmed that exactly 1024 ports are scanned.
My expectation is that the line :

caller ! scan(proc._1, proc._2)  

should send a message containing (Int, Range, Long) back to the main actor. The main actor’s act method should then catch that message and execute:

          case scanResult: Tuple3[Int, Range, Long] =>
            println("Processor " + scanResult._1 + " completed scan of ports " + scanResult._2.first + " through " + scanResult._2.last)
            results = results ::: List(scanResult)  

This is not happening, however. In fact, as far as I can tell, none of my messages are coming back to the main actor. I’m not clear on what I’m missing or doing wrong.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:22:00+00:00Added an answer on June 14, 2026 at 2:22 am

    The problem as you note is that the message being sent are not being received. This is because the actor created with actor {caller ! scan(proc._1, proc._2)} does not have an act definition associated with it. The def act{...} method has nothing to do with the actor created since it is a method on LowPortScanner and not an actor.

    Maintaining the spirit of your code you can define the body to be executed by the actor within the actor{...} and assign it to a value and send it messages.

     //spawn an actor for each processor that scans a given port range
     procPortRanges.foreach { proc =>
       val myactor = actor {
         //catch results from the processor actors above
           loop {
             reactWithin(100) {
             //update the list of results returned from scan
             case scanResult: Tuple3[Int, Range, Long] =>
               println("Processor " + scanResult._1 + " completed scan of ports " + scanResult._2.first + " through " + scanResult._2.last)
               results = results ::: List(scanResult)
    
             //check if results have been returned for each actor
             case TIMEOUT =>
               println("Main actor timed out")
               if (results.size == numProcs.size) wrapUp
    
             case _ =>
               println("got back something weird from one of the port scan actors!")
               wrapUp
            }
          }
         //catch results from the processor actors above
       } //end inner actors
         myactor ! scan(proc._1, proc._2)
     } //end numProcs.foreach
    

    Another way to do it is to extend the Actor trait. This way the def act{...} will handle messages received by the LowPortScanner actor. I also did some minor refactoring including using this instead of self. The Actor also had to be started with this.start().

    import java.net.Socket
    import scala.actors._
    import Actor._
    import scala.collection.mutable.ArrayBuffer
    
    object LowPortScanner extends Actor {
    
      var lastPort = 0
      var openPorts = ArrayBuffer[Int]()
      var longestRunTime = 00.00
      var results = List[Tuple3[Int, Range, Long]]()
    
      val host = "localhost"
      val numProcs = 1 to Runtime.getRuntime().availableProcessors()
      val portsPerProc = 1024 / numProcs.size
    
      val procPortRanges = numProcs.foldLeft(List[Tuple2[Int, Range]]()) { (portRanges, proc) =>
        val tuple2 = (proc.toInt, (lastPort + 1) to (lastPort + portsPerProc))
        lastPort += portsPerProc
        tuple2 :: portRanges
      }
        //catch results from the processor actors above
        def act {
          loop {
            reactWithin(100) {
              //update the list of results returned from scan
              case scanResult: Tuple3[Int, Range, Long] =>
                println("Processor " + scanResult._1 + " completed scan of ports " + scanResult._2.first + " through " + scanResult._2.last)
                results = results ::: List(scanResult)
    
              //check if results have been returned for each actor
              case TIMEOUT =>
                println("Main actor timed out")
                if (results.size == numProcs.size) wrapUp
    
              case _ =>
                println("got back something weird from one of the port scan actors!")
                wrapUp
            }
          }
        }
    
        //Attempt to open a socket on each port in the given range
        //returns a Tuple3[procID: Int, ports: Range, time: Long
        def scan(proc: Int, ports: Range) = {
          val startTime = System.nanoTime()
          ports.foreach { n =>
            try {
              //println("Processor " + proc + " is checking port " + n)
              print(".")
    
              val socket = new Socket(host, n)
    
              //println("Found open port: " + n)
              openPorts += n
    
              socket.close
            } catch {
              case e: Exception =>
              //println("While scanning port " + n + " caught Exception: " + e)
            }
          }
          (proc, ports, startTime - System.nanoTime())
        }
    
        //output results and kill the main actor
        def wrapUp {
          println("These are the open ports in the range 1-1024:")
          openPorts.foreach { port => println(port) }
          results.foreach { result => if (result._3 > longestRunTime) { longestRunTime = result._3 } }
          println("Time to scan ports 1 through 1024 is: %3.3f".format(longestRunTime / 1000))
          this ! exit
        }
    
      def main(args: Array[String]): Unit = {
    
        //spawn an actor for each processor that scans a given port range
        this.start()
        procPortRanges.foreach { proc =>
          actor {
            this ! scan(proc._1, proc._2)
          } //end inner actors
        } //end numProcs.foreach  
      }
    
    }
    

    Here are the results from a run:

    scala> LowPortScanner.main(Array[String]())
    
    scala> ...Processor 6 completed scan of ports 641 through 768
    ...Processor 5 completed scan of ports 513 through 640
    ...Processor 4 completed scan of ports 385 through 512
    ...Processor 3 completed scan of ports 257 through 384
    ...Processor 1 completed scan of ports 1 through 128
    ...Processor 7 completed scan of ports 769 through 896
    ...Processor 2 completed scan of ports 129 through 256
    ...Processor 8 completed scan of ports 897 through 1024
    Main actor timed out
    These are the open ports in the range 1-1024:
    139
    22
    445
    591
    631
    111
    Time to scan ports 1 through 1024 is: 0.000
    
    
    scala> LowPortScanner.results
    res2: List[(Int, Range, Long)] = List((6,Range(641, 642, 643,...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: I have this code: class MyActor extends Actor { def act() { react
So I have the following code: import java.lang.Thread; import java.lang.Integer; class MyThread extends Thread
I need to act differently in onStart() method depending on how onCreate() was called
Not sure whether I am missing something. When making an actor remote, the main
.NET has a thing called remoting where you can pass objects around between separate
I was wondering how to apply a match and case to my act() method.
I have some WCF service code that is virtually the same, except one method
I am new to this fancy language called Scala. I have enough (I guess)
I am actually using a pyopengl program to act as a socket server. At
I am building a native iPhone application wherein this application will act as a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.