summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/util/SocketConnection.scala
blob: 531caaf30dea00c59654979791733947b14f8685 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package scala.tools.util

import java.io._
import java.net._

class SocketConnection(hostname: String, port: int) {

  def this(port: int) = this(InetAddress.getLocalHost().getHostName(), port)

  private var socket: Socket = _
  var out: PrintWriter = _
  var in: BufferedReader = _
  var errorMessage: String = _

  def open(): boolean = {
    try {
      socket = new Socket(hostname, port)
      out = new PrintWriter(socket.getOutputStream(), true)
      in = new BufferedReader(new InputStreamReader(socket.getInputStream()))
      true
    } catch {
      case e: UnknownHostException =>
        errorMessage = "Don't know about host: "+hostname+"."
        false
      case e: IOException =>
        errorMessage = "Couldn't get I/O for the connection to: "+hostname+"."
        false
    }
  }

  def close() = {
    in.close()
    out.close()
    socket.close()
  }
}