我創建了一個Tcp客戶端& Groovy中的Tcp服務器,並且沒有問題。我當時只連接到一臺機器來收集數據。這次我嘗試連接到多個主機上的腳本,它只保存我的grails應用程序中的一個主機信息。Grails問題處理Tcp客戶端和Tcp服務器
我的Grails應用程序很簡單,它有一個Machine類的域類(基本上是計算機及其所尋求的信息),它將使用我的TcpClient.groovy腳本連接並從TcpServer.groovy收集信息其他電腦。對於每個主機,它應該保存收集的信息,但是,它似乎跳過了將最後一個主機旁邊的任何主機保存的問題。
TCP客戶端:
//TCP CLIENT
public void queryData(def hosts) {
for(int aHost = 0; aHost < hosts.size; aHost++) {
cristalClient(hosts[aHost]);
}
}
public void cristalClient(String host) {
commands = ["dateScan", "computerName", "ip", "quit"]
answers = [commands.size]
requestSocket = new Socket(host, 2000)
r = new BufferedReader(new InputStreamReader(requestSocket.getInputStream()));
w = new BufferedWriter(new OutputStreamWriter(requestSocket.getOutputStream()));
String message = "Connection was successful"
message = readAvailable(r)
println("Sever>" + message)
for(int n = 0; n < commands.size; n++) {
sendMessage(commands[n]);
answers[n] = readAvailable(r)
}
lastRead = answers[0]
machineName = answers[1]
ipAddress = answers[3]
w.flush()
w.close()
}
public String readAvailable(r) {
String out = ""
String dum = null
while((dum = r.readLine()) !=null) {
if(dum == ">>EOF<<") return out
if(out.length() > 0) out += "\r\n"
out += dum
}
return out
}
public void sendMessage(msg) {
w.write(msg+"\r\n");
w.flush();
println("Client>" + msg);
}
public void printData(abc) {
abc.eachWithIndex { it, index ->
println "Drive $index"
it.each { k, v ->
println "\t$k = $v"
}
}
}
TCP服務器:
//TCP Server
def server = new ServerSocket(2000)
println("Waiting for connection")
server.accept() { socket ->
socket.withStreams { input, output ->
w = new BufferedWriter(new OutputStreamWriter(output))
String message = "Connection was successful"
r = new BufferedReader(new InputStreamReader(input))
while(true) {
if(message != null) {
sendMessage(message)
message = null
}
String a = r.readLine()
if(a == "dateScan") {
message = new Date
} else if(a == "computerName") {
message = InetAddress.getLocalHost().hostName
} else if(a == "ip") {
message = InetAddress.getLocalHost().getHostAddress()
} else if(a == "quit") {
server.close()
return
} else {
message = "$a command unknown."
println message
}
}
}
}
def sendMessage(String msg) {
println("sending: >" + msg + "<")
w.writeLine(msg)
w.writeLine(">>EOF<<")
w.flush();
}
的Grails控制器:
//Grails Controller
CollectMachines {
def w = new tcpClient()
def hosts = ["winXp", "Win7"]
w.queryData(hosts)
def abc = w.hardDrive
abc.each { println it }
int numberOfDrives = abc.size()
//add new machine
numberOfDrives.times {
def machineName = abc.computerName[it]
def machineInstance = Machine.findByMachineName(machineName)
if (!machineInstance) {
machineInstance = new Machine(machineName)
}
def lastScan = abc.lastScan[it]
def scanDate = new Date().parse("E MMM dd H:m:s z yyyy", lastScan)
def ipAddress = abc.ipAddress[it]
machineInstance.setIpAddress(ipAddress)
machineInstance.setDateScanned(scanDate)
machineInstance.save()
}
redirect(action: "list")
}
我是否需要暫停,以便服務器有時間發送響應?我的Tcp客戶端發出所有的命令,但只獲得最後一組命令的響應。
另外,對於我的代碼片段中的縮進問題感到抱歉,我不確定它們爲什麼會搞砸。
。
我的歉意,我不小心拿出將數據推入硬盤的代碼。我試圖減少代碼,並拿出不必要的來源。我認爲你對每個主持人的地圖都是正確的,我會實施它並回傳。 – StartingGroovy 2011-01-21 20:21:10