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 3786546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:42:01+00:00 2026-05-19T11:42:01+00:00

I created a Tcp Client & Tcp Server in Groovy awhile back and had

  • 0

I created a Tcp Client & Tcp Server in Groovy awhile back and had no issues with it. I was only connecting to one machine at the time to gather data. This time I am attempting to connect to the script on multiple hosts and it is only saving one of the hosts information in my grails app.

My Grails application is simple, it has a domain class for Machines (basically the computers and the information on them that I seek) and it will use my TcpClient.groovy script to connect and gather information from the TcpServer.groovy on the other computers. For each host, it should save the information gathered, however, it seems to skip right over saving any host aside from the last one.

Tcp Client :

//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 Server :

//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 Controller :

//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")
}

Do I need to put a pause in so that the server has time to send a response? My Tcp Client does send out all the commands but only gets responses for the last set of commands.

Also, sorry for the indentation issues with my code snippets, I’m not sure why they are messed up.

.

  • 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-05-19T11:42:02+00:00Added an answer on May 19, 2026 at 11:42 am

    There are a few problems with your code. tcpClient never assigns to hardDrive, for example. Assuming this is an oversight, I think the real problem is that tcpClient is querying data for multiple hosts, and storing all the results in the same instance variables answers, and ultimately lastRead, machineName, and ipAddress.

    You need to store the results for each host separately. One way would be to have answers be a map of lists. For example, answers[host][0] would be the first answer for a given host.

    I don’t think any kind of pause is necessary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My application is a tcp/ip server, with main thread created only once & listening
When a client connects to a server using TCP, a new socket is created
I have created a TCP server. I am facing one problem. my TCP Server
So I created a TCP\HTTP server (IN C#). I want to give to it
When a server accepts a client over a tcp/ip connection, a new socket is
I created a client and server that connect to each other. The server is
If you create a TCP client socket with port 0 instead of a non-zero
I have written a Python TCP/IP server for internal use, using win32serviceutil/py2exe to create
I am trying to create a tcp synchronous server. My main thread would create
I'm working on this example with C threaded server and java client. This is

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.