Packages

  • package root
    Definition Classes
    root
  • package grizzled

    The Grizzled Scala Library contains a variety of miscellaneous, general purpose utility classes and objects.

    The Grizzled Scala Library contains a variety of miscellaneous, general purpose utility classes and objects.

    The home page for the Grizzled Scala Library is http://software.clapper.org/grizzled-scala/. Please see that page for complete details, including installation instructions.

    Definition Classes
    root
  • package ScalaCompat

    Compatibility definitions for Scala 2.13+ vs.

    Compatibility definitions for Scala 2.13+ vs. Scala 2.12 and lesser. This object is conceptually similar to scala.collection.compat. For Scala 2.12 and earlier, it provides a type alias and compatibility functions for LazyList. For Scala 2.13 and greater, it's empty. Thus, all code can use LazyList throughout.

    Definition Classes
    grizzled
  • package collection

    Some collection-related helpers.

    Some collection-related helpers.

    Definition Classes
    grizzled
  • package config

    Classes and objects to aid in the parsing of INI-style configuration files.

    Classes and objects to aid in the parsing of INI-style configuration files. This package is similar, in concept, to the Python ConfigParser module (though its implementation and capabilities differ quite a bit).

    Definition Classes
    grizzled
  • package datetime
    Definition Classes
    grizzled
  • package file

    File-related classes and utilities.

    File-related classes and utilities. This package is distinguished from the grizzled.io package in that this package operates on files and paths, not on open streams or sources.

    Definition Classes
    grizzled
    See also

    grizzled.io

  • package io

    I/O-related classes and utilities.

    I/O-related classes and utilities. This package is distinguished from the grizzled.file package in that this package operates on already-open Java InputStream, OutputStream, Reader and Writer objects, and on Scala Source objects.

    See grizzled.file

    Definition Classes
    grizzled
  • package math

    Miscellaneous math and statistics utilities.

    Miscellaneous math and statistics utilities.

    Definition Classes
    grizzled
  • package net

    Network-related utility methods and classes.

    Network-related utility methods and classes.

    Definition Classes
    grizzled
  • IPAddress
  • Implicits
  • UDPDatagramSocket
  • URI
  • URL
  • URLUtil
  • package parsing

    Methods and classes useful for parsing various things.

    Methods and classes useful for parsing various things.

    Definition Classes
    grizzled
  • package random
    Definition Classes
    grizzled
  • package security
    Definition Classes
    grizzled
  • package string

    String- and text-related classes.

    String- and text-related classes.

    Definition Classes
    grizzled
  • package util

    Miscellaneous utility functions and methods not otherwise categorized.

    Miscellaneous utility functions and methods not otherwise categorized.

    Definition Classes
    grizzled
  • package zip

    The grizzled.zip package contains classes and functions to make it easier to operate on zip and jar files.

    The grizzled.zip package contains classes and functions to make it easier to operate on zip and jar files.

    Definition Classes
    grizzled

package net

