1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 00:17:11 -07:00

Close the socket.

This commit is contained in:
Cedric Beust 2016-04-28 05:21:59 -08:00
parent f79d9f9497
commit e6a2ad98f7
2 changed files with 18 additions and 16 deletions

View file

@ -5,9 +5,10 @@ import java.io.PrintWriter
import java.net.Socket
fun main(argv: Array<String>) {
val socket = Socket("localhost", 1234)
(PrintWriter(socket.outputStream, true)).use { out ->
out.println("""{ "name" : "getDependencies", "buildFile":
"/Users/beust/kotlin/kobalt/kobalt/src/${Constants.BUILD_FILE_NAME}"}""")
Socket("localhost", 1234).use { socket ->
(PrintWriter(socket.outputStream, true)).use { out ->
out.println("""{ "name" : "getDependencies", "buildFile":
"/Users/beust/kotlin/kobalt/kobalt/src/${Constants.BUILD_FILE_NAME}"}""")
}
}
}

View file

@ -112,18 +112,19 @@ class ServerProcess {
}
try {
val socket = Socket("localhost", result)
val outgoing = PrintWriter(socket.outputStream, true)
val c: String = """{ "name": "ping"}"""
outgoing.println(c)
val ins = BufferedReader(InputStreamReader(socket.inputStream))
var line = ins.readLine()
val jo = JsonParser().parse(line) as JsonObject
val jsonData = jo["data"]?.asString
val dataObject = JsonParser().parse(jsonData) as JsonObject
val received = JsonParser().parse(dataObject["received"].asString) as JsonObject
if (received["name"].asString == "ping") {
result = properties.getProperty(KEY_PORT).toInt()
Socket("localhost", result).use { socket ->
val outgoing = PrintWriter(socket.outputStream, true)
val c: String = """{ "name": "ping"}"""
outgoing.println(c)
val ins = BufferedReader(InputStreamReader(socket.inputStream))
var line = ins.readLine()
val jo = JsonParser().parse(line) as JsonObject
val jsonData = jo["data"]?.asString
val dataObject = JsonParser().parse(jsonData) as JsonObject
val received = JsonParser().parse(dataObject["received"].asString) as JsonObject
if (received["name"].asString == "ping") {
result = properties.getProperty(KEY_PORT).toInt()
}
}
} catch(ex: IOException) {
log(1, "Couldn't connect to current server, launching a new one")