Network-related utility methods and classes.

Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. net
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. class IPAddress extends AnyRef

    Represents an IP address.

    Represents an IP address. This class is similar to java.net.InetAddress, but it's designed to be more intuitive and easier to use from Scala. This package provides implicit converters to make this class compatible with java.net.InetAddress. The converters ensure that all the (non-static) methods defined in the java.net.InetAddress class are directly callable from an instance of the Scala IPAddress class. For instance, the following code is perfectly legal:

    val ip = IPAddress(192, 168, 2, 5)
    
    // Test if the address is reachable within 1 second.
    println(ip + " is reachable? " + ip.isReachable(1000))
    
    // Get the canonical host name for (i.e., do a reverse lookup on) the
    // address.
    println(ip + " -> " + ip.getCanonicalHostName)
    
    // Determine whether it's the loopback address.
    println(ip + " == loopback? " + ip.isLoopbackAddress)

    Here's an IPv6 example:

    val ip = IPAddress("fe80::21d:9ff:fea7:53e3")
    
    // Test if the address is reachable within 1 second.
    println(ip + " is reachable? " + ip.isReachable(1000))
    
    // Get the canonical host name for (i.e., do a reverse lookup on) the
    // address.
    println(ip + " -> " + ip.getCanonicalHostName)
    
    // Determine whether it's the loopback address.
    println(ip + " == loopback? " + ip.isLoopbackAddress)
  2. trait UDPDatagramSocket extends AutoCloseable

    A UDPDatagramSocket object represents a UDP datagram socket, providing a simpler interface to sending and receiving UDP packets than the one provided by the Java JDK.

    A UDPDatagramSocket object represents a UDP datagram socket, providing a simpler interface to sending and receiving UDP packets than the one provided by the Java JDK.

    Sending UDP Datagrams

    The easiest way to explain how to use this API is with some code. So, without further ado, the following example shows how you might send the string "foo" to port 2003 on host "foo.example.com".

    // Import the appropriate stuff.
    import grizzled.net._
    
    // First, create an grizzled.net.IPAddress object for the destination
    // machine.
    val address = IPAddress("foo.example.com")
    
    // Next, create the socket object. Since we're sending the packet,
    // we don't care what the local port is. By not specifying one, we allow
    // the operating system to choose one one for us. Similarly, by not
    // passing an explicit address, we indicate that the API should bind to a
    // wildcard address, so that the packet can go out over any appropriate
    // interface.
    val socket = UDPDatagramSocket()
    
    // Next, use the sendString() method to send the string
    socket.sendString("foo", address, 2003)
    
    // Finally, close the socket.
    socket.close()

    That's pretty simple. However, using the utility methods provided by the UDPDatagramSocket companion object, we can further simplify the above code:

    // Import the appropriate stuff.
    import grizzled.net._
    
    UDPDatagramSocket.sendString("foo", IPAddress("foo.example.com"), 2003)

    The sendString() method in the companion object takes care of allocating the socket, sending the packet, and closing the socket. Obviously, if you're planning on sending multiple packets at once, you'll want to use the first example (perhaps in a loop), so you're not constantly allocating and deallocating sockets. But sending a one-shot UDP packet can be as simple as one line of code, as shown in the second example.

    Sending binary data is not much more complicated. You have to convert the data to a stream of bytes, which the server must then decode. After that, however, sending the bytes isn't much more difficult than sending a string. Here's an example, which assumes that you have already encoded the data to be send into an array of bytes.

    // Import the appropriate stuff.
    import grizzled.net._
    
    // Encode the data into bytes. (Not shown.)
    val data: Array[Byte] = encodeTheData()
    
    // Create the socket object.
    val socket = UDPDatagramSocket()
    
    // Send the data.
    socket.send(data, IPAddress("foo.example.com"), 2003)
    
    // Finally, close the socket.
    socket.close()

    Once again, there's a simple utility method that does most of the work for you:

    // Import the appropriate stuff.
    import grizzled.net._
    
    // Encode the data into bytes. (Not shown.)
    val data: Array[Byte] = encodeTheData()
    
    // Send the data.
    UDPDatagramSocket.send(data, IPAddress("foo.example.com"), 2003)

    Receiving UDP Datagrams

    When receiving UDP datagrams, you must first bind to an incoming socket. That is, you must listen on the port to which clients are sending their packets. In this example, we create the socket object with the receiving port, and we allow the wildcard address to be used on the local machine (permitting us to receive packets on any interface).

    // Import the appropriate stuff.
    import grizzled.net._
    
    // Create the socket object.
    val socket = UDPDatagramSocket(2003)

    Next, we want to receive and process the incoming data. Let's assume that we're receiving the "foo" string (or, for that matter, any string) being sent by the sample sender, above.

    // Receive and print strings.
    while (true)
        println(socket.receiveString(1024))

    That code says, "Wait for incoming strings, using a 1024-byte buffer. Then, decode the strings and print them to standard output.

    Receiving bytes isn't much more difficult.

    // Allocate a byte buffer. For efficiency, we'll re-use the same buffer
    // on every incoming message.
    val buf = Array.make[Byte](1024, 0)
    
    // Receive and process the incoming bytes. The process() method isn't
    // shown.
    while (true)
    {
        val totalRead = socket.receive(buf)
        process(buf, totalRead)
    }

    The above loop can be made even simpler (though a little more obscure), since the receive() method is filling our buffer and returning a count of the number of bytes it put into the buffer:

    while (true)
        process(buf, socket.receive(buf))
  3. final case class URI(scheme: Option[String], userInfo: Option[String], host: Option[String], port: Option[Int], path: Option[String], query: Option[String] = None, fragment: Option[String] = None) extends Product with Serializable

    Convenient Scala case-class wrapper for a java.net.URI.

    Convenient Scala case-class wrapper for a java.net.URI.

    scheme

    the scheme, if defined

    userInfo

    the user info, if defined

    host

    the host, if defined

    port

    the port, if defined

    path

    the path, if defined

    query

    the query string, if defined

    fragment

    the fragment, if defined

    Annotations
    @SuppressWarnings()
  4. final case class URL(protocol: String, host: Option[String], port: Option[Int], path: Option[String], query: Option[String] = None, userInfo: Option[String] = None, fragment: Option[String] = None) extends Product with Serializable

    Convenient Scala case-class wrapper for a java.net.URL.

    Convenient Scala case-class wrapper for a java.net.URL. This class doesn't include all the capabilities. For example, it lacks the equivalent of getContent(), as that's better handled through other means.

    protocol

    the protocol, if defined

    host

    the host, if defined

    port

    the port, if defined

    path

    the path

    query

    the query string, if any

    userInfo

    the URL's user info, if any

    fragment

    the fragment, if any

    Annotations
    @SuppressWarnings()

Value Members

  1. object IPAddress

    Companion object to IPAddress class.

  2. object Implicits

    Implicit conversions for network classes and types.

  3. object UDPDatagramSocket

    Companion object for the UDPDatagramSocket trait, containing methods to simplify creation of UDPDatagramSocket objects, as well as some useful utility methods.

    Companion object for the UDPDatagramSocket trait, containing methods to simplify creation of UDPDatagramSocket objects, as well as some useful utility methods. See the documentation for the UDPDatagramSocket trait for a full treatment on this API.

    See also

    UDPDatagramSocket

  4. object URI extends Serializable

    Companion object, adding some functions that aren't available in the generated one.

  5. object URL extends Serializable

    Companion object, adding some functions that aren't available in the generated one.

  6. object URLUtil

    URL-related utility methods.

Inherited from AnyRef

Inherited from Any

Ungrouped